aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjaubourg <j@ubourg.net>2011-02-23 08:00:27 +0100
committerjaubourg <j@ubourg.net>2011-02-23 08:00:27 +0100
commit3663836b0180f3a73d96ec234d5d7010e5652a4b (patch)
tree60edc2d240d792d14280ebfc4454e6bdd147e4ed
parentcacea6f7e778d42cda56066a6b1da8fb163410cc (diff)
downloadjquery-3663836b0180f3a73d96ec234d5d7010e5652a4b.tar.gz
jquery-3663836b0180f3a73d96ec234d5d7010e5652a4b.zip
Refactors jQuery.when to avoid unnecessary recursion and limit function calls as much as possible.
-rw-r--r--src/core.js46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/core.js b/src/core.js
index 87b24c80a..d77c818e6 100644
--- a/src/core.js
+++ b/src/core.js
@@ -905,13 +905,13 @@ jQuery.extend({
}
var i = promiseMethods.length;
while( i-- ) {
- obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
+ obj[ promiseMethods[i] ] = deferred[ promiseMethods[i] ];
}
return obj;
}
} );
// Make sure only one callback list will be used
- deferred.then( failDeferred.cancel, deferred.cancel );
+ deferred.done( failDeferred.cancel ).fail( deferred.cancel );
// Unexpose cancel
delete deferred.cancel;
// Call given func if any
@@ -923,24 +923,34 @@ jQuery.extend({
// Deferred helper
when: function( object ) {
- var args = arguments,
- length = args.length,
- deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
+ var lastIndex = arguments.length,
+ deferred = lastIndex <= 1 && object && jQuery.isFunction( object.promise ) ?
object :
jQuery.Deferred(),
- promise = deferred.promise(),
- resolveArray;
-
- if ( length > 1 ) {
- resolveArray = new Array( length );
- jQuery.each( args, function( index, element ) {
- jQuery.when( element ).then( function( value ) {
- resolveArray[ index ] = arguments.length > 1 ? slice.call( arguments, 0 ) : value;
- if( ! --length ) {
- deferred.resolveWith( promise, resolveArray );
- }
- }, deferred.reject );
- } );
+ promise = deferred.promise();
+
+ if ( lastIndex > 1 ) {
+ var array = slice.call( arguments, 0 ),
+ count = lastIndex,
+ iCallback = function( index ) {
+ return function( value ) {
+ array[ index ] = arguments.length > 1 ? slice.call( arguments, 0 ) : value;
+ if ( !( --count ) ) {
+ deferred.resolveWith( promise, array );
+ }
+ };
+ };
+ while( ( lastIndex-- ) ) {
+ object = array[ lastIndex ];
+ if ( object && jQuery.isFunction( object.promise ) ) {
+ object.promise().then( iCallback(lastIndex), deferred.reject );
+ } else {
+ --count;
+ }
+ }
+ if ( !count ) {
+ deferred.resolveWith( promise, array );
+ }
} else if ( deferred !== object ) {
deferred.resolve( object );
}