Blame view

bower_components/jquery/src/deferred.js 4.38 KB
a1a3bc73   Luigi Serra   graphs updates
1
  define( [
74249687   Luigi Serra   Cross browser con...
2
3
4
5
6
  	"./core",
  	"./var/slice",
  	"./callbacks"
  ], function( jQuery, slice ) {
  
a1a3bc73   Luigi Serra   graphs updates
7
  jQuery.extend( {
74249687   Luigi Serra   Cross browser con...
8
9
10
  
  	Deferred: function( func ) {
  		var tuples = [
a1a3bc73   Luigi Serra   graphs updates
11
  
74249687   Luigi Serra   Cross browser con...
12
  				// action, add listener, listener list, final state
a1a3bc73   Luigi Serra   graphs updates
13
14
15
  				[ "resolve", "done", jQuery.Callbacks( "once memory" ), "resolved" ],
  				[ "reject", "fail", jQuery.Callbacks( "once memory" ), "rejected" ],
  				[ "notify", "progress", jQuery.Callbacks( "memory" ) ]
74249687   Luigi Serra   Cross browser con...
16
17
18
19
20
21
22
23
24
25
26
27
  			],
  			state = "pending",
  			promise = {
  				state: function() {
  					return state;
  				},
  				always: function() {
  					deferred.done( arguments ).fail( arguments );
  					return this;
  				},
  				then: function( /* fnDone, fnFail, fnProgress */ ) {
  					var fns = arguments;
a1a3bc73   Luigi Serra   graphs updates
28
  					return jQuery.Deferred( function( newDefer ) {
74249687   Luigi Serra   Cross browser con...
29
30
  						jQuery.each( tuples, function( i, tuple ) {
  							var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
a1a3bc73   Luigi Serra   graphs updates
31
  
74249687   Luigi Serra   Cross browser con...
32
  							// deferred[ done | fail | progress ] for forwarding actions to newDefer
a1a3bc73   Luigi Serra   graphs updates
33
  							deferred[ tuple[ 1 ] ]( function() {
74249687   Luigi Serra   Cross browser con...
34
35
36
  								var returned = fn && fn.apply( this, arguments );
  								if ( returned && jQuery.isFunction( returned.promise ) ) {
  									returned.promise()
a1a3bc73   Luigi Serra   graphs updates
37
  										.progress( newDefer.notify )
74249687   Luigi Serra   Cross browser con...
38
  										.done( newDefer.resolve )
a1a3bc73   Luigi Serra   graphs updates
39
  										.fail( newDefer.reject );
74249687   Luigi Serra   Cross browser con...
40
  								} else {
a1a3bc73   Luigi Serra   graphs updates
41
42
43
44
  									newDefer[ tuple[ 0 ] + "With" ](
  										this === promise ? newDefer.promise() : this,
  										fn ? [ returned ] : arguments
  									);
74249687   Luigi Serra   Cross browser con...
45
  								}
a1a3bc73   Luigi Serra   graphs updates
46
47
  							} );
  						} );
74249687   Luigi Serra   Cross browser con...
48
  						fns = null;
a1a3bc73   Luigi Serra   graphs updates
49
  					} ).promise();
74249687   Luigi Serra   Cross browser con...
50
  				},
a1a3bc73   Luigi Serra   graphs updates
51
  
74249687   Luigi Serra   Cross browser con...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  				// Get a promise for this deferred
  				// If obj is provided, the promise aspect is added to the object
  				promise: function( obj ) {
  					return obj != null ? jQuery.extend( obj, promise ) : promise;
  				}
  			},
  			deferred = {};
  
  		// Keep pipe for back-compat
  		promise.pipe = promise.then;
  
  		// Add list-specific methods
  		jQuery.each( tuples, function( i, tuple ) {
  			var list = tuple[ 2 ],
  				stateString = tuple[ 3 ];
  
  			// promise[ done | fail | progress ] = list.add
a1a3bc73   Luigi Serra   graphs updates
69
  			promise[ tuple[ 1 ] ] = list.add;
74249687   Luigi Serra   Cross browser con...
70
71
72
  
  			// Handle state
  			if ( stateString ) {
a1a3bc73   Luigi Serra   graphs updates
73
74
  				list.add( function() {
  
74249687   Luigi Serra   Cross browser con...
75
76
77
78
79
80
81
82
  					// state = [ resolved | rejected ]
  					state = stateString;
  
  				// [ reject_list | resolve_list ].disable; progress_list.lock
  				}, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
  			}
  
  			// deferred[ resolve | reject | notify ]
a1a3bc73   Luigi Serra   graphs updates
83
84
  			deferred[ tuple[ 0 ] ] = function() {
  				deferred[ tuple[ 0 ] + "With" ]( this === deferred ? promise : this, arguments );
74249687   Luigi Serra   Cross browser con...
85
86
  				return this;
  			};
a1a3bc73   Luigi Serra   graphs updates
87
88
  			deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
  		} );
74249687   Luigi Serra   Cross browser con...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  
  		// Make the deferred a promise
  		promise.promise( deferred );
  
  		// Call given func if any
  		if ( func ) {
  			func.call( deferred, deferred );
  		}
  
  		// All done!
  		return deferred;
  	},
  
  	// Deferred helper
  	when: function( subordinate /* , ..., subordinateN */ ) {
  		var i = 0,
  			resolveValues = slice.call( arguments ),
  			length = resolveValues.length,
  
  			// the count of uncompleted subordinates
a1a3bc73   Luigi Serra   graphs updates
109
110
  			remaining = length !== 1 ||
  				( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
74249687   Luigi Serra   Cross browser con...
111
  
a1a3bc73   Luigi Serra   graphs updates
112
113
  			// the master Deferred.
  			// If resolveValues consist of only a single Deferred, just use that.
74249687   Luigi Serra   Cross browser con...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  			deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
  
  			// Update function for both resolve and progress values
  			updateFunc = function( i, contexts, values ) {
  				return function( value ) {
  					contexts[ i ] = this;
  					values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
  					if ( values === progressValues ) {
  						deferred.notifyWith( contexts, values );
  					} else if ( !( --remaining ) ) {
  						deferred.resolveWith( contexts, values );
  					}
  				};
  			},
  
  			progressValues, progressContexts, resolveContexts;
  
  		// Add listeners to Deferred subordinates; treat others as resolved
  		if ( length > 1 ) {
  			progressValues = new Array( length );
  			progressContexts = new Array( length );
  			resolveContexts = new Array( length );
  			for ( ; i < length; i++ ) {
  				if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
  					resolveValues[ i ].promise()
a1a3bc73   Luigi Serra   graphs updates
139
  						.progress( updateFunc( i, progressContexts, progressValues ) )
74249687   Luigi Serra   Cross browser con...
140
  						.done( updateFunc( i, resolveContexts, resolveValues ) )
a1a3bc73   Luigi Serra   graphs updates
141
  						.fail( deferred.reject );
74249687   Luigi Serra   Cross browser con...
142
143
144
145
146
147
148
149
150
151
152
153
154
  				} else {
  					--remaining;
  				}
  			}
  		}
  
  		// If we're not waiting on anything, resolve the master
  		if ( !remaining ) {
  			deferred.resolveWith( resolveContexts, resolveValues );
  		}
  
  		return deferred.promise();
  	}
a1a3bc73   Luigi Serra   graphs updates
155
  } );
74249687   Luigi Serra   Cross browser con...
156
157
  
  return jQuery;
a1a3bc73   Luigi Serra   graphs updates
158
  } );