From bdf1b8f317d793d8ebbbe7787955edabf201a685 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Mon, 11 Jan 2016 19:10:28 -0500 Subject: [PATCH] Deferred: Remove undocumented progress notifications in $.when Fixes gh-2710 Closes gh-2816 --- src/deferred.js | 39 ++++++++++++++++++--------------------- test/unit/deferred.js | 24 ++++-------------------- 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/src/deferred.js b/src/deferred.js index 846528e79..ff12ac3e0 100644 --- a/src/deferred.js +++ b/src/deferred.js @@ -295,7 +295,7 @@ jQuery.extend( { // Deferred helper when: function() { - var method, + var method, resolveContexts, i = 0, resolveValues = slice.call( arguments ), length = resolveValues.length, @@ -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 ] ); } } diff --git a/test/unit/deferred.js b/test/unit/deferred.js index 1a721d1ee..83c2f4f6b 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -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 ), @@ -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(); @@ -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; @@ -773,10 +765,6 @@ 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 ); } ); } ); @@ -784,19 +772,15 @@ QUnit.test( "jQuery.when - joined", function( assert ) { 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" ); -- 2.39.5