]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/jquery/src/deferred.js
98f9c313f6d7d3bffdf3824a9ebaab086a092673
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / space-pen / node_modules / jquery / src / deferred.js
1 define([
2 "./core",
3 "./var/slice",
4 "./callbacks"
5 ], function( jQuery, slice ) {
6
7 jQuery.extend({
8
9 Deferred: function( func ) {
10 var tuples = [
11 // action, add listener, listener list, final state
12 [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
13 [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
14 [ "notify", "progress", jQuery.Callbacks("memory") ]
15 ],
16 state = "pending",
17 promise = {
18 state: function() {
19 return state;
20 },
21 always: function() {
22 deferred.done( arguments ).fail( arguments );
23 return this;
24 },
25 then: function( /* fnDone, fnFail, fnProgress */ ) {
26 var fns = arguments;
27 return jQuery.Deferred(function( newDefer ) {
28 jQuery.each( tuples, function( i, tuple ) {
29 var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
30 // deferred[ done | fail | progress ] for forwarding actions to newDefer
31 deferred[ tuple[1] ](function() {
32 var returned = fn && fn.apply( this, arguments );
33 if ( returned && jQuery.isFunction( returned.promise ) ) {
34 returned.promise()
35 .done( newDefer.resolve )
36 .fail( newDefer.reject )
37 .progress( newDefer.notify );
38 } else {
39 newDefer[ tuple[ 0 ] + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments );
40 }
41 });
42 });
43 fns = null;
44 }).promise();
45 },
46 // Get a promise for this deferred
47 // If obj is provided, the promise aspect is added to the object
48 promise: function( obj ) {
49 return obj != null ? jQuery.extend( obj, promise ) : promise;
50 }
51 },
52 deferred = {};
53
54 // Keep pipe for back-compat
55 promise.pipe = promise.then;
56
57 // Add list-specific methods
58 jQuery.each( tuples, function( i, tuple ) {
59 var list = tuple[ 2 ],
60 stateString = tuple[ 3 ];
61
62 // promise[ done | fail | progress ] = list.add
63 promise[ tuple[1] ] = list.add;
64
65 // Handle state
66 if ( stateString ) {
67 list.add(function() {
68 // state = [ resolved | rejected ]
69 state = stateString;
70
71 // [ reject_list | resolve_list ].disable; progress_list.lock
72 }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
73 }
74
75 // deferred[ resolve | reject | notify ]
76 deferred[ tuple[0] ] = function() {
77 deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
78 return this;
79 };
80 deferred[ tuple[0] + "With" ] = list.fireWith;
81 });
82
83 // Make the deferred a promise
84 promise.promise( deferred );
85
86 // Call given func if any
87 if ( func ) {
88 func.call( deferred, deferred );
89 }
90
91 // All done!
92 return deferred;
93 },
94
95 // Deferred helper
96 when: function( subordinate /* , ..., subordinateN */ ) {
97 var i = 0,
98 resolveValues = slice.call( arguments ),
99 length = resolveValues.length,
100
101 // the count of uncompleted subordinates
102 remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
103
104 // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
105 deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
106
107 // Update function for both resolve and progress values
108 updateFunc = function( i, contexts, values ) {
109 return function( value ) {
110 contexts[ i ] = this;
111 values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
112 if ( values === progressValues ) {
113 deferred.notifyWith( contexts, values );
114 } else if ( !( --remaining ) ) {
115 deferred.resolveWith( contexts, values );
116 }
117 };
118 },
119
120 progressValues, progressContexts, resolveContexts;
121
122 // Add listeners to Deferred subordinates; treat others as resolved
123 if ( length > 1 ) {
124 progressValues = new Array( length );
125 progressContexts = new Array( length );
126 resolveContexts = new Array( length );
127 for ( ; i < length; i++ ) {
128 if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
129 resolveValues[ i ].promise()
130 .done( updateFunc( i, resolveContexts, resolveValues ) )
131 .fail( deferred.reject )
132 .progress( updateFunc( i, progressContexts, progressValues ) );
133 } else {
134 --remaining;
135 }
136 }
137 }
138
139 // If we're not waiting on anything, resolve the master
140 if ( !remaining ) {
141 deferred.resolveWith( resolveContexts, resolveValues );
142 }
143
144 return deferred.promise();
145 }
146 });
147
148 return jQuery;
149 });