6 ], function( jQuery, toType, isFunction, rnothtmlwhite ) {
10 // Convert String-formatted options into Object-formatted ones
11 function createOptions( options ) {
13 jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
14 object[ flag ] = true;
20 * Create a callback list using the following parameters:
22 * options: an optional list of space-separated options that will change how
23 * the callback list behaves or a more traditional option object
25 * By default a callback list will act like an event callback list and can be
26 * "fired" multiple times.
30 * once: will ensure the callback list can only be fired once (like a Deferred)
32 * memory: will keep track of previous values and will call any callback added
33 * after the list has been fired right away with the latest "memorized"
34 * values (like a Deferred)
36 * unique: will ensure a callback can only be added once (no duplicate in the list)
38 * stopOnFalse: interrupt callings when a callback returns false
41 jQuery.Callbacks = function( options ) {
43 // Convert options from String-formatted to Object-formatted if needed
44 // (we check in cache first)
45 options = typeof options === "string" ?
46 createOptions( options ) :
47 jQuery.extend( {}, options );
49 var // Flag to know if list is currently firing
52 // Last fire value for non-forgettable lists
55 // Flag to know if list was already fired
58 // Flag to prevent firing
61 // Actual callback list
64 // Queue of execution data for repeatable lists
67 // Index of currently firing callback (modified by add/remove as needed)
73 // Enforce single-firing
74 locked = locked || options.once;
76 // Execute callbacks for all pending executions,
77 // respecting firingIndex overrides and runtime changes
78 fired = firing = true;
79 for ( ; queue.length; firingIndex = -1 ) {
80 memory = queue.shift();
81 while ( ++firingIndex < list.length ) {
83 // Run callback and check for early termination
84 if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false &&
85 options.stopOnFalse ) {
87 // Jump to end and forget the data so .add doesn't re-fire
88 firingIndex = list.length;
94 // Forget the data if we're done with it
95 if ( !options.memory ) {
101 // Clean up if we're done firing for good
104 // Keep an empty list if we have data for future add calls
108 // Otherwise, this object is spent
115 // Actual Callbacks object
118 // Add a callback or a collection of callbacks to the list
122 // If we have memory from a past run, we should fire after adding
123 if ( memory && !firing ) {
124 firingIndex = list.length - 1;
125 queue.push( memory );
128 ( function add( args ) {
129 jQuery.each( args, function( _, arg ) {
130 if ( isFunction( arg ) ) {
131 if ( !options.unique || !self.has( arg ) ) {
134 } else if ( arg && arg.length && toType( arg ) !== "string" ) {
136 // Inspect recursively
142 if ( memory && !firing ) {
149 // Remove a callback from the list
151 jQuery.each( arguments, function( _, arg ) {
153 while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
154 list.splice( index, 1 );
156 // Handle firing indexes
157 if ( index <= firingIndex ) {
165 // Check if a given callback is in the list.
166 // If no argument is given, return whether or not list has callbacks attached.
167 has: function( fn ) {
169 jQuery.inArray( fn, list ) > -1 :
173 // Remove all callbacks from the list
181 // Disable .fire and .add
182 // Abort any current/pending executions
183 // Clear all callbacks and values
184 disable: function() {
189 disabled: function() {
194 // Also disable .add unless we have memory (since it would have no effect)
195 // Abort any pending executions
198 if ( !memory && !firing ) {
207 // Call all callbacks with the given context and arguments
208 fireWith: function( context, args ) {
211 args = [ context, args.slice ? args.slice() : args ];
220 // Call all the callbacks with the given arguments
222 self.fireWith( this, arguments );
226 // To know if the callbacks have already been called at least once