Fixes repeatable callbacks list with memory disabling. Unit tests for Deferreds updated.

This commit is contained in:
jaubourg 2011-05-24 01:03:30 +02:00
parent a3a657cb2c
commit 4f3f0e1d4e
2 changed files with 35 additions and 34 deletions

View file

@ -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;

View file

@ -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,