Adds an invert method to promises that returns a "inverted" promise that is resolved when the underlying deferred is rejected and rejected when the underlying deferred is resolved.

This commit is contained in:
jaubourg 2011-01-31 14:36:59 +01:00
parent d753a7a117
commit 4e97543051
2 changed files with 75 additions and 17 deletions

View file

@ -56,8 +56,16 @@ var jQuery = function( selector, context ) {
// The deferred used on DOM ready
readyList,
// Promise methods
promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
// Promise methods (with equivalent for invert)
promiseMethods = {
then: 0, // will be overwritten for invert
done: "fail",
fail: "done",
isResolved: "isRejected",
isRejected: "isResolved",
promise: "invert",
invert: "promise"
},
// The ready event handler
DOMContentLoaded,
@ -879,8 +887,9 @@ jQuery.extend({
Deferred: function( func ) {
var deferred = jQuery._Deferred(),
failDeferred = jQuery._Deferred(),
promise;
// Add errorDeferred methods, then and promise
promise,
invert;
// Add errorDeferred methods, then, promise and invert
jQuery.extend( deferred, {
then: function( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
@ -892,18 +901,35 @@ jQuery.extend({
isRejected: failDeferred.isResolved,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function( obj , i /* internal */ ) {
promise: function( obj ) {
if ( obj == null ) {
if ( promise ) {
return promise;
}
promise = obj = {};
}
i = promiseMethods.length;
while( i-- ) {
obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
for( var methodName in promiseMethods ) {
obj[ methodName ] = deferred[ methodName ];
}
return obj;
},
// Get the invert promise for this deferred
// If obj is provided, the invert promise aspect is added to the object
invert: function( obj ) {
if ( obj == null ) {
if ( invert ) {
return invert;
}
invert = obj = {};
}
for( var methodName in promiseMethods ) {
obj[ methodName ] = promiseMethods[ methodName ] && deferred[ promiseMethods[methodName] ];
}
obj.then = invert.then || function( doneCallbacks, failCallbacks ) {
deferred.done( failCallbacks ).fail( doneCallbacks );
return this;
};
return obj;
}
} );
// Make sure only one callback list will be used

View file

@ -554,8 +554,8 @@ test("toArray()", function() {
expect(1);
same( jQuery("p").toArray(),
q("firstp","ap","sndp","en","sap","first"),
"Convert jQuery object to an Array" )
})
"Convert jQuery object to an Array" );
});
test("get(Number)", function() {
expect(2);
@ -567,7 +567,7 @@ test("get(-Number)",function() {
expect(2);
equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
})
});
test("each(Function)", function() {
expect(1);
@ -1009,7 +1009,7 @@ test("jQuery._Deferred()", function() {
test("jQuery.Deferred()", function() {
expect( 10 );
expect( 20 );
jQuery.Deferred( function( defer ) {
strictEqual( this , defer , "Defer passed as this & first argument" );
@ -1049,10 +1049,26 @@ test("jQuery.Deferred()", function() {
ok( true , "Error on reject (new)" );
});
strictEqual( jQuery.Deferred().resolve( "test" ).invert().then(null,function(value) {
strictEqual( value, "test", "Resolved deferred => then fail callback called" );
}).fail(function( value ) {
strictEqual( value, "test", "Resolved deferred => fail callback called" );
}).isRejected(), true, "Invert promise is rejected when deferred is resolved" );
strictEqual( jQuery.Deferred().reject( "test" ).invert().then(function(value) {
strictEqual( value, "test", "Rejected deferred => then done callback called" );
}).done(function( value ) {
strictEqual( value, "test", "Rejected deferred => done callback called" );
}).isResolved(), true, "Invert promise is resolved when deferred is rejected" );
var tmp = jQuery.Deferred();
strictEqual( tmp.promise() , tmp.promise() , "Test deferred always return same promise" );
strictEqual( tmp.invert() , tmp.invert() , "Test deferred always return same invert" );
strictEqual( tmp.promise() , tmp.promise().promise() , "Test deferred's promise always return same promise as deferred" );
strictEqual( tmp.promise() , tmp.invert().invert() , "Test deferred's promise is the same as double invert" );
strictEqual( tmp.invert() , tmp.invert().promise() , "Test deferred's invert always return same invert as deferred as a promise" );
strictEqual( tmp.invert() , tmp.promise().invert() , "Test deferred's promise always return same invert as deferred" );
});
test("jQuery.when()", function() {
@ -1100,7 +1116,7 @@ test("jQuery.when()", function() {
test("jQuery.when() - joined", function() {
expect(8);
expect(14);
jQuery.when( 1, 2, 3 ).done( function( a, b, c ) {
strictEqual( a , 1 , "Test first param is first resolved value - non-observables" );
@ -1121,12 +1137,28 @@ test("jQuery.when() - joined", function() {
ok( false , "Test the created deferred was resolved - resolved observable");
});
jQuery.when( 1 , successDeferred.invert() , 3 ).fail( function( a, b, c ) {
strictEqual( a , 1 , "Test first param is first rejected value - resolved observable inverted" );
same( b , 2 , "Test second param is second rejected value - resolved observable inverted" );
strictEqual( c , 3 , "Test third param is third rejected value - resolved observable inverted" );
}).done( function() {
ok( false , "Test the inverted deferred was rejected - resolved observable inverted");
});
jQuery.when( 1 , errorDeferred , 3 ).done( function() {
ok( false , "Test the created deferred was rejected - rejected observable");
}).fail( function( error , errorParam ) {
strictEqual( error , "error" , "Test first param is first rejected value - rejected observable" );
strictEqual( errorParam , "errorParam" , "Test second param is second rejected value - rejected observable" );
});
jQuery.when( 1 , errorDeferred.invert() , 3 ).fail( function() {
ok( false , "Test the inverted deferred was resolved - rejected observable inverted");
}).done( function( a , b , c ) {
strictEqual( a , 1 , "Test first param is first resolved value - rejected observable inverted" );
same( b , [ "error", "errorParam" ] , "Test second param is second resolved value - rejected observable inverted" );
strictEqual( c , 3 , "Test third param is third resolved value - rejected observable inverted" );
});
});
test("jQuery.sub() - Static Methods", function(){