-
Notifications
You must be signed in to change notification settings - Fork 88
/
Deferred.js
320 lines (284 loc) · 9.57 KB
/
Deferred.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
define([
"./has",
"./_base/lang",
"./errors/CancelError",
"./promise/Promise",
"./has!config-deferredInstrumentation?./promise/instrumentation"
], function(has, lang, CancelError, Promise, instrumentation){
"use strict";
// module:
// dojo/Deferred
var PROGRESS = 0,
RESOLVED = 1,
REJECTED = 2;
var FULFILLED_ERROR_MESSAGE = "This deferred has already been fulfilled.";
var freezeObject = Object.freeze || function(){};
var signalWaiting = function(waiting, type, result, rejection, deferred){
if(has("config-deferredInstrumentation")){
if(type === REJECTED && Deferred.instrumentRejected && waiting.length === 0){
Deferred.instrumentRejected(result, false, rejection, deferred);
}
}
for(var i = 0; i < waiting.length; i++){
signalListener(waiting[i], type, result, rejection);
}
};
var signalListener = function(listener, type, result, rejection){
var func = listener[type];
var deferred = listener.deferred;
if(func){
try{
var newResult = func(result);
if(type === PROGRESS){
if(typeof newResult !== "undefined"){
signalDeferred(deferred, type, newResult);
}
}else{
if(newResult && typeof newResult.then === "function"){
listener.cancel = newResult.cancel;
newResult.then(
// Only make resolvers if they're actually going to be used
makeDeferredSignaler(deferred, RESOLVED),
makeDeferredSignaler(deferred, REJECTED),
makeDeferredSignaler(deferred, PROGRESS));
return;
}
signalDeferred(deferred, RESOLVED, newResult);
}
}catch(error){
signalDeferred(deferred, REJECTED, error);
}
}else{
signalDeferred(deferred, type, result);
}
if(has("config-deferredInstrumentation")){
if(type === REJECTED && Deferred.instrumentRejected){
Deferred.instrumentRejected(result, !!func, rejection, deferred.promise);
}
}
};
var makeDeferredSignaler = function(deferred, type){
return function(value){
signalDeferred(deferred, type, value);
};
};
var signalDeferred = function(deferred, type, result){
if(!deferred.isCanceled()){
switch(type){
case PROGRESS:
deferred.progress(result);
break;
case RESOLVED:
deferred.resolve(result);
break;
case REJECTED:
deferred.reject(result);
break;
}
}
};
var Deferred = function(canceler){
// summary:
// Creates a new deferred. This API is preferred over
// `dojo/_base/Deferred`.
// description:
// Creates a new deferred, as an abstraction over (primarily)
// asynchronous operations. The deferred is the private interface
// that should not be returned to calling code. That's what the
// `promise` is for. See `dojo/promise/Promise`.
// canceler: Function?
// Will be invoked if the deferred is canceled. The canceler
// receives the reason the deferred was canceled as its argument.
// The deferred is rejected with its return value, or a new
// `dojo/errors/CancelError` instance.
// promise: dojo/promise/Promise
// The public promise object that clients can add callbacks to.
var promise = this.promise = new Promise();
var deferred = this;
var fulfilled, result, rejection;
var canceled = false;
var waiting = [];
if(has("config-deferredInstrumentation") && Error.captureStackTrace){
Error.captureStackTrace(deferred, Deferred);
Error.captureStackTrace(promise, Deferred);
}
this.isResolved = promise.isResolved = function(){
// summary:
// Checks whether the deferred has been resolved.
// returns: Boolean
return fulfilled === RESOLVED;
};
this.isRejected = promise.isRejected = function(){
// summary:
// Checks whether the deferred has been rejected.
// returns: Boolean
return fulfilled === REJECTED;
};
this.isFulfilled = promise.isFulfilled = function(){
// summary:
// Checks whether the deferred has been resolved or rejected.
// returns: Boolean
return !!fulfilled;
};
this.isCanceled = promise.isCanceled = function(){
// summary:
// Checks whether the deferred has been canceled.
// returns: Boolean
return canceled;
};
this.progress = function(update, strict){
// summary:
// Emit a progress update on the deferred.
// description:
// Emit a progress update on the deferred. Progress updates
// can be used to communicate updates about the asynchronous
// operation before it has finished.
// update: any
// The progress update. Passed to progbacks.
// strict: Boolean?
// If strict, will throw an error if the deferred has already
// been fulfilled and consequently no progress can be emitted.
// returns: dojo/promise/Promise
// Returns the original promise for the deferred.
if(!fulfilled){
signalWaiting(waiting, PROGRESS, update, null, deferred);
return promise;
}else if(strict === true){
throw new Error(FULFILLED_ERROR_MESSAGE);
}else{
return promise;
}
};
this.resolve = function(value, strict){
// summary:
// Resolve the deferred.
// description:
// Resolve the deferred, putting it in a success state.
// value: any
// The result of the deferred. Passed to callbacks.
// strict: Boolean?
// If strict, will throw an error if the deferred has already
// been fulfilled and consequently cannot be resolved.
// returns: dojo/promise/Promise
// Returns the original promise for the deferred.
if(!fulfilled){
// Set fulfilled, store value. After signaling waiting listeners unset
// waiting.
signalWaiting(waiting, fulfilled = RESOLVED, result = value, null, deferred);
waiting = null;
return promise;
}else if(strict === true){
throw new Error(FULFILLED_ERROR_MESSAGE);
}else{
return promise;
}
};
var reject = this.reject = function(error, strict){
// summary:
// Reject the deferred.
// description:
// Reject the deferred, putting it in an error state.
// error: any
// The error result of the deferred. Passed to errbacks.
// strict: Boolean?
// If strict, will throw an error if the deferred has already
// been fulfilled and consequently cannot be rejected.
// returns: dojo/promise/Promise
// Returns the original promise for the deferred.
if(!fulfilled){
if(has("config-deferredInstrumentation") && Error.captureStackTrace){
Error.captureStackTrace(rejection = {}, reject);
}
signalWaiting(waiting, fulfilled = REJECTED, result = error, rejection, deferred);
waiting = null;
return promise;
}else if(strict === true){
throw new Error(FULFILLED_ERROR_MESSAGE);
}else{
return promise;
}
};
this.then = promise.then = function(callback, errback, progback){
// summary:
// Add new callbacks to the deferred.
// description:
// Add new callbacks to the deferred. Callbacks can be added
// before or after the deferred is fulfilled.
// callback: Function?
// Callback to be invoked when the promise is resolved.
// Receives the resolution value.
// errback: Function?
// Callback to be invoked when the promise is rejected.
// Receives the rejection error.
// progback: Function?
// Callback to be invoked when the promise emits a progress
// update. Receives the progress update.
// returns: dojo/promise/Promise
// Returns a new promise for the result of the callback(s).
// This can be used for chaining many asynchronous operations.
var listener = [progback, callback, errback];
// Ensure we cancel the promise we're waiting for, or if callback/errback
// have returned a promise, cancel that one.
listener.cancel = promise.cancel;
listener.deferred = new Deferred(function(reason){
// Check whether cancel is really available, returned promises are not
// required to expose `cancel`
return listener.cancel && listener.cancel(reason);
});
if(fulfilled && !waiting){
signalListener(listener, fulfilled, result, rejection);
}else{
waiting.push(listener);
}
return listener.deferred.promise;
};
this.cancel = promise.cancel = function(reason, strict){
// summary:
// Inform the deferred it may cancel its asynchronous operation.
// description:
// Inform the deferred it may cancel its asynchronous operation.
// The deferred's (optional) canceler is invoked and the
// deferred will be left in a rejected state. Can affect other
// promises that originate with the same deferred.
// reason: any
// A message that may be sent to the deferred's canceler,
// explaining why it's being canceled.
// strict: Boolean?
// If strict, will throw an error if the deferred has already
// been fulfilled and consequently cannot be canceled.
// returns: any
// Returns the rejection reason if the deferred was canceled
// normally.
if(!fulfilled){
// Cancel can be called even after the deferred is fulfilled
if(canceler){
var returnedReason = canceler(reason);
reason = typeof returnedReason === "undefined" ? reason : returnedReason;
}
canceled = true;
if(!fulfilled){
// Allow canceler to provide its own reason, but fall back to a CancelError
if(typeof reason === "undefined"){
reason = new CancelError();
}
reject(reason);
return reason;
}else if(fulfilled === REJECTED && result === reason){
return reason;
}
}else if(strict === true){
throw new Error(FULFILLED_ERROR_MESSAGE);
}
};
freezeObject(promise);
};
Deferred.prototype.toString = function(){
// returns: String
// Returns `[object Deferred]`.
return "[object Deferred]";
};
if(instrumentation){
instrumentation(Deferred);
}
return Deferred;
});