Skip to content
Permalink
Browse files

Trims down deferred.js. The gist of it is from the magnificent @gibso…

…n042, I just added some minor touches.
  • Loading branch information
jaubourg committed Apr 25, 2012
1 parent 87c83b0 commit 6e066a4db72ff6b0d12dd8a43faec0a80e4a1fed
Showing with 86 additions and 89 deletions.
  1. +86 −89 src/deferred.js
@@ -6,90 +6,82 @@ var // Static reference to slice
jQuery.extend({

Deferred: function( func ) {
var doneList = jQuery.Callbacks( "once memory" ),
failList = jQuery.Callbacks( "once memory" ),
progressList = jQuery.Callbacks( "memory" ),
var tuples = [
// action, add listener, listener list
[ "resolve", "done", jQuery.Callbacks("once memory") ],
[ "reject", "fail", jQuery.Callbacks("once memory") ],
[ "notify", "progress", jQuery.Callbacks("memory") ]
],
state = "pending",
lists = {
resolve: doneList,
reject: failList,
notify: progressList
},
promise = {
done: doneList.add,
fail: failList.add,
progress: progressList.add,

state: function() {
return state;
},

// Deprecated
isResolved: doneList.fired,
isRejected: failList.fired,

always: function() {
deferred.done.apply( deferred, arguments ).fail.apply( deferred, arguments );
deferred.done( arguments ).fail( arguments );

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 25, 2012

Member

I LOVE this change.

This comment has been minimized.

Copy link
@rwaldron

rwaldron Apr 25, 2012

Member

+1 to that sentiment

This comment has been minimized.

Copy link
@jaubourg

jaubourg Apr 25, 2012

Author Member

Yeah, that's the big advantage of using jQuery.each in Callbacks.add!

return this;
},
then: function( fnDone, fnFail, fnProgress ) {
then: function( /* fnDone, fnFail, fnProgress */ ) {
var fns = arguments;
return jQuery.Deferred(function( newDefer ) {
jQuery.each( {
done: [ fnDone, "resolve" ],
fail: [ fnFail, "reject" ],
progress: [ fnProgress, "notify" ]
}, function( handler, data ) {
var fn = data[ 0 ],
action = data[ 1 ],
returned;
if ( jQuery.isFunction( fn ) ) {
deferred[ handler ](function() {
returned = fn.apply( this, arguments );
jQuery.each( tuples, function( i, tuple ) {
var action = tuple[ 0 ],
fn = fns[ i ];
// deferred[ done | fail | progress ] for forwarding actions to newDefer
deferred[ tuple[1] ]( jQuery.isFunction( fn ) ?
function() {
var returned = fn.apply( this, arguments );
if ( returned && jQuery.isFunction( returned.promise ) ) {
returned.promise().done( newDefer.resolve ).fail( newDefer.reject ).progress( newDefer.notify );
returned.promise()
.done( newDefer.resolve )
.fail( newDefer.reject )
.progress( newDefer.notify );
} else {
newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] );
}
});
} else {
deferred[ handler ]( newDefer[ action ] );
}
} :
newDefer[ action ]
);
});
fns = null;
}).promise();
},
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function( obj ) {
if ( obj == null ) {
obj = promise;
} else {
for ( var key in promise ) {
obj[ key ] = promise[ key ];
}
}
return obj;
return typeof obj === "object" ? jQuery.extend( obj, promise ) : promise;
}
},
deferred,
key;
deferred = {};

// Keep pipe for back-compat
promise.pipe = promise.then;

// Construct deferred
deferred = promise.promise({});
// Add list-specific methods
jQuery.each( tuples, function( i, tuple ) {
var list = tuple[ 2 ], stateString;

for ( key in lists ) {
deferred[ key ] = lists[ key ].fire;
deferred[ key + "With" ] = lists[ key ].fireWith;
}
// promise[ done | fail | progress ] = list.add
promise[ tuple[1] ] = list.add;

// Handle state
deferred.done( function() {
state = "resolved";
}, failList.disable, progressList.lock ).fail( function() {
state = "rejected";
}, doneList.disable, progressList.lock );
// Handle state
if ( i < 2 ) {
stateString = tuple[ 0 ].replace( /e?$/, "ed" );

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 25, 2012

Member

Ick. It's probably both smaller and clearer to put this string into the tuples like I had in earlier iterations.

This comment has been minimized.

Copy link
@jaubourg

jaubourg Apr 25, 2012

Author Member

When I tried, it was smaller min/gzipped that way. but feel free to test and PR if it changed (I tried to change stuff one at a time, but you know how interdependant everything is when you optimize for gzip).

This comment has been minimized.

Copy link
@staabm

staabm Apr 26, 2012

Contributor

Maybe the RE should only be compiled once?

This comment has been minimized.

Copy link
@jaubourg

jaubourg Apr 26, 2012

Author Member

It's 20 bytes (last time I checked) against this perf hit.

list.add(function() {
state = stateString;
tuples[ i ^ 1 ][ 2 ].disable();
tuples[ 2 ][ 2 ].lock();
});

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 25, 2012

Member

Did you check the effect on gzipped size of doing it this way vs. three arguments (function, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock), or is this a performance optimization?

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 25, 2012

Member

Also, I like the unwrapping here. Great catch.

This comment has been minimized.

Copy link
@jaubourg

jaubourg Apr 25, 2012

Author Member

If you can make it smaller with duplicated strings, be my guess. I was more concerned with the unwrapping than I was with micro-gains here.

promise[ "isR" + stateString.substr( 1 ) ] = list.fired;

This comment has been minimized.

Copy link
@gibson042

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 25, 2012

Member

But needs the "Deprecated" comment from above added back in.

This comment has been minimized.

Copy link
@jaubourg

jaubourg Apr 25, 2012

Author Member

Not home right now but you're right. I even told myself so but completely forgot to add it back.

}

// deferred[ resolve | reject | notify ] = list.fire
deferred[ tuple[0] ] = list.fire;
deferred[ tuple[0] + "With" ] = list.fireWith;
});

// Make the deferred a promise
promise.promise( deferred );

// Call given func if any
if ( func ) {
@@ -101,45 +93,50 @@ jQuery.extend({
},

// Deferred helper
when: function( firstParam ) {
var args = sliceDeferred.call( arguments ),
i = 0,
length = args.length,
pValues = new Array( length ),
count = length,
pCount = length,
deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ?
firstParam :
jQuery.Deferred(),
promise = deferred.promise();
function resolveFunc( i ) {
return function( value ) {
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments ) : value;
if ( !( --count ) ) {
deferred.resolveWith( deferred, args );
}
};
}
function progressFunc( i ) {
return function( value ) {
pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments ) : value;
deferred.notifyWith( promise, pValues );
when: function( subordinate /* , ..., subordinateN */ ) {
var i = 0,
resolveValues = sliceDeferred.call( arguments ),
length = resolveValues.length,
progressValues = new Array( length ),

// the count of uncompleted subordinates
remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,

// the master Deferred. If resolveValues consist of only a single Deferred, just use that.
deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
promise = deferred.promise(),

// Update function for both resolve and progress values
updateFunc = function( i, arr ) {
return function( value ) {
arr[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments ) : value;
if( arr === progressValues ) {
deferred.notifyWith( promise, arr );
} else if ( !( --remaining ) ) {
deferred.resolveWith( promise, arr );
}
};
};
}

// add listeners to Deferred subordinates; treat others as resolved
if ( length > 1 ) {
for ( ; i < length; i++ ) {
if ( args[ i ] && args[ i ].promise && jQuery.isFunction( args[ i ].promise ) ) {
args[ i ].promise().done( resolveFunc(i) ).fail( deferred.reject ).progress( progressFunc(i) );
if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
resolveValues[ i ].promise()
.done( updateFunc( i, resolveValues ) )
.fail( deferred.reject )
.progress( updateFunc( i, progressValues ) );
} else {
--count;
--remaining;
}
}
if ( !count ) {
deferred.resolveWith( deferred, args );
}
} else if ( deferred !== firstParam ) {
deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
}

// if we're not waiting on anything, resolve the master
if ( !remaining ) {
deferred.resolveWith( deferred, resolveValues );
}

return promise;
}
});

0 comments on commit 6e066a4

Please sign in to comment.
You can’t perform that action at this time.