Skip to content
Permalink
Browse files
Revert "Adds an invert method to promises that returns a "inverted" p…
…romise that is resolved when the underlying deferred is rejected and rejected when the underlying deferred is resolved."

This reverts commit 4e97543.
  • Loading branch information
jaubourg committed Feb 18, 2011
1 parent 752db8f commit a43a5ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 75 deletions.
@@ -56,16 +56,8 @@ var jQuery = function( selector, context ) {
// The deferred used on DOM ready
readyList,

// 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"
},
// Promise methods
promiseMethods = "then done fail isResolved isRejected promise".split( " " ),

// The ready event handler
DOMContentLoaded,
@@ -885,9 +877,8 @@ jQuery.extend({
Deferred: function( func ) {
var deferred = jQuery._Deferred(),
failDeferred = jQuery._Deferred(),
promise,
invert;
// Add errorDeferred methods, then, promise and invert
promise;
// Add errorDeferred methods, then and promise
jQuery.extend( deferred, {
then: function( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
@@ -899,34 +890,17 @@ 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 ) {
promise: function( obj , i /* internal */ ) {
if ( obj == null ) {
if ( promise ) {
return promise;
}
promise = obj = {};
}
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] ];
i = promiseMethods.length;

This comment has been minimized.

Copy link
@RStankov

RStankov Feb 21, 2011

Why you are not declaring "var i" here ?

This comment has been minimized.

Copy link
@staabm

staabm Feb 21, 2011

Contributor

Its a parameter to this function...

This comment has been minimized.

Copy link
@RStankov

RStankov Feb 21, 2011

Yes can see that, but why? It's value starts from promiseMethods.length always not dependent on what you are passing to the function.

This comment has been minimized.

Copy link
@vivin

vivin Feb 21, 2011

I don't think "i" needs to be a parameter to this function; the second parameter is effectively ignored.

This comment has been minimized.

Copy link
@dmethvin

dmethvin Feb 22, 2011

Member

It's just an extreme way of avoiding a "var" declaration, which can yield a bit more compressibility. It can be confusing though.

This comment has been minimized.

Copy link
@vivin

vivin Feb 22, 2011

Interesting. I did not know that.

This comment has been minimized.

Copy link
@curiousdannii

curiousdannii Feb 22, 2011

Might as well keep all of the var statements in the source code. If that extreme compression was needed uglifyjs could shift them all to parameters.

This comment has been minimized.

Copy link
@StevenBlack

StevenBlack Feb 22, 2011

I can't help but sense a false-economy here...

This comment has been minimized.

Copy link
@jaubourg

jaubourg Feb 22, 2011

Author Member

Yeah, a very bad habit I took when working on jQuery-JSONP. You'll notice the "invert" commit removed it. I just forgot to "fix it" again which is now done here: e405419

while( i-- ) {
obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
}
obj.then = invert.then || function( doneCallbacks, failCallbacks ) {
deferred.done( failCallbacks ).fail( doneCallbacks );
return this;
};
return obj;
}
} );
@@ -554,8 +554,8 @@ test("toArray()", function() {
expect(1);
same( jQuery("#main 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( 20 );
expect( 10 );

jQuery.Deferred( function( defer ) {
strictEqual( this , defer , "Defer passed as this & first argument" );
@@ -1049,26 +1049,10 @@ 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() {
@@ -1116,7 +1100,7 @@ test("jQuery.when()", function() {

test("jQuery.when() - joined", function() {

expect(14);
expect(8);

jQuery.when( 1, 2, 3 ).done( function( a, b, c ) {
strictEqual( a , 1 , "Test first param is first resolved value - non-observables" );
@@ -1137,28 +1121,12 @@ 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(){
@@ -1175,16 +1143,16 @@ test("jQuery.sub() - Static Methods", function(){
}
});
Subclass.fn.extend({subClassMethod: function() { return this;}});

//Test Simple Subclass
ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');
same(Subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
equal(jQuery.ajax, Subclass.ajax, 'The subclass failed to get all top level methods');

//Create a SubSubclass
var SubSubclass = Subclass.sub();

//Make Sure the SubSubclass inherited properly
ok(SubSubclass.topLevelMethod() === false, 'SubSubclass.topLevelMethod thought debug was true');
ok(SubSubclass.config.locale == 'en_US', SubSubclass.config.locale + ' is wrong!');
@@ -1201,7 +1169,7 @@ test("jQuery.sub() - Static Methods", function(){
ok(SubSubclass.config.locale == 'es_MX', SubSubclass.config.locale + ' is wrong!');
ok(SubSubclass.config.test == 'worked', 'SubSubclass.config.test is set incorrectly');
notEqual(jQuery.ajax, SubSubclass.ajax, 'The subsubclass failed to get all top level methods');

//This shows that the modifications to the SubSubClass did not bubble back up to it's superclass
ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');

0 comments on commit a43a5ca

Please sign in to comment.