]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/jquery/src/deferred.js
98f9c313f6d7d3bffdf3824a9ebaab086a092673
5 ], function( jQuery
, slice
) {
9 Deferred: function( func
) {
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") ]
22 deferred
.done( arguments
).fail( arguments
);
25 then: function( /* fnDone, fnFail, fnProgress */ ) {
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
) ) {
35 .done( newDefer
.resolve
)
36 .fail( newDefer
.reject
)
37 .progress( newDefer
.notify
);
39 newDefer
[ tuple
[ 0 ] + "With" ]( this === promise
? newDefer
.promise() : this, fn
? [ returned
] : arguments
);
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
;
54 // Keep pipe for back-compat
55 promise
.pipe
= promise
.then
;
57 // Add list-specific methods
58 jQuery
.each( tuples
, function( i
, tuple
) {
59 var list
= tuple
[ 2 ],
60 stateString
= tuple
[ 3 ];
62 // promise[ done | fail | progress ] = list.add
63 promise
[ tuple
[1] ] = list
.add
;
68 // state = [ resolved | rejected ]
71 // [ reject_list | resolve_list ].disable; progress_list.lock
72 }, tuples
[ i
^ 1 ][ 2 ].disable
, tuples
[ 2 ][ 2 ].lock
);
75 // deferred[ resolve | reject | notify ]
76 deferred
[ tuple
[0] ] = function() {
77 deferred
[ tuple
[0] + "With" ]( this === deferred
? promise : this, arguments
);
80 deferred
[ tuple
[0] + "With" ] = list
.fireWith
;
83 // Make the deferred a promise
84 promise
.promise( deferred
);
86 // Call given func if any
88 func
.call( deferred
, deferred
);
96 when: function( subordinate
/* , ..., subordinateN */ ) {
98 resolveValues
= slice
.call( arguments
),
99 length
= resolveValues
.length
,
101 // the count of uncompleted subordinates
102 remaining
= length
!== 1 || ( subordinate
&& jQuery
.isFunction( subordinate
.promise
) ) ? length : 0,
104 // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
105 deferred
= remaining
=== 1 ? subordinate : jQuery
.Deferred(),
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
);
120 progressValues
, progressContexts
, resolveContexts
;
122 // Add listeners to Deferred subordinates; treat others as resolved
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
) );
139 // If we're not waiting on anything, resolve the master
141 deferred
.resolveWith( resolveContexts
, resolveValues
);
144 return deferred
.promise();