Adds addAfterFire flag. Unit tests updated with addAfterFire cases and also for when a callback returns false.
This commit is contained in:
parent
f392f8fcbc
commit
38aea727ec
|
@ -40,6 +40,9 @@ function createFlags( flags ) {
|
||||||
*
|
*
|
||||||
* stopOnFalse: interrupt callings when a callback returns false
|
* stopOnFalse: interrupt callings when a callback returns false
|
||||||
*
|
*
|
||||||
|
* addAfterFire: if callbacks are added while firing, then they are not executed until after
|
||||||
|
* the next call to fire/fireWith
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
jQuery.Callbacks = function( flags, filter ) {
|
jQuery.Callbacks = function( flags, filter ) {
|
||||||
|
|
||||||
|
@ -63,6 +66,8 @@ jQuery.Callbacks = function( flags, filter ) {
|
||||||
firing,
|
firing,
|
||||||
// First callback to fire (used internally by add and fireWith)
|
// First callback to fire (used internally by add and fireWith)
|
||||||
firingStart,
|
firingStart,
|
||||||
|
// End of the loop when firing
|
||||||
|
firingLength,
|
||||||
// Index of currently firing callback (modified by remove if needed)
|
// Index of currently firing callback (modified by remove if needed)
|
||||||
firingIndex,
|
firingIndex,
|
||||||
// Add a list of callbacks to the list
|
// Add a list of callbacks to the list
|
||||||
|
@ -101,9 +106,15 @@ jQuery.Callbacks = function( flags, filter ) {
|
||||||
if ( list ) {
|
if ( list ) {
|
||||||
var length = list.length;
|
var length = list.length;
|
||||||
add( arguments );
|
add( arguments );
|
||||||
|
// Do we need to add the callbacks to the
|
||||||
|
// current firing batch?
|
||||||
|
if ( firing ) {
|
||||||
|
if ( !flags.addAfterFire ) {
|
||||||
|
firingLength = list.length;
|
||||||
|
}
|
||||||
// With memory, if we're not firing then
|
// With memory, if we're not firing then
|
||||||
// we should call right away
|
// we should call right away
|
||||||
if ( !firing && flags.memory && memory ) {
|
} else if ( flags.memory && memory ) {
|
||||||
var tmp = memory;
|
var tmp = memory;
|
||||||
memory = undefined;
|
memory = undefined;
|
||||||
firingStart = length;
|
firingStart = length;
|
||||||
|
@ -117,10 +128,15 @@ jQuery.Callbacks = function( flags, filter ) {
|
||||||
if ( list ) {
|
if ( list ) {
|
||||||
for ( var i = 0; i < list.length; i++ ) {
|
for ( var i = 0; i < list.length; i++ ) {
|
||||||
if ( fn === list[ i ][ 0 ] ) {
|
if ( fn === list[ i ][ 0 ] ) {
|
||||||
// Handle firingIndex
|
// Handle firingIndex and firingLength
|
||||||
if ( firing && i <= firingIndex ) {
|
if ( firing ) {
|
||||||
|
if ( i <= firingLength ) {
|
||||||
|
firingLength--;
|
||||||
|
if ( i <= firingIndex ) {
|
||||||
firingIndex--;
|
firingIndex--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// Remove the element
|
// Remove the element
|
||||||
list.splice( i--, 1 );
|
list.splice( i--, 1 );
|
||||||
// If we have some unicity property then
|
// If we have some unicity property then
|
||||||
|
@ -185,7 +201,8 @@ jQuery.Callbacks = function( flags, filter ) {
|
||||||
firing = true;
|
firing = true;
|
||||||
firingIndex = firingStart || 0;
|
firingIndex = firingStart || 0;
|
||||||
firingStart = 0;
|
firingStart = 0;
|
||||||
for ( ; list && firingIndex < list.length; firingIndex++ ) {
|
firingLength = list.length;
|
||||||
|
for ( ; list && firingIndex < firingLength; firingIndex++ ) {
|
||||||
if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) {
|
if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,22 +12,28 @@ var output,
|
||||||
outputB = addToOutput( "B" ),
|
outputB = addToOutput( "B" ),
|
||||||
outputC = addToOutput( "C" ),
|
outputC = addToOutput( "C" ),
|
||||||
tests = {
|
tests = {
|
||||||
"": "X XABCABCC X XBB X",
|
"": "XABC X XABCABCC X XBB X XABA",
|
||||||
"once": "X X X X X",
|
"once": "XABC X X X X X XABA",
|
||||||
"memory": "XABC XABCABCCC XA XBB XB",
|
"memory": "XABC XABC XABCABCCC XA XBB XB XABA",
|
||||||
"unique": "X XABCA X XBB X",
|
"unique": "XABC X XABCA X XBB X XAB",
|
||||||
"relocate": "X XAABC X XBB X",
|
"relocate": "XABC X XAABC X XBB X XBA",
|
||||||
"stopOnFalse": "X XABCABCC X XBB X",
|
"stopOnFalse": "XABC X XABCABCC X XBB X XA",
|
||||||
"once memory": "XABC X XA X XA",
|
"addAfterFire": "XAB X XABCAB X XBB X XABA",
|
||||||
"once unique": "X X X X X",
|
"once memory": "XABC XABC X XA X XA XABA",
|
||||||
"once relocate": "X X X X X",
|
"once unique": "XABC X X X X X XAB",
|
||||||
"once stopOnFalse": "X X X X X",
|
"once relocate": "XABC X X X X X XBA",
|
||||||
"memory unique": "XA XABCA XA XBB XB",
|
"once stopOnFalse": "XABC X X X X X XA",
|
||||||
"memory relocate": "XB XAABC XA XBB XB",
|
"once addAfterFire": "XAB X X X X X XABA",
|
||||||
"memory stopOnFalse": "XABC XABCABCCC XA XBB XB",
|
"memory unique": "XABC XA XABCA XA XBB XB XAB",
|
||||||
"unique relocate": "X XAABC X XBB X",
|
"memory relocate": "XABC XB XAABC XA XBB XB XBA",
|
||||||
"unique stopOnFalse": "X XABCA X XBB X",
|
"memory stopOnFalse": "XABC XABC XABCABCCC XA XBB XB XA",
|
||||||
"relocate stopOnFalse": "X XAABC X XBB X"
|
"memory addAfterFire": "XAB XAB XABCABC XA XBB XB XABA",
|
||||||
|
"unique relocate": "XABC X XAABC X XBB X XBA",
|
||||||
|
"unique stopOnFalse": "XABC X XABCA X XBB X XA",
|
||||||
|
"unique addAfterFire": "XAB X XABCA X XBB X XAB",
|
||||||
|
"relocate stopOnFalse": "XABC X XAABC X XBB X X",
|
||||||
|
"relocate addAfterFire": "XAB X XAA X XBB X XBA",
|
||||||
|
"stopOnFalse addAfterFire": "XAB X XABCAB X XBB X XA"
|
||||||
},
|
},
|
||||||
filters = {
|
filters = {
|
||||||
"no filter": undefined,
|
"no filter": undefined,
|
||||||
|
@ -44,7 +50,7 @@ jQuery.each( tests, function( flags, resultString ) {
|
||||||
|
|
||||||
test( "jQuery.Callbacks( \"" + flags + "\" ) - " + filterLabel, function() {
|
test( "jQuery.Callbacks( \"" + flags + "\" ) - " + filterLabel, function() {
|
||||||
|
|
||||||
expect( 17 );
|
expect( 18 );
|
||||||
|
|
||||||
// Give qunit a little breathing room
|
// Give qunit a little breathing room
|
||||||
stop();
|
stop();
|
||||||
|
@ -133,7 +139,7 @@ jQuery.each( tests, function( flags, resultString ) {
|
||||||
outputA();
|
outputA();
|
||||||
}, outputB );
|
}, outputB );
|
||||||
cblist.fire();
|
cblist.fire();
|
||||||
strictEqual( output, "XABC", "Proper ordering" );
|
strictEqual( output, results.shift(), "Proper ordering" );
|
||||||
|
|
||||||
// Add and fire again
|
// Add and fire again
|
||||||
output = "X";
|
output = "X";
|
||||||
|
@ -169,6 +175,13 @@ jQuery.each( tests, function( flags, resultString ) {
|
||||||
} );
|
} );
|
||||||
strictEqual( output, results.shift(), "Multiple fire (second new callback)" );
|
strictEqual( output, results.shift(), "Multiple fire (second new callback)" );
|
||||||
|
|
||||||
|
// Return false
|
||||||
|
output = "X";
|
||||||
|
cblist = jQuery.Callbacks( flags );
|
||||||
|
cblist.add( outputA, function() { return false; }, outputB );
|
||||||
|
cblist.add( outputA );
|
||||||
|
cblist.fire();
|
||||||
|
strictEqual( output, results.shift(), "Callback returning false" );
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue