4 ], function( jQuery, rnothtmlwhite ) {
8 // Convert String-formatted options into Object-formatted ones
9 function createOptions( options ) {
11 jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
12 object[ flag ] = true;
18 * Create a callback list using the following parameters:
20 * options: an optional list of space-separated options that will change how
21 * the callback list behaves or a more traditional option object
23 * By default a callback list will act like an event callback list and can be
24 * "fired" multiple times.
28 * once: will ensure the callback list can only be fired once (like a Deferred)
30 * memory: will keep track of previous values and will call any callback added
31 * after the list has been fired right away with the latest "memorized"
32 * values (like a Deferred)
34 * unique: will ensure a callback can only be added once (no duplicate in the list)
36 * stopOnFalse: interrupt callings when a callback returns false
39 jQuery.Callbacks = function( options ) {
41 // Convert options from String-formatted to Object-formatted if needed
42 // (we check in cache first)
43 options = typeof options === "string" ?
44 createOptions( options ) :
45 jQuery.extend( {}, options );
47 var // Flag to know if list is currently firing
50 // Last fire value for non-forgettable lists
53 // Flag to know if list was already fired
56 // Flag to prevent firing
59 // Actual callback list
62 // Queue of execution data for repeatable lists
65 // Index of currently firing callback (modified by add/remove as needed)
71 // Enforce single-firing
72 locked = locked || options.once;
74 // Execute callbacks for all pending executions,
75 // respecting firingIndex overrides and runtime changes
76 fired = firing = true;
77 for ( ; queue.length; firingIndex = -1 ) {
78 memory = queue.shift();
79 while ( ++firingIndex < list.length ) {
81 // Run callback and check for early termination
82 if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false &&
83 options.stopOnFalse ) {
85 // Jump to end and forget the data so .add doesn't re-fire
86 firingIndex = list.length;
92 // Forget the data if we're done with it
93 if ( !options.memory ) {
99 // Clean up if we're done firing for good
102 // Keep an empty list if we have data for future add calls
106 // Otherwise, this object is spent
113 // Actual Callbacks object
116 // Add a callback or a collection of callbacks to the list
120 // If we have memory from a past run, we should fire after adding
121 if ( memory && !firing ) {
122 firingIndex = list.length - 1;
123 queue.push( memory );
126 ( function add( args ) {
127 jQuery.each( args, function( _, arg ) {
128 if ( jQuery.isFunction( arg ) ) {
129 if ( !options.unique || !self.has( arg ) ) {
132 } else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
134 // Inspect recursively
140 if ( memory && !firing ) {
147 // Remove a callback from the list
149 jQuery.each( arguments, function( _, arg ) {
151 while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
152 list.splice( index, 1 );
154 // Handle firing indexes
155 if ( index <= firingIndex ) {
163 // Check if a given callback is in the list.
164 // If no argument is given, return whether or not list has callbacks attached.
165 has: function( fn ) {
167 jQuery.inArray( fn, list ) > -1 :
171 // Remove all callbacks from the list
179 // Disable .fire and .add
180 // Abort any current/pending executions
181 // Clear all callbacks and values
182 disable: function() {
187 disabled: function() {
192 // Also disable .add unless we have memory (since it would have no effect)
193 // Abort any pending executions
196 if ( !memory && !firing ) {
205 // Call all callbacks with the given context and arguments
206 fireWith: function( context, args ) {
209 args = [ context, args.slice ? args.slice() : args ];
218 // Call all the callbacks with the given arguments
220 self.fireWith( this, arguments );
224 // To know if the callbacks have already been called at least once