From 9edc3d4f398975b65ceeab4e35f11c314b51925e Mon Sep 17 00:00:00 2001 From: jaubourg Date: Tue, 24 May 2011 01:03:30 +0200 Subject: [PATCH] Fixes repeatable callbacks list with memory disabling. Unit tests for Deferreds updated. --- src/callbacks.js | 57 ++++++++++++++++++++++--------------------- test/unit/deferred.js | 12 ++++----- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/callbacks.js b/src/callbacks.js index bf86575e..b3635df7 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -100,6 +100,33 @@ jQuery.Callbacks = function( flags, filter ) { } } }, + fire = function( context, args ) { + args = args || []; + memory = !flags.memory || [ context, args ]; + firing = true; + firingIndex = firingStart || 0; + firingStart = 0; + firingLength = list.length; + for ( ; list && firingIndex < firingLength; firingIndex++ ) { + if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) { + memory = true; // Mark as halted + break; + } + } + firing = false; + if ( list ) { + if ( !flags.once ) { + if ( stack && stack.length ) { + memory = stack.shift(); + object.fireWith( memory[ 0 ], memory[ 1 ] ); + } + } else if ( memory === true ) { + object.disable(); + } else { + list = []; + } + } + }, object = { // Add a callback or a collection of callbacks to the list add: function() { @@ -116,10 +143,8 @@ jQuery.Callbacks = function( flags, filter ) { // we should call right away, unless previous // firing was halted (stopOnFalse) } else if ( memory && memory !== true ) { - var tmp = memory; - memory = undefined; firingStart = length; - object.fireWith( tmp[ 0 ], tmp[ 1 ] ); + fire( memory[ 0 ], memory[ 1 ] ); } } return this; @@ -197,31 +222,7 @@ jQuery.Callbacks = function( flags, filter ) { stack.push( [ context, args ] ); } } else if ( !( flags.once && memory ) ) { - args = args || []; - memory = !flags.memory || [ context, args ]; - firing = true; - firingIndex = firingStart || 0; - firingStart = 0; - firingLength = list.length; - for ( ; list && firingIndex < firingLength; firingIndex++ ) { - if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) { - memory = true; // Mark as halted - break; - } - } - firing = false; - if ( list ) { - if ( !flags.once ) { - if ( stack && stack.length ) { - memory = stack.shift(); - object.fireWith( memory[ 0 ], memory[ 1 ] ); - } - } else if ( memory === true ) { - object.disable(); - } else { - list = []; - } - } + fire( context, args ); } } return this; diff --git a/test/unit/deferred.js b/test/unit/deferred.js index 6e3cab89..ed568799 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -39,7 +39,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) { createDeferred( function( defer ) { var checked = 0; defer.progress(function( value ) { - strictEqual( value, checked, "Progress: right value (" + value + ") received" ) + strictEqual( value, checked, "Progress: right value (" + value + ") received" ); }); for( checked = 0; checked < 3 ; checked++ ) { defer.ping( checked ); @@ -146,8 +146,8 @@ test( "jQuery.Deferred.pipe - filtering (progress)", function() { defer.ping( 2, 3 ); - strictEqual( value1, 2, "first reject value ok" ); - strictEqual( value2, 3, "second reject value ok" ); + strictEqual( value1, 2, "first progress value ok" ); + strictEqual( value2, 3, "second progress value ok" ); strictEqual( value3, 6, "result of filter ok" ); }); @@ -236,8 +236,8 @@ test( "jQuery.Deferred.pipe - deferred (progress)", function() { defer.ping( 2, 3 ); - strictEqual( value1, 2, "first reject value ok" ); - strictEqual( value2, 3, "second reject value ok" ); + strictEqual( value1, 2, "first progress value ok" ); + strictEqual( value2, 3, "second progress value ok" ); strictEqual( value3, 6, "result of filter ok" ); }); @@ -284,7 +284,7 @@ test( "jQuery.when" , function() { test("jQuery.when - joined", function() { - expect(50); + expect(53); var deferreds = { value: 1,