aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOleg Gaidarenko <markelog@gmail.com>2015-12-22 17:07:04 +0300
committerOleg Gaidarenko <markelog@gmail.com>2015-12-22 17:07:04 +0300
commit5826cafb95624321882ecfbe87073e507b77431d (patch)
treef5888ea1876e9f77408d16c7b754828b1a6e5898 /src
parent3b573a59d9acc5478e0817d3e0407b29c1fc6fa5 (diff)
downloadjquery-5826cafb95624321882ecfbe87073e507b77431d.tar.gz
jquery-5826cafb95624321882ecfbe87073e507b77431d.zip
Revert "Deferred: Backwards-compatible standards interoperability"
This reverts commit 34f25631795e0641cc706012cfe5a88dacdedfec.
Diffstat (limited to 'src')
-rw-r--r--src/deferred.js281
1 files changed, 38 insertions, 243 deletions
diff --git a/src/deferred.js b/src/deferred.js
index c4d61446b..51a40c7cb 100644
--- a/src/deferred.js
+++ b/src/deferred.js
@@ -4,26 +4,14 @@ define( [
"./callbacks"
], function( jQuery, slice ) {
-function Identity( v ) {
- return v;
-}
-function Thrower( ex ) {
- throw ex;
-}
-
-jQuery.extend( {
+jQuery.extend({
Deferred: function( func ) {
var tuples = [
-
- // 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" ]
+ // action, add listener, listener list, final state
+ [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
+ [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
+ [ "notify", "progress", jQuery.Callbacks("memory") ]
],
state = "pending",
promise = {
@@ -34,21 +22,13 @@ jQuery.extend( {
deferred.done( arguments ).fail( arguments );
return this;
},
-
- // Keep pipe for back-compat
- pipe: function( /* fnDone, fnFail, fnProgress */ ) {
+ then: function( /* fnDone, fnFail, fnProgress */ ) {
var fns = arguments;
-
- return jQuery.Deferred( function( newDefer ) {
+ return jQuery.Deferred(function( newDefer ) {
jQuery.each( tuples, function( i, tuple ) {
-
- // Map tuples (progress, done, fail) to arguments (done, fail, progress)
- var fn = jQuery.isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ];
-
- // 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() {
+ var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
+ // deferred[ done | fail | progress ] for forwarding actions to newDefer
+ deferred[ tuple[1] ](function() {
var returned = fn && fn.apply( this, arguments );
if ( returned && jQuery.isFunction( returned.promise ) ) {
returned.promise()
@@ -66,162 +46,6 @@ jQuery.extend( {
fns = null;
} ).promise();
},
- then: function( onFulfilled, onRejected, onProgress ) {
- var maxDepth = 0;
- function resolve( depth, deferred, handler, special ) {
- return function() {
- var that = this === promise ? undefined : 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
- if ( jQuery.isFunction( then ) ) {
-
- // 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.notify )
- );
- }
-
- // Handle all other returned values
- } else {
-
- // Only substitue 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 || deferred.promise(), args );
- }
- },
-
- // Only normal processors (resolve) catch and reject exceptions
- process = special ?
- mightThrow :
- function() {
- try {
- mightThrow();
- } catch ( e ) {
-
- // Support: Promises/A+ section 2.3.3.3.4.1
- // https://promisesaplus.com/#point-61
- // Ignore post-resolution exceptions
- if ( depth + 1 >= maxDepth ) {
-
- // Only substitue handlers pass on context
- // and multiple values (non-spec behavior)
- if ( handler !== Thrower ) {
- that = undefined;
- args = [ e ];
- }
-
- deferred.rejectWith( that || deferred.promise(),
- 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 {
- window.setTimeout( process );
- }
- };
- }
-
- return jQuery.Deferred( function( newDefer ) {
-
- // progress_handlers.add( ... )
- tuples[ 0 ][ 3 ].add(
- resolve(
- 0,
- newDefer,
- jQuery.isFunction( onProgress ) ?
- onProgress :
- Identity,
- newDefer.notifyWith
- )
- );
-
- // fulfilled_handlers.add( ... )
- tuples[ 1 ][ 3 ].add(
- resolve(
- 0,
- newDefer,
- jQuery.isFunction( onFulfilled ) ?
- onFulfilled :
- Identity
- )
- );
-
- // rejected_handlers.add( ... )
- tuples[ 2 ][ 3 ].add(
- resolve(
- 0,
- newDefer,
- jQuery.isFunction( onRejected ) ?
- onRejected :
- Thrower
- )
- );
- } ).promise();
- },
-
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function( obj ) {
@@ -230,53 +54,34 @@ jQuery.extend( {
},
deferred = {};
+ // Keep pipe for back-compat
+ promise.pipe = promise.then;
+
// Add list-specific methods
jQuery.each( tuples, function( i, tuple ) {
var list = tuple[ 2 ],
- stateString = tuple[ 5 ];
+ stateString = tuple[ 3 ];
- // promise.progress = list.add
- // promise.done = list.add
- // promise.fail = list.add
- promise[ tuple[ 1 ] ] = list.add;
+ // promise[ done | fail | progress ] = list.add
+ promise[ tuple[1] ] = list.add;
// Handle state
if ( stateString ) {
- list.add(
- function() {
-
- // state = "resolved" (i.e., fulfilled)
- // state = "rejected"
- state = stateString;
- },
+ list.add(function() {
+ // state = [ resolved | rejected ]
+ state = stateString;
- // rejected_callbacks.disable
- // fulfilled_callbacks.disable
- tuples[ 3 - i ][ 2 ].disable,
-
- // progress_callbacks.lock
- tuples[ 0 ][ 2 ].lock
- );
+ // [ reject_list | resolve_list ].disable; progress_list.lock
+ }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
}
- // 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 ? promise : this, arguments );
+ // deferred[ resolve | reject | notify ]
+ deferred[ tuple[0] ] = function() {
+ deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
return this;
};
-
- // deferred.notifyWith = list.fireWith
- // deferred.resolveWith = list.fireWith
- // deferred.rejectWith = list.fireWith
- deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
- } );
+ deferred[ tuple[0] + "With" ] = list.fireWith;
+ });
// Make the deferred a promise
promise.promise( deferred );
@@ -292,8 +97,7 @@ jQuery.extend( {
// Deferred helper
when: function( subordinate /* , ..., subordinateN */ ) {
- var method,
- i = 0,
+ var i = 0,
resolveValues = slice.call( arguments ),
length = resolveValues.length,
@@ -303,7 +107,7 @@ jQuery.extend( {
// the master Deferred.
// If resolveValues consist of only a single Deferred, just use that.
- master = remaining === 1 ? subordinate : jQuery.Deferred(),
+ deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
// Update function for both resolve and progress values
updateFunc = function( i, contexts, values ) {
@@ -311,12 +115,14 @@ jQuery.extend( {
contexts[ i ] = this;
values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
if ( values === progressValues ) {
- master.notifyWith( contexts, values );
- } else if ( !( --remaining ) ) {
- master.resolveWith( contexts, values );
+ deferred.notifyWith( contexts, values );
+
+ } else if ( !(--remaining) ) {
+ deferred.resolveWith( contexts, values );
}
};
},
+
progressValues, progressContexts, resolveContexts;
// add listeners to Deferred subordinates; treat others as resolved
@@ -325,22 +131,11 @@ jQuery.extend( {
progressContexts = new Array( length );
resolveContexts = new Array( length );
for ( ; i < length; i++ ) {
- if ( resolveValues[ i ] &&
- jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) {
-
- method.call( resolveValues[ i ] )
+ if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
+ resolveValues[ i ].promise()
.progress( updateFunc( i, progressContexts, progressValues ) )
.done( updateFunc( i, resolveContexts, resolveValues ) )
- .fail( master.reject );
- } else if ( resolveValues[ i ] &&
- jQuery.isFunction( ( method = resolveValues[ i ].then ) ) ) {
-
- method.call(
- resolveValues[ i ],
- updateFunc( i, resolveContexts, resolveValues ),
- master.reject,
- updateFunc( i, progressContexts, progressValues )
- );
+ .fail( deferred.reject );
} else {
--remaining;
}
@@ -349,10 +144,10 @@ jQuery.extend( {
// if we're not waiting on anything, resolve the master
if ( !remaining ) {
- master.resolveWith( resolveContexts, resolveValues );
+ deferred.resolveWith( resolveContexts, resolveValues );
}
- return master.promise();
+ return deferred.promise();
}
} );