Research on 1:jquery._deferred method of JQuery asynchronous framework

Source: Internet
Author: User
Tags finally block

The jquery asynchronous framework is one of the basic functions of jquery, such as jquery data cache module, jquery ajax module, jquery event binding module, and many other modules. is actually a jquery implementation of an asynchronous processing framework, in essence, there is no difference from Java AIO, so the need for a more abstract level of "asynchronous processing" perspective analysis of the module. This section has little to do with DOM functionality and is an independent part that can be seen as one of the jquery tool families.

The methods associated with the asynchronous framework are defined in the static methods of the jquery class. There are only three methods, but features and applications and their power! This article explains the first method jquery._deferred () in detail.


_deferred:function () {var//callbacks listcallbacks = [],//stored [context, args]fired,//to avoid firing when Alrea DY doing sofiring,//flag to know if the deferred have been cancelledcancelled,//the deferred itselfdeferred = {//Done ( F1, F2, ...) Done:function () {if (!cancelled) {var args = Arguments,i,length,elem,type,_fired;if (fired) {_fired = fired;fired = 0 ;}  for (i = 0, length = args.length; i < length; i++) {elem = args[i];type = Jquery.type (Elem); if (type = = = "Array" ) {deferred.done.apply (deferred, elem);} else if (type = = = "function") {Callbacks.push (elem);}} if (_fired) {Deferred.resolvewith (_fired[0], _fired[1]);}} Return this;},//resolve with given context and argsresolvewith:function (context, args) {if (!cancelled &&!fi Red &&!firing) {//Make sure Args is available (#8421) args = args | | [];firing = 1;try {while (callbacks[0]) {callbacks.shift (). Apply (context, args);}} finally {fired = [context, args];firing = 0;}} Return this;},//resolve with this as context and given argumentsresolve:function () {Deferred.resolvewith (this, argument s); return this;},//have this deferred been resolved?isresolved:function () {return!! (Firing | | fired);},//cancelcancel:function () {cancelled = 1;callbacks = [];return this;}}; return deferred;},

This is the most basic function of the jquery asynchronous framework, the real external exposure of the deferred function depends on the implementation of the function, from its name can be seen, jquery designers do not want users to call the method directly, although this method has been very flexible can be applied to many scenarios, And the JavaScript language has no mechanism at the language level to prevent users from actually calling the method.

Analysis of its internal implementation can be found that the function is designed to be called when it does not need to pass in any parameters, but instead directly call to get an asynchronous object: var deferred = jquery._deferred (). The focus is on the returned asynchronous object, which has five methods: done, Resolvewith, Resolve, isresolved, and Cancel, each of which deserves in-depth analysis. Of course, there are four variables in the _deferred function: callbacks, fired, firing, cancelled, these four variables are also worth in-depth analysis.

1 Done methods
The implementation of this method is to push the passed parameters into the callbacks empty array so that subsequent resolvewith methods use the functions in the callbacks array.

The parameters of this method only accept two types: either a function or an array of functions, and other types are not processed. and recursively invokes the done method itself when passing in an array of functions to continue pushing the function elements in the array into the callbacks array.

The Resolvewith method can be used to know that the main function of the done method is to store the set of functions that will be executed, and the function of the Resolvewith method is to invoke the set of functions sequentially on the specified object according to the specified Parameter object. Therefore, the callbacks function set is equivalent to the callback processor collection, and fired = [context, args] is equivalent to the calling object (including its arguments). The flexibility that comes with this separation is amazing-the asynchronous processing framework is about to be formed. By the way, the name of this method is done very unfriendly, for the familiar callback pattern of friends, in fact, this method name should be addhandlers or addcallbacks, or even return object deferred (delay) The name is not as intuitive as asynchor.

This method finally returns the This keyword, which is the Deferred object itself, and the benefit is that it can be called multiple times, such as jquery._deferred (). Done (F1,F2). Done ([F3,f4,f5]), If other methods of the deferred object are doing the same thing, you can also invoke other methods in a chained way, and the other method is doing so.

The role of _fired variables and related code will be clear after analyzing the Resolvewith method.

2 Resolvewith method
The implementation of the Resolvewith method is that the function in the iteration callback function collection callbacks executes above the specified object context and parameter args--if the callbacks is not empty, Finally, the specified object context and parameter args are always stored in the fired variable-it is possible that the callback has not been executed if callbacks is empty.

The Resolvewith method first determines whether the callback function set callbacks has a value of "callbacks[0]", and nothing is triggered.

The core of the Resolvewith method is this line: Callbacks.shift (). Apply (context, args). In addition to removing the first function element call from the callback function collection callbacks It also removes the element, altering the callbacks array so that each element in the callback function collection callbacks can only be executed once.

For the military fan, there is a model similar to the _deferred function here, almost no more appropriate analogy-pistol.
Throughout the _deferred function, the callback function array callbacks equivalent to the magazine;
The function parameters passed to the Done method are equivalent to the bullets that will be stored in the magazine, and the main duty of the Do method is to provide a "bomb-in-clip" operation;
The parameter passed to the Resolvewith method (context, args) is equivalent to a pistol, and the main responsibility of the Resolvewith method is to perform a "shot" operation;
The fired variable is equivalent to a holster, storing the pistol passed to the Resolvewith method [context, args];

Now look back to the done method, the done method is used in conjunction with the Resolvewith, in general, both can be shot after the first--call the Done method after the call to the Resolvewith method, this scene under the pistol before the existence of the bullets have been available, Therefore, the "fire" operation is triggered directly when the Resolvewith method is called, but it is also possible to reload the gun after the first call to the Resolvewith method, which has no bullets when passing the pistol. Provide bullets to trigger firing action--done method internally calls the Resolvewith method:

if (fired) {_fired = fired;fired = 0;} <span style= "White-space:pre" ></span>//... if (_fired) {Deferred.resolvewith (_fired[0], _fired[1]); }


There is a special operation in addition to the previous two operations: reload--shot------and then shot (call the Done method before calling the Resolvewith method and then call the Do method), this time the done method is loaded with a new bomb ( After passing the new function parameter push to the callback function array, you can repeat the fire (continue to call the Resolvewith method internally), if you do not put on a new bomb (do not pass in the new function parameters) can not be repeated fire (the original callback function array after the Resolvewith method call once emptied, It can not be reused), this operation sequence perfect simulation pistol empty position hang machine reset shooting function (bullet light to prevent the sleeve back, after the replacement of the magazine to quickly push into the bore)!

This line of code "fired = 0;" is important because there is this line of code that can take effect when the done method calls the Resolvewith method to repeat the shot (because the second condition within the Resolvewith method is "if (!cancelled &&!fired & &!firing) ", as we said before fired variable represents a holster, how to understand it? There is also a need to explain the fired of the holster variable, see the section explaining fired in detail below.

Let's look at an extended operation: jquery._deferred (). Done (F1,F2). Resolvewith (Context1, ARGS1). Do (F3). Do ([F4]). Done (F5,[f6,f7]), Description of the empty position hanging machine reset shooting is indeed repeatable! Should no gun engineer in the world design a pistol that cannot be reused or can only be reused once?

For these two methods, summarize the use of the same gun:
Jquery._deferred (). Done (F1,F2). Resolvewith (Context1, ARGS1)--start with a gun and fire once.
Jquery._deferred (). Resolvewith (Context1, ARGS1). Done (F1,F2)--the gun was first made and then shot once.
Jquery._deferred (). Do (F1,F2). Resolvewith (Context1, ARGS1). Done (F3,F4). Do ([F5,f6],f7)--first shot and then fired once, Then the empty position hangs the machine to shoot two times (can repeat more multiple shots).

The two methods are not finished even after summing up, pay attention to a previous suggestive description: For the same gun, we already know that the same gun can be repeated using different bullets, but if you want to use the same bullets for different guns? The demand is justified – within NATO, the standard 9mm Baram bombs are not used only in desert eagles.

"For different guns" instructions need to call the Resolvewith method at least two times, first call the Resolvewith method passed in parameters (Context1, ARGS1) on behalf of the first gun, the second incoming parameter (CONTEXT2, ARGS2) represents the second gun, Let's see what Happens: "jquery._deferred (). Done (F1,F2). Resolvewith (Context1, ARGS1). Resolvewith (CONTEXT2,ARGS2);" Unfortunately, this call will break the dish! Or because of the fired of the gun set variable. To work around it: "jquery._deferred (). Done (F1,F2). Resolvewith (Context1, ARGS1). Done (F3). Resolvewith (Context2, ARGS2)" or not! The Resolvewith method can and can only be called once because of the fired of the gun sleeve variable, and it does not have to be chained multiple times as done.

In addition to the reasons for the fired variable, even if this reason, "different guns using the same bullets" is not possible, the gun to the bullet is a one-to-many relationship, the bullet to the gun is a one-to-two relationship, that is, the callback function in the array of each bullet was shot by a hand after the shooting and can not be another A truly reasonable demand description is that "different models of firearms should be able to use bullets of the same size."

So the conclusion is that--jquery._deferred () cannot use a different pistol for the same bullet, and it thinks it can only be matched to the first incoming pistol (or the pistol after the first shot), if it is to use a different pistol. --Multiple calls to the Jquery._deferred () method.

3 Gun Set--Variable fired
The first basic point to be familiar with is that the done method and the Resolvewith method operation variable fired are in the call object at the level of the function call object chain, the other three variables, which means that the different methods of deferred objects can work together.

The Resolvewith method has a line of judgment: "If (!cancelled &&!fired &&!firing)", either of these three variables can cause a "fire", For firing and cancelled see detailed analysis below, here to analyze fired variables. If the holster does not have a gun to carry out the shooting operation, or simply that the holster is empty to execute the shot (in fact, both firing and cancelled constraints): The gun sleeve is not empty description of the inside storage pistol, The pistol was not taken out of course.-Actually, even if a new pistol comes in at this point, it can't be used (the jquery._deferred () object is more sentimental, it only uses the first touch of the pistol, which also fully illustrates how important the first time is. )! The Resolvewith method assigns a value to the variable in the Finally block: "fired = [context, args];", which is equivalent to inserting a pistol into a holster after the end of the use,

Then look at the action of the Done method on the fired variable: "if (fired) {_fired = fired;fired = 0;}" when there is a gun in the holster, take it out to the temporary variable in the current calling Object field _fired, be sure to empty the holster! Otherwise the following call to the Resolvewith method will fail (the Resolvewith method will first check the holster condition).

4 Shooting indicator--variable firing
This variable is equivalent to the "shooting indicator", which is currently firing (calling the Resolvewith method and passing the conditional judgment) to the pistol to mark a state-the "firing" state ("firing = 1;"), the pistol in this state cannot pull the trigger again ( That is, the Resolvewith method cannot be called again unless the current shot ends (the state "firing = 0;" is cleared in the finally block).

If you use the parts of the pistol analogy, a bit like "trigger linkage protrusion", the role is the same: the trigger linkage hook stop iron can carry out the current shooting, the trigger linkage decoupling resistance iron, while the sleeve in the back of the action through the "trigger connecting rod protrusion" forced the trigger rod in the shooting process can not automatically return to the hook resistance Until the bullet hit the back of the sleeve into place after the end of the shot to release the trigger linkage, so as to ensure that the shooting process will not repeatedly perform the firing action. Of course, not all pistols are designed to complete the firing indicator function by designing a "trigger linkage bump".

This variable should be too restrictive in the actual execution environment, unless the JavaScript executor is using a multithreaded execution environment and different threads may have race condition access to this variable, I don't think I should need this variable in a single-threaded environment (or perhaps I understand it incorrectly).

5 Universal Insurance--variable cancelled and cancel method
The cancelled variable is very similar to the pistol "firing insurance" + "clip drop insurance", if it is not necessary to call the Cancel method explicitly set, then it can also be similar to "pin insurance", in short, it is an all-round insurance.
After the insurance is closed ("cancelled = 1;"), the Resolvewith method cannot shoot (judging conditions do not pass), just as the fire insurance is locked.
After the insurance is closed ("cancelled = 1;"), the Done method cannot be pressed into the box (judging the condition does not pass), just like the clip is locked dead.
Calling the Cancel method also empties the clip while closing the insurance ("callbacks = [];").
After calling the Cancel method, the whole system is bad, and most of the places are not able to continue to work properly.

6 Resolve method
This method calls the Resolvewith method, and does not need to say much.

7 Isresolved method
As long as the shooting indicator firing on or a gun set fired gun in the thought has been resolved.

In short, if you are a military fan of the pistol model working principle is very clear, then read jquery._deferred source code and the principle is almost natural. Jquery._deferred is just the first most basic part of the jquery async framework that has been so much space, but because it is the foundation of the latter two methods, it is worthwhile to unfold.

Finally solemnly affirm one point: without consent, strict reprint! Respect for the fruits of others ' work is a precondition for equal respect.


Research on 1:jquery._deferred method of JQuery asynchronous framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.