/
Deferred.js
406 lines (380 loc) · 14.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*******************************************************************************
* @license
* Copyright (c) 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
/*eslint-env browser, amd, node*/
(function(root, factory) { // UMD
if (typeof define === "function" && define.amd) { //$NON-NLS-0$
define(factory);
} else if (typeof exports === "object") { //$NON-NLS-0$
module.exports = factory();
} else {
root.orion = root.orion || {};
root.orion.Deferred = factory();
}
}(this, function() {
var queue = [],
running = false;
function run() {
var fn;
while ((fn = queue.shift())) {
fn();
}
running = false;
}
var runAsync = (function() {
if (typeof process !== "undefined" && typeof process.nextTick === "function") {
var nextTick = process.nextTick;
return function() {
nextTick(run);
};
} else if (typeof MutationObserver === "function") {
var div = document.createElement("div");
var observer = new MutationObserver(run);
observer.observe(div, {
attributes: true
});
return function() {
div.setAttribute("class", "_tick");
};
}
return function() {
setTimeout(run, 0);
};
})();
function enqueue(fn) {
queue.push(fn);
if (!running) {
running = true;
runAsync();
}
}
function noReturn(fn) {
return function(result) {
fn(result);
};
}
function settleDeferred(fn, result, deferred) {
try {
var listenerResult = fn(result);
var listenerThen = listenerResult && (typeof listenerResult === "object" || typeof listenerResult === "function") && listenerResult.then;
if (typeof listenerThen === "function") {
if (listenerResult === deferred.promise) {
deferred.reject(new TypeError());
} else {
var listenerResultCancel = listenerResult.cancel;
if (typeof listenerResultCancel === "function") {
deferred._parentCancel = listenerResultCancel.bind(listenerResult);
} else {
delete deferred._parentCancel;
}
listenerThen.call(listenerResult, noReturn(deferred.resolve), noReturn(deferred.reject), noReturn(deferred.progress));
}
} else {
deferred.resolve(listenerResult);
}
} catch (e) {
deferred.reject(e);
}
}
/**
* @name orion.Promise
* @class Interface representing an eventual value.
* @description Promise is an interface that represents an eventual value returned from the single completion of an operation.
*
* <p>For a concrete class that implements Promise and provides additional API, see {@link orion.Deferred}.</p>
* @see orion.Deferred
* @see orion.Deferred#promise
*/
/**
* @name then
* @function
* @memberOf orion.Promise.prototype
* @description Adds handlers to be called on fulfillment or progress of this promise.
* @param {Function} [onResolve] Called when this promise is resolved.
* @param {Function} [onReject] Called when this promise is rejected.
* @param {Function} [onProgress] May be called to report progress events on this promise.
* @returns {orion.Promise} A new promise that is fulfilled when the given <code>onResolve</code> or <code>onReject</code>
* callback is finished. The callback's return value gives the fulfillment value of the returned promise.
*/
/**
* Cancels this promise.
* @name cancel
* @function
* @memberOf orion.Promise.prototype
* @param {Object} reason The reason for canceling this promise.
* @param {Boolean} [strict]
*/
/**
* @name orion.Deferred
* @borrows orion.Promise#then as #then
* @borrows orion.Promise#cancel as #cancel
* @class Provides abstraction over asynchronous operations.
* @description Deferred provides abstraction over asynchronous operations.
*
* <p>Because Deferred implements the {@link orion.Promise} interface, a Deferred may be used anywhere a Promise is called for.
* However, in most such cases it is recommended to use the Deferred's {@link #promise} field instead, which exposes a
* simplified, minimally <a href="https://github.com/promises-aplus/promises-spec">Promises/A+</a>-compliant interface to callers.</p>
*/
function Deferred() {
var result, state, listeners = [],
_this = this;
function notify() {
var listener;
while ((listener = listeners.shift())) {
var deferred = listener.deferred;
var methodName = state === "fulfilled" ? "resolve" : "reject"; //$NON-NLS-0$ //$NON-NLS-1$ //$NON-NLS-2$
var fn = listener[methodName];
if (typeof fn === "function") { //$NON-NLS-0$
settleDeferred(fn, result, deferred);
} else {
deferred[methodName](result);
}
}
}
function _reject(error) {
delete _this._parentCancel;
state = "rejected";
result = error;
if (listeners.length) {
enqueue(notify);
}
}
function _resolve(value) {
function once(fn) {
return function(result) {
if (!state || state === "assumed") {
fn(result);
}
};
}
delete _this._parentCancel;
try {
var valueThen = value && (typeof value === "object" || typeof value === "function") && value.then;
if (typeof valueThen === "function") {
if (value === _this) {
_reject(new TypeError());
} else {
state = "assumed";
var valueCancel = value && value.cancel;
if (typeof valueCancel !== "function") {
var deferred = new Deferred();
value = deferred.promise;
try {
valueThen(deferred.resolve, deferred.reject, deferred.progress);
} catch (thenError) {
deferred.reject(thenError);
}
valueCancel = value.cancel;
valueThen = value.then;
}
result = value;
valueThen.call(value, once(_resolve), once(_reject));
_this._parentCancel = valueCancel.bind(value);
}
} else {
state = "fulfilled";
result = value;
if (listeners.length) {
enqueue(notify);
}
}
} catch (error) {
once(_reject)(error);
}
}
function cancel() {
var parentCancel = _this._parentCancel;
if (parentCancel) {
delete _this._parentCancel;
parentCancel();
} else if (!state) {
var cancelError = new Error("Cancel");
cancelError.name = "Cancel";
_reject(cancelError);
}
}
/**
* Resolves this Deferred.
* @name resolve
* @function
* @memberOf orion.Deferred.prototype
* @param {Object} value
* @returns {orion.Promise}
*/
this.resolve = function(value) {
if (!state) {
_resolve(value);
}
return _this;
};
/**
* Rejects this Deferred.
* @name reject
* @function
* @memberOf orion.Deferred.prototype
* @param {Object} error
* @param {Boolean} [strict]
* @returns {orion.Promise}
*/
this.reject = function(error) {
if (!state) {
_reject(error);
}
return _this;
};
/**
* Notifies listeners of progress on this Deferred.
* @name progress
* @function
* @memberOf orion.Deferred.prototype
* @param {Object} update The progress update.
* @returns {orion.Promise}
*/
this.progress = function(update) {
if (!state) {
listeners.forEach(function(listener) {
if (listener.progress) {
try {
listener.progress(update);
} catch (ignore) {
// ignore
}
}
});
}
return _this.promise;
};
this.cancel = function() {
if (_this._parentCancel) {
setTimeout(cancel, 0);
} else {
cancel();
}
return _this;
};
// Note: "then" ALWAYS returns before having onResolve or onReject called as per http://promises-aplus.github.com/promises-spec/
this.then = function(onFulfill, onReject, onProgress) {
var deferred = new Deferred();
deferred._parentCancel = _this.promise.cancel;
listeners.push({
resolve: onFulfill,
reject: onReject,
progress: onProgress,
deferred: deferred
});
if (state === "fulfilled" || state === "rejected") {
enqueue(notify);
}
return deferred.promise;
};
/**
* The promise exposed by this Deferred.
* @name promise
* @field
* @memberOf orion.Deferred.prototype
* @type orion.Promise
*/
this.promise = {
then: _this.then,
cancel: _this.cancel
};
}
/**
* Returns a promise that represents the outcome of all the input promises.
* <p>When <code>all</code> is called with a single parameter, the returned promise has <dfn>eager</dfn> semantics,
* meaning that if any input promise rejects, the returned promise immediately rejects, without waiting for the rest of the
* input promises to fulfill.</p>
*
* To obtain <dfn>lazy</dfn> semantics (meaning the returned promise waits for every input promise to fulfill), pass the
* optional parameter <code>optOnError</code>.
* @name all
* @function
* @memberOf orion.Deferred
* @static
* @param {orion.Promise[]} promises The input promises.
* @param {Function} [optOnError] Handles a rejected input promise. <code>optOnError</code> is invoked for every rejected
* input promise, and is passed the reason the input promise was rejected. <p><code>optOnError</code> can return a value, which
* allows it to act as a transformer: the return value serves as the final fulfillment value of the rejected promise in the
* results array generated by <code>all</code>.
* @returns {orion.Promise} A new promise. The returned promise is generally fulfilled to an <code>Array</code> whose elements
* give the fulfillment values of the input promises. <p>However, if an input promise rejects and eager semantics is used, the
* returned promise will instead be fulfilled to a single error value.</p>
*/
Deferred.all = function(promises, optOnError) {
var count = promises.length,
result = [],
rejected = false,
deferred = new Deferred();
deferred.then(undefined, function() {
rejected = true;
promises.forEach(function(promise) {
if (promise.cancel) {
promise.cancel();
}
});
});
function onResolve(i, value) {
if (!rejected) {
result[i] = value;
if (--count === 0) {
deferred.resolve(result);
}
}
}
function onReject(i, error) {
if (!rejected) {
if (optOnError) {
try {
onResolve(i, optOnError(error));
return;
} catch (e) {
error = e;
}
}
deferred.reject(error);
}
}
if (count === 0) {
deferred.resolve(result);
} else {
promises.forEach(function(promise, i) {
promise.then(onResolve.bind(undefined, i), onReject.bind(undefined, i));
});
}
return deferred.promise;
};
/**
* Applies callbacks to a promise or to a regular object.
* @name when
* @function
* @memberOf orion.Deferred
* @static
* @param {Object|orion.Promise} value Either a {@link orion.Promise}, or a normal value.
* @param {Function} onResolve Called when the <code>value</code> promise is resolved. If <code>value</code> is not a promise,
* this function is called immediately.
* @param {Function} onReject Called when the <code>value</code> promise is rejected. If <code>value</code> is not a promise,
* this function is never called.
* @param {Function} onProgress Called when the <code>value</code> promise provides a progress update. If <code>value</code> is
* not a promise, this function is never called.
* @returns {orion.Promise} A new promise.
*/
Deferred.when = function(value, onResolve, onReject, onProgress) {
var promise, deferred;
if (value && typeof value.then === "function") { //$NON-NLS-0$
promise = value;
} else {
deferred = new Deferred();
deferred.resolve(value);
promise = deferred.promise;
}
return promise.then(onResolve, onReject, onProgress);
};
return Deferred;
}));