4 ], function( jQuery, rnotwhite ) {
6 // Convert String-formatted options into Object-formatted ones
7 function createOptions( options ) {
9 jQuery.each( options.match( rnotwhite ) || [], function( _, flag ) {
10 object[ flag ] = true;
16 * Create a callback list using the following parameters:
18 * options: an optional list of space-separated options that will change how
19 * the callback list behaves or a more traditional option object
21 * By default a callback list will act like an event callback list and can be
22 * "fired" multiple times.
26 * once: will ensure the callback list can only be fired once (like a Deferred)
28 * memory: will keep track of previous values and will call any callback added
29 * after the list has been fired right away with the latest "memorized"
30 * values (like a Deferred)
32 * unique: will ensure a callback can only be added once (no duplicate in the list)
34 * stopOnFalse: interrupt callings when a callback returns false
37 jQuery.Callbacks = function( options ) {
39 // Convert options from String-formatted to Object-formatted if needed
40 // (we check in cache first)
41 options = typeof options === "string" ?
42 createOptions( options ) :
43 jQuery.extend( {}, options );
45 var // Flag to know if list is currently firing
48 // Last fire value for non-forgettable lists
51 // Flag to know if list was already fired
54 // Flag to prevent firing
57 // Actual callback list
60 // Queue of execution data for repeatable lists
63 // Index of currently firing callback (modified by add/remove as needed)
69 // Enforce single-firing
70 locked = options.once;
72 // Execute callbacks for all pending executions,
73 // respecting firingIndex overrides and runtime changes
74 fired = firing = true;
75 for ( ; queue.length; firingIndex = -1 ) {
76 memory = queue.shift();
77 while ( ++firingIndex < list.length ) {
79 // Run callback and check for early termination
80 if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false &&
81 options.stopOnFalse ) {
83 // Jump to end and forget the data so .add doesn't re-fire
84 firingIndex = list.length;
90 // Forget the data if we're done with it
91 if ( !options.memory ) {
97 // Clean up if we're done firing for good
100 // Keep an empty list if we have data for future add calls
104 // Otherwise, this object is spent
111 // Actual Callbacks object
114 // Add a callback or a collection of callbacks to the list
118 // If we have memory from a past run, we should fire after adding
119 if ( memory && !firing ) {
120 firingIndex = list.length - 1;
121 queue.push( memory );
124 ( function add( args ) {
125 jQuery.each( args, function( _, arg ) {
126 if ( jQuery.isFunction( arg ) ) {
127 if ( !options.unique || !self.has( arg ) ) {
130 } else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
132 // Inspect recursively
138 if ( memory && !firing ) {
145 // Remove a callback from the list
147 jQuery.each( arguments, function( _, arg ) {
149 while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
150 list.splice( index, 1 );
152 // Handle firing indexes
153 if ( index <= firingIndex ) {
161 // Check if a given callback is in the list.
162 // If no argument is given, return whether or not list has callbacks attached.
163 has: function( fn ) {
165 jQuery.inArray( fn, list ) > -1 :
169 // Remove all callbacks from the list
177 // Disable .fire and .add
178 // Abort any current/pending executions
179 // Clear all callbacks and values
180 disable: function() {
185 disabled: function() {
190 // Also disable .add unless we have memory (since it would have no effect)
191 // Abort any pending executions
203 // Call all callbacks with the given context and arguments
204 fireWith: function( context, args ) {
207 args = [ context, args.slice ? args.slice() : args ];
216 // Call all the callbacks with the given arguments
218 self.fireWith( this, arguments );
222 // To know if the callbacks have already been called at least once