Simplifies how removal when firing is handled. Removes exception handling (lets the list deadlock). Makes sure no external code can fire the list from a given index (only possible internally).

This commit is contained in:
jaubourg 2011-05-08 16:11:00 +02:00
parent 03c4fe9da9
commit 198290a9a7

View file

@ -61,8 +61,10 @@ jQuery.Callbacks = function( flags, filter ) {
memory, memory,
// Flag to know if list is currently firing // Flag to know if list is currently firing
firing, firing,
// Index of cells to remove in the list after firing // First callback to fire (used internally by add and fireWith)
deleteAfterFire, firingStart,
// Index of currently firing callback (modified by remove if needed)
firingIndex,
// Add a list of callbacks to the list // Add a list of callbacks to the list
add = function( args ) { add = function( args ) {
var i, var i,
@ -101,10 +103,10 @@ jQuery.Callbacks = function( flags, filter ) {
// 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 ) { if ( !firing && flags.memory && memory ) {
// Trick the list into thinking it needs to be fired
var tmp = memory; var tmp = memory;
memory = undefined; memory = undefined;
object.fireWith( tmp[ 0 ], tmp[ 1 ], length ); firingStart = length;
object.fireWith( tmp[ 0 ], tmp[ 1 ] );
} }
} }
return this; return this;
@ -112,17 +114,14 @@ jQuery.Callbacks = function( flags, filter ) {
// Remove a callback from the list // Remove a callback from the list
remove: function( fn ) { remove: function( fn ) {
if ( list ) { if ( list ) {
var i = 0, for ( var i = 0; i < list.length; i++ ) {
length = list.length; if ( fn === list[ i ][ 0 ] ) {
for ( ; i < length; i++ ) { // Handle firingIndex
if ( list[ i ] && fn === list[ i ][ 0 ] ) { if ( firing && i <= firingIndex ) {
if ( firing ) { firingIndex--;
list[ i ] = undefined;
deleteAfterFire.push( i );
} else {
list.splice( i, 1 );
i--;
} }
// Remove the element
list.splice( i--, 1 );
// If we have some unicity property then // If we have some unicity property then
// we only need to do this once // we only need to do this once
if ( flags.unique || flags.relocate ) { if ( flags.unique || flags.relocate ) {
@ -139,7 +138,7 @@ jQuery.Callbacks = function( flags, filter ) {
var i = 0, var i = 0,
length = list.length; length = list.length;
for ( ; i < length; i++ ) { for ( ; i < length; i++ ) {
if ( list[ i ] && fn === list[ i ][ 0 ] ) { if ( fn === list[ i ][ 0 ] ) {
return true; return true;
} }
} }
@ -165,10 +164,9 @@ jQuery.Callbacks = function( flags, filter ) {
return this; return this;
}, },
// Call all callbacks with the given context and arguments // Call all callbacks with the given context and arguments
fireWith: function( context, args, start /* internal */ ) { fireWith: function( context, args ) {
var i, var start = firingStart;
done, firingStart = 0;
stoppedOnFalse;
if ( list && stack && ( !flags.once || !memory && !firing ) ) { if ( list && stack && ( !flags.once || !memory && !firing ) ) {
if ( firing ) { if ( firing ) {
stack.push( [ context, args ] ); stack.push( [ context, args ] );
@ -176,24 +174,15 @@ jQuery.Callbacks = function( flags, filter ) {
args = args || []; args = args || [];
memory = !flags.memory || [ context, args ]; memory = !flags.memory || [ context, args ];
firing = true; firing = true;
deleteAfterFire = []; for ( firingIndex = start || 0; list && firingIndex < list.length; firingIndex++ ) {
try { if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) {
for ( i = start || 0; list && i < list.length; i++ ) {
if (( stoppedOnFalse = list[ i ] &&
list[ i ][ 1 ].apply( context, args ) === false &&
flags.stopOnFalse )) {
break; break;
} }
} }
} finally {
firing = false; firing = false;
if ( list ) { if ( list ) {
done = ( stoppedOnFalse || i >= list.length );
for ( i = 0; i < deleteAfterFire.length; i++ ) {
list.splice( deleteAfterFire[ i ], 1 );
}
if ( !flags.once ) { if ( !flags.once ) {
if ( done && stack && stack.length ) { if ( stack && stack.length ) {
object.fireWith.apply( this, stack.shift() ); object.fireWith.apply( this, stack.shift() );
} }
} else if ( !flags.memory ) { } else if ( !flags.memory ) {
@ -204,7 +193,6 @@ jQuery.Callbacks = function( flags, filter ) {
} }
} }
} }
}
return this; return this;
}, },
// Call all the callbacks with the given arguments // Call all the callbacks with the given arguments