Blame view

bower_components/jquery/src/deferred.js 4.31 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
3
4
5
6
  	"./core",
  	"./var/slice",
  	"./callbacks"
  ], function( jQuery, slice ) {
  
c5169e0e   Renato De Donato   a new hope
7
  jQuery.extend({
74249687   Luigi Serra   Cross browser con...
8
9
10
  
  	Deferred: function( func ) {
  		var tuples = [
74249687   Luigi Serra   Cross browser con...
11
  				// action, add listener, listener list, final state
c5169e0e   Renato De Donato   a new hope
12
13
14
  				[ "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...
15
16
17
18
19
20
21
22
23
24
25
26
  			],
  			state = "pending",
  			promise = {
  				state: function() {
  					return state;
  				},
  				always: function() {
  					deferred.done( arguments ).fail( arguments );
  					return this;
  				},
  				then: function( /* fnDone, fnFail, fnProgress */ ) {
  					var fns = arguments;
c5169e0e   Renato De Donato   a new hope
27
  					return jQuery.Deferred(function( newDefer ) {
74249687   Luigi Serra   Cross browser con...
28
29
  						jQuery.each( tuples, function( i, tuple ) {
  							var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
74249687   Luigi Serra   Cross browser con...
30
  							// deferred[ done | fail | progress ] for forwarding actions to newDefer
c5169e0e   Renato De Donato   a new hope
31
  							deferred[ tuple[1] ](function() {
74249687   Luigi Serra   Cross browser con...
32
33
34
  								var returned = fn && fn.apply( this, arguments );
  								if ( returned && jQuery.isFunction( returned.promise ) ) {
  									returned.promise()
74249687   Luigi Serra   Cross browser con...
35
  										.done( newDefer.resolve )
c5169e0e   Renato De Donato   a new hope
36
37
  										.fail( newDefer.reject )
  										.progress( newDefer.notify );
74249687   Luigi Serra   Cross browser con...
38
  								} else {
c5169e0e   Renato De Donato   a new hope
39
  									newDefer[ tuple[ 0 ] + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments );
74249687   Luigi Serra   Cross browser con...
40
  								}
c5169e0e   Renato De Donato   a new hope
41
42
  							});
  						});
74249687   Luigi Serra   Cross browser con...
43
  						fns = null;
c5169e0e   Renato De Donato   a new hope
44
  					}).promise();
74249687   Luigi Serra   Cross browser con...
45
  				},
74249687   Luigi Serra   Cross browser con...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  				// 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
c5169e0e   Renato De Donato   a new hope
63
  			promise[ tuple[1] ] = list.add;
74249687   Luigi Serra   Cross browser con...
64
65
66
  
  			// Handle state
  			if ( stateString ) {
c5169e0e   Renato De Donato   a new hope
67
  				list.add(function() {
74249687   Luigi Serra   Cross browser con...
68
69
70
71
72
73
74
75
  					// 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 ]
c5169e0e   Renato De Donato   a new hope
76
77
  			deferred[ tuple[0] ] = function() {
  				deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
74249687   Luigi Serra   Cross browser con...
78
79
  				return this;
  			};
c5169e0e   Renato De Donato   a new hope
80
81
  			deferred[ tuple[0] + "With" ] = list.fireWith;
  		});
74249687   Luigi Serra   Cross browser con...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  
  		// 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
c5169e0e   Renato De Donato   a new hope
102
  			remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
74249687   Luigi Serra   Cross browser con...
103
  
c5169e0e   Renato De Donato   a new hope
104
  			// the master Deferred. If resolveValues consist of only a single Deferred, just use that.
74249687   Luigi Serra   Cross browser con...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  			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()
74249687   Luigi Serra   Cross browser con...
130
  						.done( updateFunc( i, resolveContexts, resolveValues ) )
c5169e0e   Renato De Donato   a new hope
131
132
  						.fail( deferred.reject )
  						.progress( updateFunc( i, progressContexts, progressValues ) );
74249687   Luigi Serra   Cross browser con...
133
134
135
136
137
138
139
140
141
142
143
144
145
  				} else {
  					--remaining;
  				}
  			}
  		}
  
  		// If we're not waiting on anything, resolve the master
  		if ( !remaining ) {
  			deferred.resolveWith( resolveContexts, resolveValues );
  		}
  
  		return deferred.promise();
  	}
c5169e0e   Renato De Donato   a new hope
146
  });
74249687   Luigi Serra   Cross browser con...
147
148
  
  return jQuery;
c5169e0e   Renato De Donato   a new hope
149
  });