Skip to content

Commit

Permalink
Deferred: Remove undocumented progress notifications in $.when
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin committed Jan 13, 2016
1 parent f5fb8d7 commit bdf1b8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
39 changes: 18 additions & 21 deletions src/deferred.js
Expand Up @@ -295,7 +295,7 @@ jQuery.extend( {

// Deferred helper
when: function() {
var method,
var method, resolveContexts,
i = 0,
resolveValues = slice.call( arguments ),
length = resolveValues.length,
Expand All @@ -306,47 +306,44 @@ jQuery.extend( {
// the master Deferred.
master = jQuery.Deferred(),

// Update function for both resolve and progress values
updateFunc = function( i, contexts, values ) {
// Update function for both resolving subordinates
updateFunc = function( i ) {
return function( value ) {
contexts[ i ] = this;
values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
if ( values === progressValues ) {
master.notifyWith( contexts, values );
} else if ( !( --remaining ) ) {
resolveContexts[ i ] = this;
resolveValues[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
if ( !( --remaining ) ) {
master.resolveWith(
contexts.length === 1 ? contexts[ 0 ] : contexts,
values
resolveContexts.length === 1 ? resolveContexts[ 0 ] : resolveContexts,
resolveValues
);
}
};
},
progressValues, progressContexts, resolveContexts;
};

// Add listeners to Deferred subordinates; treat others as resolved
// Add listeners to promise-like subordinates; treat others as resolved
if ( length > 0 ) {
progressValues = new Array( length );
progressContexts = new Array( length );
resolveContexts = new Array( length );
for ( ; i < length; i++ ) {

// jQuery.Deferred - treated specially to get resolve-sync behavior
if ( resolveValues[ i ] &&
jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) {

method.call( resolveValues[ i ] )
.progress( updateFunc( i, progressContexts, progressValues ) )
.done( updateFunc( i, resolveContexts, resolveValues ) )
.done( updateFunc( i ) )
.fail( master.reject );

// Other thenables
} else if ( resolveValues[ i ] &&
jQuery.isFunction( ( method = resolveValues[ i ].then ) ) ) {

method.call(
resolveValues[ i ],
updateFunc( i, resolveContexts, resolveValues ),
master.reject,
updateFunc( i, progressContexts, progressValues )
updateFunc( i ),
master.reject
);
} else {
updateFunc( i, resolveContexts, resolveValues )( resolveValues[ i ] );
updateFunc( i )( resolveValues[ i ] );
}
}

Expand Down
24 changes: 4 additions & 20 deletions test/unit/deferred.js
Expand Up @@ -710,13 +710,12 @@ QUnit.test( "jQuery.when", function( assert ) {

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

assert.expect( 195 );
assert.expect( 81 );

var deferreds = {
rawValue: 1,
fulfilled: jQuery.Deferred().resolve( 1 ),
rejected: jQuery.Deferred().reject( 0 ),
notified: jQuery.Deferred().notify( true ),
eventuallyFulfilled: jQuery.Deferred().notify( true ),
eventuallyRejected: jQuery.Deferred().notify( true ),
fulfilledStandardPromise: Promise.resolve( 1 ),
Expand All @@ -733,11 +732,6 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
eventuallyRejected: true,
rejectedStandardPromise: true
},
willNotify = {
notified: true,
eventuallyFulfilled: true,
eventuallyRejected: true
},
counter = 49;

QUnit.stop();
Expand All @@ -752,9 +746,7 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
jQuery.each( deferreds, function( id2, defer2 ) {
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
shouldError = willError[ id1 ] || willError[ id2 ],
shouldNotify = willNotify[ id1 ] || willNotify[ id2 ],
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
code = "jQuery.when( " + id1 + ", " + id2 + " )",
context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : window,
context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : window;
Expand All @@ -773,30 +765,22 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
} else {
assert.ok( false, code + " => reject" );
}
} ).progress( function( a, b ) {
assert.deepEqual( [ a, b ], expectedNotify, code + " => progress" );
assert.strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" );
assert.strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" );
} ).always( restart );
} );
} );
deferreds.eventuallyFulfilled.resolve( 1 );
deferreds.eventuallyRejected.reject( 0 );
} );

QUnit.test( "jQuery.when - resolved", function( assert ) {
QUnit.test( "jQuery.when - notify does not affect resolved", function( assert ) {

assert.expect( 6 );
assert.expect( 3 );

var a = jQuery.Deferred().notify( 1 ).resolve( 4 ),
b = jQuery.Deferred().notify( 2 ).resolve( 5 ),
c = jQuery.Deferred().notify( 3 ).resolve( 6 );

jQuery.when( a, b, c ).progress( function( a, b, c ) {
assert.strictEqual( a, 1, "first notify value ok" );
assert.strictEqual( b, 2, "second notify value ok" );
assert.strictEqual( c, 3, "third notify value ok" );
} ).done( function( a, b, c ) {
jQuery.when( a, b, c ).done( function( a, b, c ) {
assert.strictEqual( a, 4, "first resolve value ok" );
assert.strictEqual( b, 5, "second resolve value ok" );
assert.strictEqual( c, 6, "third resolve value ok" );
Expand Down

0 comments on commit bdf1b8f

Please sign in to comment.