2017-05-13 13:25:33 +02:00
|
|
|
define( [
|
2014-05-12 20:08:19 +02:00
|
|
|
"./core",
|
2019-03-29 22:00:08 +01:00
|
|
|
"./var/isFunction",
|
2014-05-12 20:08:19 +02:00
|
|
|
"./var/slice",
|
|
|
|
"./callbacks"
|
2019-03-29 22:00:08 +01:00
|
|
|
], function( jQuery, isFunction, slice ) {
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function Identity( v ) {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
function Thrower( ex ) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
function adoptValue( value, resolve, reject, noValue ) {
|
|
|
|
var method;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Check for promise aspect first to privilege synchronous behavior
|
2019-03-29 22:00:08 +01:00
|
|
|
if ( value && isFunction( ( method = value.promise ) ) ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
method.call( value ).done( resolve ).fail( reject );
|
|
|
|
|
|
|
|
// Other thenables
|
2019-03-29 22:00:08 +01:00
|
|
|
} else if ( value && isFunction( ( method = value.then ) ) ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
method.call( value, resolve, reject );
|
|
|
|
|
|
|
|
// Other non-thenables
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Control `resolve` arguments by letting Array#slice cast boolean `noValue` to integer:
|
|
|
|
// * false: [ value ].slice( 0 ) => resolve( value )
|
|
|
|
// * true: [ value ].slice( 1 ) => resolve()
|
|
|
|
resolve.apply( undefined, [ value ].slice( noValue ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// For Promises/A+, convert exceptions into rejections
|
|
|
|
// Since jQuery.when doesn't unwrap thenables, we can skip the extra checks appearing in
|
|
|
|
// Deferred#then to conditionally suppress rejection.
|
|
|
|
} catch ( value ) {
|
|
|
|
|
|
|
|
// Support: Android 4.0 only
|
|
|
|
// Strict mode functions invoked without .call/.apply get global-object context
|
|
|
|
reject.apply( undefined, [ value ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jQuery.extend( {
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
Deferred: function( func ) {
|
|
|
|
var tuples = [
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// action, add listener, callbacks,
|
|
|
|
// ... .then handlers, argument index, [final state]
|
|
|
|
[ "notify", "progress", jQuery.Callbacks( "memory" ),
|
|
|
|
jQuery.Callbacks( "memory" ), 2 ],
|
|
|
|
[ "resolve", "done", jQuery.Callbacks( "once memory" ),
|
|
|
|
jQuery.Callbacks( "once memory" ), 0, "resolved" ],
|
|
|
|
[ "reject", "fail", jQuery.Callbacks( "once memory" ),
|
|
|
|
jQuery.Callbacks( "once memory" ), 1, "rejected" ]
|
2014-05-12 20:08:19 +02:00
|
|
|
],
|
|
|
|
state = "pending",
|
|
|
|
promise = {
|
|
|
|
state: function() {
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
always: function() {
|
|
|
|
deferred.done( arguments ).fail( arguments );
|
|
|
|
return this;
|
|
|
|
},
|
2017-05-13 13:25:33 +02:00
|
|
|
"catch": function( fn ) {
|
|
|
|
return promise.then( null, fn );
|
|
|
|
},
|
|
|
|
|
|
|
|
// Keep pipe for back-compat
|
|
|
|
pipe: function( /* fnDone, fnFail, fnProgress */ ) {
|
2014-05-12 20:08:19 +02:00
|
|
|
var fns = arguments;
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
return jQuery.Deferred( function( newDefer ) {
|
2014-05-12 20:08:19 +02:00
|
|
|
jQuery.each( tuples, function( i, tuple ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// Map tuples (progress, done, fail) to arguments (done, fail, progress)
|
2019-03-29 22:00:08 +01:00
|
|
|
var fn = isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ];
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// deferred.progress(function() { bind to newDefer or newDefer.notify })
|
|
|
|
// deferred.done(function() { bind to newDefer or newDefer.resolve })
|
|
|
|
// deferred.fail(function() { bind to newDefer or newDefer.reject })
|
|
|
|
deferred[ tuple[ 1 ] ]( function() {
|
2014-05-12 20:08:19 +02:00
|
|
|
var returned = fn && fn.apply( this, arguments );
|
2019-03-29 22:00:08 +01:00
|
|
|
if ( returned && isFunction( returned.promise ) ) {
|
2014-05-12 20:08:19 +02:00
|
|
|
returned.promise()
|
2017-05-13 13:25:33 +02:00
|
|
|
.progress( newDefer.notify )
|
2014-05-12 20:08:19 +02:00
|
|
|
.done( newDefer.resolve )
|
2017-05-13 13:25:33 +02:00
|
|
|
.fail( newDefer.reject );
|
2014-05-12 20:08:19 +02:00
|
|
|
} else {
|
2017-05-13 13:25:33 +02:00
|
|
|
newDefer[ tuple[ 0 ] + "With" ](
|
|
|
|
this,
|
|
|
|
fn ? [ returned ] : arguments
|
|
|
|
);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2017-05-13 13:25:33 +02:00
|
|
|
} );
|
|
|
|
} );
|
2014-05-12 20:08:19 +02:00
|
|
|
fns = null;
|
2017-05-13 13:25:33 +02:00
|
|
|
} ).promise();
|
|
|
|
},
|
|
|
|
then: function( onFulfilled, onRejected, onProgress ) {
|
|
|
|
var maxDepth = 0;
|
|
|
|
function resolve( depth, deferred, handler, special ) {
|
|
|
|
return function() {
|
|
|
|
var that = this,
|
|
|
|
args = arguments,
|
|
|
|
mightThrow = function() {
|
|
|
|
var returned, then;
|
|
|
|
|
|
|
|
// Support: Promises/A+ section 2.3.3.3.3
|
|
|
|
// https://promisesaplus.com/#point-59
|
|
|
|
// Ignore double-resolution attempts
|
|
|
|
if ( depth < maxDepth ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
returned = handler.apply( that, args );
|
|
|
|
|
|
|
|
// Support: Promises/A+ section 2.3.1
|
|
|
|
// https://promisesaplus.com/#point-48
|
|
|
|
if ( returned === deferred.promise() ) {
|
|
|
|
throw new TypeError( "Thenable self-resolution" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support: Promises/A+ sections 2.3.3.1, 3.5
|
|
|
|
// https://promisesaplus.com/#point-54
|
|
|
|
// https://promisesaplus.com/#point-75
|
|
|
|
// Retrieve `then` only once
|
|
|
|
then = returned &&
|
|
|
|
|
|
|
|
// Support: Promises/A+ section 2.3.4
|
|
|
|
// https://promisesaplus.com/#point-64
|
|
|
|
// Only check objects and functions for thenability
|
|
|
|
( typeof returned === "object" ||
|
|
|
|
typeof returned === "function" ) &&
|
|
|
|
returned.then;
|
|
|
|
|
|
|
|
// Handle a returned thenable
|
2019-03-29 22:00:08 +01:00
|
|
|
if ( isFunction( then ) ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// Special processors (notify) just wait for resolution
|
|
|
|
if ( special ) {
|
|
|
|
then.call(
|
|
|
|
returned,
|
|
|
|
resolve( maxDepth, deferred, Identity, special ),
|
|
|
|
resolve( maxDepth, deferred, Thrower, special )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Normal processors (resolve) also hook into progress
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// ...and disregard older resolution values
|
|
|
|
maxDepth++;
|
|
|
|
|
|
|
|
then.call(
|
|
|
|
returned,
|
|
|
|
resolve( maxDepth, deferred, Identity, special ),
|
|
|
|
resolve( maxDepth, deferred, Thrower, special ),
|
|
|
|
resolve( maxDepth, deferred, Identity,
|
|
|
|
deferred.notifyWith )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all other returned values
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Only substitute handlers pass on context
|
|
|
|
// and multiple values (non-spec behavior)
|
|
|
|
if ( handler !== Identity ) {
|
|
|
|
that = undefined;
|
|
|
|
args = [ returned ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the value(s)
|
|
|
|
// Default process is resolve
|
|
|
|
( special || deferred.resolveWith )( that, args );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Only normal processors (resolve) catch and reject exceptions
|
|
|
|
process = special ?
|
|
|
|
mightThrow :
|
|
|
|
function() {
|
|
|
|
try {
|
|
|
|
mightThrow();
|
|
|
|
} catch ( e ) {
|
|
|
|
|
|
|
|
if ( jQuery.Deferred.exceptionHook ) {
|
|
|
|
jQuery.Deferred.exceptionHook( e,
|
|
|
|
process.stackTrace );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support: Promises/A+ section 2.3.3.3.4.1
|
|
|
|
// https://promisesaplus.com/#point-61
|
|
|
|
// Ignore post-resolution exceptions
|
|
|
|
if ( depth + 1 >= maxDepth ) {
|
|
|
|
|
|
|
|
// Only substitute handlers pass on context
|
|
|
|
// and multiple values (non-spec behavior)
|
|
|
|
if ( handler !== Thrower ) {
|
|
|
|
that = undefined;
|
|
|
|
args = [ e ];
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.rejectWith( that, args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Support: Promises/A+ section 2.3.3.3.1
|
|
|
|
// https://promisesaplus.com/#point-57
|
|
|
|
// Re-resolve promises immediately to dodge false rejection from
|
|
|
|
// subsequent errors
|
|
|
|
if ( depth ) {
|
|
|
|
process();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Call an optional hook to record the stack, in case of exception
|
|
|
|
// since it's otherwise lost when execution goes async
|
|
|
|
if ( jQuery.Deferred.getStackHook ) {
|
|
|
|
process.stackTrace = jQuery.Deferred.getStackHook();
|
|
|
|
}
|
|
|
|
window.setTimeout( process );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return jQuery.Deferred( function( newDefer ) {
|
|
|
|
|
|
|
|
// progress_handlers.add( ... )
|
|
|
|
tuples[ 0 ][ 3 ].add(
|
|
|
|
resolve(
|
|
|
|
0,
|
|
|
|
newDefer,
|
2019-03-29 22:00:08 +01:00
|
|
|
isFunction( onProgress ) ?
|
2017-05-13 13:25:33 +02:00
|
|
|
onProgress :
|
|
|
|
Identity,
|
|
|
|
newDefer.notifyWith
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// fulfilled_handlers.add( ... )
|
|
|
|
tuples[ 1 ][ 3 ].add(
|
|
|
|
resolve(
|
|
|
|
0,
|
|
|
|
newDefer,
|
2019-03-29 22:00:08 +01:00
|
|
|
isFunction( onFulfilled ) ?
|
2017-05-13 13:25:33 +02:00
|
|
|
onFulfilled :
|
|
|
|
Identity
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// rejected_handlers.add( ... )
|
|
|
|
tuples[ 2 ][ 3 ].add(
|
|
|
|
resolve(
|
|
|
|
0,
|
|
|
|
newDefer,
|
2019-03-29 22:00:08 +01:00
|
|
|
isFunction( onRejected ) ?
|
2017-05-13 13:25:33 +02:00
|
|
|
onRejected :
|
|
|
|
Thrower
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} ).promise();
|
2014-05-12 20:08:19 +02:00
|
|
|
},
|
2017-05-13 13:25:33 +02:00
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
// Get a promise for this deferred
|
|
|
|
// If obj is provided, the promise aspect is added to the object
|
|
|
|
promise: function( obj ) {
|
|
|
|
return obj != null ? jQuery.extend( obj, promise ) : promise;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deferred = {};
|
|
|
|
|
|
|
|
// Add list-specific methods
|
|
|
|
jQuery.each( tuples, function( i, tuple ) {
|
|
|
|
var list = tuple[ 2 ],
|
2017-05-13 13:25:33 +02:00
|
|
|
stateString = tuple[ 5 ];
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// promise.progress = list.add
|
|
|
|
// promise.done = list.add
|
|
|
|
// promise.fail = list.add
|
|
|
|
promise[ tuple[ 1 ] ] = list.add;
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
// Handle state
|
|
|
|
if ( stateString ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
list.add(
|
|
|
|
function() {
|
|
|
|
|
|
|
|
// state = "resolved" (i.e., fulfilled)
|
|
|
|
// state = "rejected"
|
|
|
|
state = stateString;
|
|
|
|
},
|
|
|
|
|
|
|
|
// rejected_callbacks.disable
|
|
|
|
// fulfilled_callbacks.disable
|
|
|
|
tuples[ 3 - i ][ 2 ].disable,
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
// rejected_handlers.disable
|
|
|
|
// fulfilled_handlers.disable
|
|
|
|
tuples[ 3 - i ][ 3 ].disable,
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// progress_callbacks.lock
|
2019-03-29 22:00:08 +01:00
|
|
|
tuples[ 0 ][ 2 ].lock,
|
|
|
|
|
|
|
|
// progress_handlers.lock
|
|
|
|
tuples[ 0 ][ 3 ].lock
|
2017-05-13 13:25:33 +02:00
|
|
|
);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// progress_handlers.fire
|
|
|
|
// fulfilled_handlers.fire
|
|
|
|
// rejected_handlers.fire
|
|
|
|
list.add( tuple[ 3 ].fire );
|
|
|
|
|
|
|
|
// deferred.notify = function() { deferred.notifyWith(...) }
|
|
|
|
// deferred.resolve = function() { deferred.resolveWith(...) }
|
|
|
|
// deferred.reject = function() { deferred.rejectWith(...) }
|
|
|
|
deferred[ tuple[ 0 ] ] = function() {
|
|
|
|
deferred[ tuple[ 0 ] + "With" ]( this === deferred ? undefined : this, arguments );
|
2014-05-12 20:08:19 +02:00
|
|
|
return this;
|
|
|
|
};
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// deferred.notifyWith = list.fireWith
|
|
|
|
// deferred.resolveWith = list.fireWith
|
|
|
|
// deferred.rejectWith = list.fireWith
|
|
|
|
deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
|
|
|
|
} );
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
// Make the deferred a promise
|
|
|
|
promise.promise( deferred );
|
|
|
|
|
|
|
|
// Call given func if any
|
|
|
|
if ( func ) {
|
|
|
|
func.call( deferred, deferred );
|
|
|
|
}
|
|
|
|
|
|
|
|
// All done!
|
|
|
|
return deferred;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deferred helper
|
2017-05-13 13:25:33 +02:00
|
|
|
when: function( singleValue ) {
|
|
|
|
var
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// count of uncompleted subordinates
|
|
|
|
remaining = arguments.length,
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// count of unprocessed arguments
|
|
|
|
i = remaining,
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// subordinate fulfillment data
|
|
|
|
resolveContexts = Array( i ),
|
|
|
|
resolveValues = slice.call( arguments ),
|
|
|
|
|
|
|
|
// the master Deferred
|
|
|
|
master = jQuery.Deferred(),
|
|
|
|
|
|
|
|
// subordinate callback factory
|
|
|
|
updateFunc = function( i ) {
|
2014-05-12 20:08:19 +02:00
|
|
|
return function( value ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
resolveContexts[ i ] = this;
|
|
|
|
resolveValues[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
|
|
|
|
if ( !( --remaining ) ) {
|
|
|
|
master.resolveWith( resolveContexts, resolveValues );
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
};
|
2017-05-13 13:25:33 +02:00
|
|
|
};
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// Single- and empty arguments are adopted like Promise.resolve
|
|
|
|
if ( remaining <= 1 ) {
|
|
|
|
adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject,
|
|
|
|
!remaining );
|
|
|
|
|
|
|
|
// Use .then() to unwrap secondary thenables (cf. gh-3000)
|
|
|
|
if ( master.state() === "pending" ||
|
2019-03-29 22:00:08 +01:00
|
|
|
isFunction( resolveValues[ i ] && resolveValues[ i ].then ) ) {
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
return master.then();
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// Multiple arguments are aggregated like Promise.all array elements
|
|
|
|
while ( i-- ) {
|
|
|
|
adoptValue( resolveValues[ i ], updateFunc( i ), master.reject );
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
return master.promise();
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2017-05-13 13:25:33 +02:00
|
|
|
} );
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
return jQuery;
|
2017-05-13 13:25:33 +02:00
|
|
|
} );
|