Revert "Deferred: Backwards-compatible standards interoperability"

This reverts commit 34f2563179.
This commit is contained in:
Oleg Gaidarenko 2015-12-22 17:07:04 +03:00
parent 3b573a59d9
commit 5826cafb95
6 changed files with 320 additions and 917 deletions

View File

@ -80,8 +80,6 @@ module.exports = function( grunt ) {
"sizzle/dist": "sizzle/dist", "sizzle/dist": "sizzle/dist",
"sizzle/LICENSE.txt": "sizzle/LICENSE.txt", "sizzle/LICENSE.txt": "sizzle/LICENSE.txt",
"npo/npo.js": "native-promise-only/npo.js",
"qunit/qunit.js": "qunitjs/qunit/qunit.js", "qunit/qunit.js": "qunitjs/qunit/qunit.js",
"qunit/qunit.css": "qunitjs/qunit/qunit.css", "qunit/qunit.css": "qunitjs/qunit/qunit.css",
"qunit/LICENSE.txt": "qunitjs/LICENSE.txt", "qunit/LICENSE.txt": "qunitjs/LICENSE.txt",
@ -205,9 +203,7 @@ module.exports = function( grunt ) {
// jQuery on Windows. (see gh-2519) // jQuery on Windows. (see gh-2519)
grunt.registerTask( "test_fast", runJsdomTests ? [ "node_smoke_tests" ] : [] ); grunt.registerTask( "test_fast", runJsdomTests ? [ "node_smoke_tests" ] : [] );
grunt.registerTask( "test", [ "test_fast" ].concat( grunt.registerTask( "test", [ "test_fast" ] );
runJsdomTests ? [ "promises_aplus_tests" ] : []
) );
// Short list as a high frequency watch task // Short list as a high frequency watch task
grunt.registerTask( "dev", [ "build:*:*", "lint", "uglify", "remove_map_comment", "dist:*" ] ); grunt.registerTask( "dev", [ "build:*:*", "lint", "uglify", "remove_map_comment", "dist:*" ] );

View File

@ -1,5 +1,5 @@
/** vim: et:ts=4:sw=4:sts=4 /** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 2.1.15 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. * @license RequireJS 2.1.14 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
@ -12,7 +12,7 @@ var requirejs, require, define;
(function (global) { (function (global) {
var req, s, head, baseElement, dataMain, src, var req, s, head, baseElement, dataMain, src,
interactiveScript, currentlyAddingScript, mainScript, subPath, interactiveScript, currentlyAddingScript, mainScript, subPath,
version = '2.1.15', version = '2.1.14',
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g, cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
jsSuffixRegExp = /\.js$/, jsSuffixRegExp = /\.js$/,

View File

@ -41,9 +41,7 @@
"gzip-js": "0.3.2", "gzip-js": "0.3.2",
"jsdom": "5.6.1", "jsdom": "5.6.1",
"load-grunt-tasks": "1.0.0", "load-grunt-tasks": "1.0.0",
"native-promise-only": "0.7.8-a", "npm": "2.1.12",
"promises-aplus-tests": "2.1.0",
"q": "1.1.2",
"qunitjs": "1.17.1", "qunitjs": "1.17.1",
"qunit-assert-step": "1.0.3", "qunit-assert-step": "1.0.3",
"requirejs": "2.1.17", "requirejs": "2.1.17",

View File

@ -4,26 +4,14 @@ define( [
"./callbacks" "./callbacks"
], function( jQuery, slice ) { ], function( jQuery, slice ) {
function Identity( v ) { jQuery.extend({
return v;
}
function Thrower( ex ) {
throw ex;
}
jQuery.extend( {
Deferred: function( func ) { Deferred: function( func ) {
var tuples = [ var tuples = [
// action, add listener, listener list, final state
// action, add listener, callbacks, [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
// ... .then handlers, argument index, [final state] [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
[ "notify", "progress", jQuery.Callbacks( "memory" ), [ "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" ]
], ],
state = "pending", state = "pending",
promise = { promise = {
@ -34,21 +22,13 @@ jQuery.extend( {
deferred.done( arguments ).fail( arguments ); deferred.done( arguments ).fail( arguments );
return this; return this;
}, },
then: function( /* fnDone, fnFail, fnProgress */ ) {
// Keep pipe for back-compat
pipe: function( /* fnDone, fnFail, fnProgress */ ) {
var fns = arguments; var fns = arguments;
return jQuery.Deferred(function( newDefer ) {
return jQuery.Deferred( function( newDefer ) {
jQuery.each( tuples, function( i, tuple ) { jQuery.each( tuples, function( i, tuple ) {
var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
// Map tuples (progress, done, fail) to arguments (done, fail, progress) // deferred[ done | fail | progress ] for forwarding actions to newDefer
var fn = jQuery.isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ]; deferred[ tuple[1] ](function() {
// 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 returned = fn && fn.apply( this, arguments ); var returned = fn && fn.apply( this, arguments );
if ( returned && jQuery.isFunction( returned.promise ) ) { if ( returned && jQuery.isFunction( returned.promise ) ) {
returned.promise() returned.promise()
@ -66,162 +46,6 @@ jQuery.extend( {
fns = null; fns = null;
} ).promise(); } ).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 // Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object // If obj is provided, the promise aspect is added to the object
promise: function( obj ) { promise: function( obj ) {
@ -230,53 +54,34 @@ jQuery.extend( {
}, },
deferred = {}; deferred = {};
// Keep pipe for back-compat
promise.pipe = promise.then;
// Add list-specific methods // Add list-specific methods
jQuery.each( tuples, function( i, tuple ) { jQuery.each( tuples, function( i, tuple ) {
var list = tuple[ 2 ], var list = tuple[ 2 ],
stateString = tuple[ 5 ]; stateString = tuple[ 3 ];
// promise.progress = list.add // promise[ done | fail | progress ] = list.add
// promise.done = list.add promise[ tuple[1] ] = list.add;
// promise.fail = list.add
promise[ tuple[ 1 ] ] = list.add;
// Handle state // Handle state
if ( stateString ) { if ( stateString ) {
list.add( list.add(function() {
function() { // state = [ resolved | rejected ]
state = stateString;
// state = "resolved" (i.e., fulfilled) // [ reject_list | resolve_list ].disable; progress_list.lock
// state = "rejected" }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
state = stateString;
},
// rejected_callbacks.disable
// fulfilled_callbacks.disable
tuples[ 3 - i ][ 2 ].disable,
// progress_callbacks.lock
tuples[ 0 ][ 2 ].lock
);
} }
// progress_handlers.fire // deferred[ resolve | reject | notify ]
// fulfilled_handlers.fire deferred[ tuple[0] ] = function() {
// rejected_handlers.fire deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
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 );
return this; return this;
}; };
deferred[ tuple[0] + "With" ] = list.fireWith;
// deferred.notifyWith = list.fireWith });
// deferred.resolveWith = list.fireWith
// deferred.rejectWith = list.fireWith
deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
} );
// Make the deferred a promise // Make the deferred a promise
promise.promise( deferred ); promise.promise( deferred );
@ -292,8 +97,7 @@ jQuery.extend( {
// Deferred helper // Deferred helper
when: function( subordinate /* , ..., subordinateN */ ) { when: function( subordinate /* , ..., subordinateN */ ) {
var method, var i = 0,
i = 0,
resolveValues = slice.call( arguments ), resolveValues = slice.call( arguments ),
length = resolveValues.length, length = resolveValues.length,
@ -303,7 +107,7 @@ jQuery.extend( {
// the master Deferred. // the master Deferred.
// If resolveValues consist of only a single Deferred, just use that. // 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 // Update function for both resolve and progress values
updateFunc = function( i, contexts, values ) { updateFunc = function( i, contexts, values ) {
@ -311,12 +115,14 @@ jQuery.extend( {
contexts[ i ] = this; contexts[ i ] = this;
values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value; values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
if ( values === progressValues ) { if ( values === progressValues ) {
master.notifyWith( contexts, values ); deferred.notifyWith( contexts, values );
} else if ( !( --remaining ) ) {
master.resolveWith( contexts, values ); } else if ( !(--remaining) ) {
deferred.resolveWith( contexts, values );
} }
}; };
}, },
progressValues, progressContexts, resolveContexts; progressValues, progressContexts, resolveContexts;
// add listeners to Deferred subordinates; treat others as resolved // add listeners to Deferred subordinates; treat others as resolved
@ -325,22 +131,11 @@ jQuery.extend( {
progressContexts = new Array( length ); progressContexts = new Array( length );
resolveContexts = new Array( length ); resolveContexts = new Array( length );
for ( ; i < length; i++ ) { for ( ; i < length; i++ ) {
if ( resolveValues[ i ] && if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) { resolveValues[ i ].promise()
method.call( resolveValues[ i ] )
.progress( updateFunc( i, progressContexts, progressValues ) ) .progress( updateFunc( i, progressContexts, progressValues ) )
.done( updateFunc( i, resolveContexts, resolveValues ) ) .done( updateFunc( i, resolveContexts, resolveValues ) )
.fail( master.reject ); .fail( deferred.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 )
);
} else { } else {
--remaining; --remaining;
} }
@ -349,10 +144,10 @@ jQuery.extend( {
// if we're not waiting on anything, resolve the master // if we're not waiting on anything, resolve the master
if ( !remaining ) { if ( !remaining ) {
master.resolveWith( resolveContexts, resolveValues ); deferred.resolveWith( resolveContexts, resolveValues );
} }
return master.promise(); return deferred.promise();
} }
} ); } );

View File

@ -16,7 +16,6 @@
<script src="../external/qunit-assert-step/qunit-assert-step.js"></script> <script src="../external/qunit-assert-step/qunit-assert-step.js"></script>
<script src="../external/sinon/sinon-1.14.1.js"></script> <script src="../external/sinon/sinon-1.14.1.js"></script>
<script src="../external/sinon/timers_ie.js"></script> <script src="../external/sinon/timers_ie.js"></script>
<script src="../external/npo/npo.js"></script>
<script src="../external/requirejs/require.js"></script> <script src="../external/requirejs/require.js"></script>
<!-- See testinit for the list of tests --> <!-- See testinit for the list of tests -->
<script src="data/testinit.js"></script> <script src="data/testinit.js"></script>

File diff suppressed because it is too large Load Diff