@@ -45,24 +45,27 @@ jQuery.Callbacks = function( options ) {
45
45
( optionsCache [ options ] || createOptions ( options ) ) :
46
46
jQuery . extend ( { } , options ) ;
47
47
48
- var // Last fire value (for non-forgettable lists)
48
+ var // Flag to know if list is currently firing
49
+ firing ,
50
+ // Last fire value (for non-forgettable lists)
49
51
memory ,
50
52
// Flag to know if list was already fired
51
53
fired ,
52
- // Flag to know if list is currently firing
53
- firing ,
54
- // First callback to fire (used internally by add and fireWith)
55
- firingStart ,
54
+ // Flag to prevent .fire/.fireWith
55
+ locked ,
56
56
// End of the loop when firing
57
57
firingLength ,
58
58
// Index of currently firing callback (modified by remove if needed)
59
59
firingIndex ,
60
+ // First callback to fire (used internally by add and fireWith)
61
+ firingStart ,
60
62
// Actual callback list
61
63
list = [ ] ,
62
64
// Stack of fire calls for repeatable lists
63
65
stack = ! options . once && [ ] ,
64
66
// Fire callbacks
65
67
fire = function ( data ) {
68
+ locked = options . once ;
66
69
memory = options . memory && data ;
67
70
fired = true ;
68
71
firingIndex = firingStart || 0 ;
@@ -78,13 +81,21 @@ jQuery.Callbacks = function( options ) {
78
81
}
79
82
}
80
83
firing = false ;
84
+
85
+ // If not disabled,
81
86
if ( list ) {
87
+
88
+ // If repeatable, check for pending execution
82
89
if ( stack ) {
83
90
if ( stack . length ) {
84
91
fire ( stack . shift ( ) ) ;
85
92
}
93
+
94
+ // If not repeatable but with memory, clear out spent callbacks
86
95
} else if ( memory ) {
87
96
list = [ ] ;
97
+
98
+ // Else, disable
88
99
} else {
89
100
self . disable ( ) ;
90
101
}
@@ -123,6 +134,7 @@ jQuery.Callbacks = function( options ) {
123
134
}
124
135
return this ;
125
136
} ,
137
+
126
138
// Remove a callback from the list
127
139
remove : function ( ) {
128
140
if ( list ) {
@@ -144,11 +156,13 @@ jQuery.Callbacks = function( options ) {
144
156
}
145
157
return this ;
146
158
} ,
159
+
147
160
// Check if a given callback is in the list.
148
161
// If no argument is given, return whether or not list has callbacks attached.
149
162
has : function ( fn ) {
150
163
return fn ? jQuery . inArray ( fn , list ) > - 1 : ! ! ( list && list . length ) ;
151
164
} ,
165
+
152
166
// Remove all callbacks from the list
153
167
empty : function ( ) {
154
168
if ( list ) {
@@ -157,30 +171,37 @@ jQuery.Callbacks = function( options ) {
157
171
}
158
172
return this ;
159
173
} ,
160
- // Have the list do nothing anymore
174
+
175
+ // Disable .fire and .add
176
+ // Abort any current/pending executions
177
+ // Clear all callbacks and values
161
178
disable : function ( ) {
162
179
list = stack = memory = undefined ;
180
+ locked = true ;
163
181
return this ;
164
182
} ,
165
- // Is it disabled?
166
183
disabled : function ( ) {
167
184
return ! list ;
168
185
} ,
169
- // Lock the list in its current state
186
+
187
+ // Disable .fire
188
+ // Also disable .add unless we have memory (since it would have no effect)
189
+ // Abort any pending executions
170
190
lock : function ( ) {
171
191
stack = undefined ;
192
+ locked = true ;
172
193
if ( ! memory ) {
173
194
self . disable ( ) ;
174
195
}
175
196
return this ;
176
197
} ,
177
- // Is it locked?
178
198
locked : function ( ) {
179
- return ! stack ;
199
+ return ! ! locked ;
180
200
} ,
201
+
181
202
// Call all callbacks with the given context and arguments
182
203
fireWith : function ( context , args ) {
183
- if ( list && ( ! fired || stack ) ) {
204
+ if ( ! locked ) {
184
205
args = args || [ ] ;
185
206
args = [ context , args . slice ? args . slice ( ) : args ] ;
186
207
if ( firing ) {
@@ -191,11 +212,13 @@ jQuery.Callbacks = function( options ) {
191
212
}
192
213
return this ;
193
214
} ,
215
+
194
216
// Call all the callbacks with the given arguments
195
217
fire : function ( ) {
196
218
self . fireWith ( this , arguments ) ;
197
219
return this ;
198
220
} ,
221
+
199
222
// To know if the callbacks have already been called at least once
200
223
fired : function ( ) {
201
224
return ! ! fired ;
0 commit comments