jquery/src/deferred.js

145 lines
4.0 KiB
JavaScript

(function( jQuery ) {
var // Static reference to slice
sliceDeferred = [].slice;
jQuery.extend({
Deferred: function( func ) {
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",
promise = {
state: function() {
return state;
},
always: function() {
deferred.done( arguments ).fail( arguments );
return this;
},
then: function( /* fnDone, fnFail, fnProgress */ ) {
var fns = arguments;
return jQuery.Deferred(function( newDefer ) {
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 );
} else {
newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] );
}
} :
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 ) {
return typeof obj === "object" ? jQuery.extend( obj, promise ) : promise;
}
},
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;
// promise[ done | fail | progress ] = list.add
promise[ tuple[1] ] = list.add;
// Handle state
if ( i < 2 ) {
stateString = tuple[ 0 ].replace( /e?$/, "ed" );
list.add(function() {
state = stateString;
tuples[ i ^ 1 ][ 2 ].disable();
tuples[ 2 ][ 2 ].lock();
});
promise[ "isR" + stateString.substr( 1 ) ] = list.fired;
}
// 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 ) {
func.call( deferred, deferred );
}
// All done!
return deferred;
},
// Deferred helper
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 ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
resolveValues[ i ].promise()
.done( updateFunc( i, resolveValues ) )
.fail( deferred.reject )
.progress( updateFunc( i, progressValues ) );
} else {
--remaining;
}
}
}
// if we're not waiting on anything, resolve the master
if ( !remaining ) {
deferred.resolveWith( deferred, resolveValues );
}
return promise;
}
});
})( jQuery );