Skip to content

Commit 198290a

Browse files
committed
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).
1 parent 03c4fe9 commit 198290a

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

src/callbacks.js

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ jQuery.Callbacks = function( flags, filter ) {
6161
memory,
6262
// Flag to know if list is currently firing
6363
firing,
64-
// Index of cells to remove in the list after firing
65-
deleteAfterFire,
64+
// First callback to fire (used internally by add and fireWith)
65+
firingStart,
66+
// Index of currently firing callback (modified by remove if needed)
67+
firingIndex,
6668
// Add a list of callbacks to the list
6769
add = function( args ) {
6870
var i,
@@ -101,28 +103,25 @@ jQuery.Callbacks = function( flags, filter ) {
101103
// With memory, if we're not firing then
102104
// we should call right away
103105
if ( !firing && flags.memory && memory ) {
104-
// Trick the list into thinking it needs to be fired
105106
var tmp = memory;
106107
memory = undefined;
107-
object.fireWith( tmp[ 0 ], tmp[ 1 ], length );
108+
firingStart = length;
109+
object.fireWith( tmp[ 0 ], tmp[ 1 ] );
108110
}
109111
}
110112
return this;
111113
},
112114
// Remove a callback from the list
113115
remove: function( fn ) {
114116
if ( list ) {
115-
var i = 0,
116-
length = list.length;
117-
for ( ; i < length; i++ ) {
118-
if ( list[ i ] && fn === list[ i ][ 0 ] ) {
119-
if ( firing ) {
120-
list[ i ] = undefined;
121-
deleteAfterFire.push( i );
122-
} else {
123-
list.splice( i, 1 );
124-
i--;
117+
for ( var i = 0; i < list.length; i++ ) {
118+
if ( fn === list[ i ][ 0 ] ) {
119+
// Handle firingIndex
120+
if ( firing && i <= firingIndex ) {
121+
firingIndex--;
125122
}
123+
// Remove the element
124+
list.splice( i--, 1 );
126125
// If we have some unicity property then
127126
// we only need to do this once
128127
if ( flags.unique || flags.relocate ) {
@@ -139,7 +138,7 @@ jQuery.Callbacks = function( flags, filter ) {
139138
var i = 0,
140139
length = list.length;
141140
for ( ; i < length; i++ ) {
142-
if ( list[ i ] && fn === list[ i ][ 0 ] ) {
141+
if ( fn === list[ i ][ 0 ] ) {
143142
return true;
144143
}
145144
}
@@ -165,42 +164,31 @@ jQuery.Callbacks = function( flags, filter ) {
165164
return this;
166165
},
167166
// Call all callbacks with the given context and arguments
168-
fireWith: function( context, args, start /* internal */ ) {
169-
var i,
170-
done,
171-
stoppedOnFalse;
167+
fireWith: function( context, args ) {
168+
var start = firingStart;
169+
firingStart = 0;
172170
if ( list && stack && ( !flags.once || !memory && !firing ) ) {
173171
if ( firing ) {
174172
stack.push( [ context, args ] );
175173
} else {
176174
args = args || [];
177175
memory = !flags.memory || [ context, args ];
178176
firing = true;
179-
deleteAfterFire = [];
180-
try {
181-
for ( i = start || 0; list && i < list.length; i++ ) {
182-
if (( stoppedOnFalse = list[ i ] &&
183-
list[ i ][ 1 ].apply( context, args ) === false &&
184-
flags.stopOnFalse )) {
185-
break;
186-
}
177+
for ( firingIndex = start || 0; list && firingIndex < list.length; firingIndex++ ) {
178+
if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) {
179+
break;
187180
}
188-
} finally {
189-
firing = false;
190-
if ( list ) {
191-
done = ( stoppedOnFalse || i >= list.length );
192-
for ( i = 0; i < deleteAfterFire.length; i++ ) {
193-
list.splice( deleteAfterFire[ i ], 1 );
194-
}
195-
if ( !flags.once ) {
196-
if ( done && stack && stack.length ) {
197-
object.fireWith.apply( this, stack.shift() );
198-
}
199-
} else if ( !flags.memory ) {
200-
object.disable();
201-
} else {
202-
list = [];
181+
}
182+
firing = false;
183+
if ( list ) {
184+
if ( !flags.once ) {
185+
if ( stack && stack.length ) {
186+
object.fireWith.apply( this, stack.shift() );
203187
}
188+
} else if ( !flags.memory ) {
189+
object.disable();
190+
} else {
191+
list = [];
204192
}
205193
}
206194
}

0 commit comments

Comments
 (0)