forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync.js
More file actions
507 lines (451 loc) · 19.2 KB
/
Async.js
File metadata and controls
507 lines (451 loc) · 19.2 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, window */
/**
* Utilities for working with Deferred, Promise, and other asynchronous processes.
*/
define(function (require, exports, module) {
"use strict";
// Further ideas for Async utilities...
// - Utilities for blocking UI until a Promise completes?
// - A "SuperDeferred" could feature some very useful enhancements:
// - API for cancellation (non guaranteed, best attempt)
// - Easier way to add a timeout clause (withTimeout() wrapper below is more verbose)
// - Encapsulate the task kickoff code so you can start it later, e.g. superDeferred.start()
// - Deferred/Promise are unable to do anything akin to a 'finally' block. It'd be nice if we
// could harvest exceptions across all steps of an async process and pipe them to a handler,
// so that we don't leave UI-blocking overlays up forever, etc. But this is hard: we'd have
// wrap every async callback (including low-level native ones that don't use [Super]Deferred)
// to catch exceptions, and then understand which Deferred(s) the code *would* have resolved/
// rejected had it run to completion.
/**
* Executes a series of tasks in parallel, returning a "master" Promise that is resolved once
* all the tasks have resolved. If one or more tasks fail, behavior depends on the failFast
* flag:
* - If true, the master Promise is rejected as soon as the first task fails. The remaining
* tasks continue to completion in the background.
* - If false, the master Promise is rejected after all tasks have completed.
*
* If nothing fails: (M = master promise; 1-4 = tasks; d = done; F = fail)
* M ------------d
* 1 >---d .
* 2 >------d .
* 3 >---------d .
* 4 >------------d
*
* With failFast = false:
* M ------------F
* 1 >---d . .
* 2 >------d . .
* 3 >---------F .
* 4 >------------d
*
* With failFast = true: -- equivalent to $.when()
* M ---------F
* 1 >---d .
* 2 >------d .
* 3 >---------F
* 4 >------------d (#4 continues even though master Promise has failed)
* (Note: if tasks finish synchronously, the behavior is more like failFast=false because you
* won't get a chance to respond to the master Promise until after all items have been processed)
*
* To perform task-specific work after an individual task completes, attach handlers to each
* Promise before beginProcessItem() returns it.
*
* Note: don't use this if individual tasks (or their done/fail handlers) could ever show a user-
* visible dialog: because they run in parallel, you could show multiple dialogs atop each other.
*
* @param {!Array.<*>} items
* @param {!function(*, number):Promise} beginProcessItem
* @param {!boolean} failFast
* @return {$.Promise}
*/
function doInParallel(items, beginProcessItem, failFast) {
var promises = [];
var masterDeferred = new $.Deferred();
if (items.length === 0) {
masterDeferred.resolve();
} else {
var numCompleted = 0;
var hasFailed = false;
items.forEach(function (item, i) {
var itemPromise = beginProcessItem(item, i);
promises.push(itemPromise);
itemPromise.fail(function () {
if (failFast) {
masterDeferred.reject();
} else {
hasFailed = true;
}
});
itemPromise.always(function () {
numCompleted++;
if (numCompleted === items.length) {
if (hasFailed) {
masterDeferred.reject();
} else {
masterDeferred.resolve();
}
}
});
});
}
return masterDeferred.promise();
}
/**
* Executes a series of tasks in serial (task N does not begin until task N-1 has completed).
* Returns a "master" Promise that is resolved once all the tasks have resolved. If one or more
* tasks fail, behavior depends on the failAndStopFast flag:
* - If true, the master Promise is rejected as soon as the first task fails. The remaining
* tasks are never started (the serial sequence is stopped).
* - If false, the master Promise is rejected after all tasks have completed.
*
* If nothing fails:
* M ------------d
* 1 >---d .
* 2 >--d .
* 3 >--d .
* 4 >--d
*
* With failAndStopFast = false:
* M ------------F
* 1 >---d . .
* 2 >--d . .
* 3 >--F .
* 4 >--d
*
* With failAndStopFast = true:
* M ---------F
* 1 >---d .
* 2 >--d .
* 3 >--F
* 4 (#4 never runs)
*
* To perform task-specific work after an individual task completes, attach handlers to each
* Promise before beginProcessItem() returns it.
*
* @param {!Array.<*>} items
* @param {!function(*, number):Promise} beginProcessItem
* @param {!boolean} failAndStopFast
* @return {$.Promise}
*/
function doSequentially(items, beginProcessItem, failAndStopFast) {
var masterDeferred = new $.Deferred(),
hasFailed = false;
function doItem(i) {
if (i >= items.length) {
if (hasFailed) {
masterDeferred.reject();
} else {
masterDeferred.resolve();
}
return;
}
var itemPromise = beginProcessItem(items[i], i);
itemPromise.done(function () {
doItem(i + 1);
});
itemPromise.fail(function () {
if (failAndStopFast) {
masterDeferred.reject();
// note: we do NOT process any further items in this case
} else {
hasFailed = true;
doItem(i + 1);
}
});
}
doItem(0);
return masterDeferred.promise();
}
/**
* Executes a series of synchronous tasks sequentially spread over time-slices less than maxBlockingTime.
* Processing yields by idleTime between time-slices.
*
* @param {!Array.<*>} items
* @param {!function(*, number)} fnProcessItem Function that synchronously processes one item
* @param {number=} maxBlockingTime
* @param {number=} idleTime
* @return {$.Promise}
*/
function doSequentiallyInBackground(items, fnProcessItem, maxBlockingTime, idleTime) {
maxBlockingTime = maxBlockingTime || 15;
idleTime = idleTime || 30;
var sliceStartTime = (new Date()).getTime();
return doSequentially(items, function (item, i) {
var result = new $.Deferred();
// process the next item
fnProcessItem(item, i);
// if we've exhausted our maxBlockingTime
if ((new Date()).getTime() - sliceStartTime >= maxBlockingTime) {
//yield
window.setTimeout(function () {
sliceStartTime = (new Date()).getTime();
result.resolve();
}, idleTime);
} else {
//continue processing
result.resolve();
}
return result;
}, false);
}
/**
* Executes a series of tasks in parallel, saving up error info from any that fail along the way.
* Returns a Promise that is only resolved/rejected once all tasks are complete. This is
* essentially a wrapper around doInParallel(..., false).
*
* If one or more tasks failed, the entire "master" promise is rejected at the end - with one
* argument: an array objects, one per failed task. Each error object contains:
* - item -- the entry in items whose task failed
* - error -- the first argument passed to the fail() handler when the task failed
*
* @param {!Array.<*>} items
* @param {!function(*, number):Promise} beginProcessItem
* @return {$.Promise}
*/
function doInParallel_aggregateErrors(items, beginProcessItem) {
var errors = [];
var masterDeferred = new $.Deferred();
var parallelResult = doInParallel(
items,
function (item, i) {
var itemResult = beginProcessItem(item, i);
itemResult.fail(function (error) {
errors.push({ item: item, error: error });
});
return itemResult;
},
false
);
parallelResult
.done(function () {
masterDeferred.resolve();
})
.fail(function () {
masterDeferred.reject(errors);
});
return masterDeferred.promise();
}
/** Value passed to fail() handlers that have been triggered due to withTimeout()'s timeout */
var ERROR_TIMEOUT = {};
/**
* Adds timeout-driven failure to a Promise: returns a new Promise that is resolved/rejected when
* the given original Promise is resolved/rejected, OR is rejected after the given delay - whichever
* happens first.
*
* If the original Promise is resolved/rejected first, done()/fail() handlers receive arguments
* piped from the original Promise. If the timeout occurs first instead, fail() is called with the
* token Async.ERROR_TIMEOUT.
*
* @param {$.Promise} promise
* @param {number} timeout
* @return {$.Promise}
*/
function withTimeout(promise, timeout) {
var wrapper = new $.Deferred();
var timer = window.setTimeout(function () {
wrapper.reject(ERROR_TIMEOUT);
}, timeout);
promise.always(function () {
window.clearTimeout(timer);
});
// If the wrapper was already rejected due to timeout, the Promise's calls to resolve/reject
// won't do anything
promise.then(wrapper.resolve, wrapper.reject);
return wrapper.promise();
}
/**
* Allows waiting for all the promises to be either resolved or rejected.
* Unlike $.when(), it does not call .fail() or .always() handlers on first
* reject. The caller should take all the precaution to make sure all the
* promises passed to this function are completed to avoid blocking.
*
* If failOnReject is set to true, promise returned by the function will be
* rejected if at least one of the promises was rejected. The default value
* is false, which will cause the call to this function to be always
* successfully resolved.
*
* If timeout is specified, the promise will be rejected on timeout as per
* Async.withTimeout.
*
* @param {!Array.<$.Promise>} promises Array of promises to wait for
* @param {boolean=} failOnReject Whether to reject or not if one of the promises has been rejected.
* @param {number=} timeout Number of milliseconds to wait until rejecting the promise
*
* @return {$.Promise} Promise which will be completed once al the
*
*/
function waitForAll(promises, failOnReject, timeout) {
var masterDeferred = new $.Deferred(),
count = 0,
sawRejects = false;
if (!promises || promises.length === 0) {
masterDeferred.resolve();
return masterDeferred.promise();
}
// set defaults if needed
failOnReject = (failOnReject === undefined) ? false : true;
if (timeout !== undefined) {
withTimeout(masterDeferred, timeout);
}
promises.forEach(function (promise) {
promise
.fail(function (err) {
sawRejects = true;
})
.always(function () {
count++;
if (count === promises.length) {
if (failOnReject && sawRejects) {
masterDeferred.reject();
} else {
masterDeferred.resolve();
}
}
});
});
return masterDeferred.promise();
}
/**
* Chains a series of synchronous and asynchronous (jQuery promise-returning) functions
* together, using the result of each successive function as the argument(s) to the next.
* A promise is returned that resolves with the result of the final call if all calls
* resolve or return normally. Otherwise, if any of the functions reject or throw, the
* computation is halted immediately and the promise is rejected with this halting error.
*
* @param {Array.<function(*)>} functions Functions to be chained
* @param {?Array} args Arguments to call the first function with
* @return {jQuery.Promise} A promise that resolves with the result of the final call, or
* rejects with the first error.
*/
function chain(functions, args) {
var deferred = $.Deferred();
function chainHelper(index, args) {
if (functions.length === index) {
deferred.resolveWith(null, args);
} else {
var nextFunction = functions[index++];
try {
var responseOrPromise = nextFunction.apply(null, args);
if (responseOrPromise.hasOwnProperty("done") &&
responseOrPromise.hasOwnProperty("fail")) {
responseOrPromise.done(function () {
chainHelper(index, arguments);
});
responseOrPromise.fail(function () {
deferred.rejectWith(null, arguments);
});
} else {
chainHelper(index, [responseOrPromise]);
}
} catch (e) {
deferred.reject(e);
}
}
}
chainHelper(0, args || []);
return deferred.promise();
}
/**
* @constructor
* Creates a queue of async operations that will be executed sequentially. Operations can be added to the
* queue at any time. If the queue is empty and nothing is currently executing when an operation is added,
* it will execute immediately. Otherwise, it will execute when the last operation currently in the queue
* has finished.
*/
function PromiseQueue() {
}
/**
* @private
* @type {Array.<function(): $.Promise>}
* The queue of operations to execute sequentially. Note that even if this array is empty, there might
* still be an operation we need to wait on; that operation's promise is stored in _curPromise.
*/
PromiseQueue.prototype._queue = [];
/**
* @private
* @type {$.Promise}
* The promise we're currently waiting on, or null if there's nothing currently executing.
*/
PromiseQueue.prototype._curPromise = null;
/**
* @type {number} The number of queued promises.
*/
Object.defineProperties(PromiseQueue.prototype, {
"length": {
get: function () { return this._queue.length; },
set: function () { throw new Error("Cannot set length"); }
}
});
/**
* Adds an operation to the queue. If nothing is currently executing, it will execute immediately (and
* the next operation added to the queue will wait for it to complete). Otherwise, it will wait until
* the last operation in the queue (or the currently executing operation if nothing is in the queue) is
* finished. The operation must return a promise that will be resolved or rejected when it's finished;
* the queue will continue with the next operation regardless of whether the current operation's promise
* is resolved or rejected.
* @param {function(): $.Promise} op The operation to add to the queue.
*/
PromiseQueue.prototype.add = function (op) {
this._queue.push(op);
// If something is currently executing, then _doNext() will get called when it's done. If nothing
// is executing (in which case the queue should have been empty), we need to call _doNext() to kickstart
// the queue.
if (!this._curPromise) {
this._doNext();
}
};
/**
* Removes all pending promises from the queue.
*/
PromiseQueue.prototype.removeAll = function () {
this._queue = [];
};
/**
* @private
* Pulls the next operation off the queue and executes it.
*/
PromiseQueue.prototype._doNext = function () {
var self = this;
if (this._queue.length) {
var op = this._queue.shift();
this._curPromise = op();
this._curPromise.always(function () {
self._curPromise = null;
self._doNext();
});
}
};
// Define public API
exports.doInParallel = doInParallel;
exports.doSequentially = doSequentially;
exports.doSequentiallyInBackground = doSequentiallyInBackground;
exports.doInParallel_aggregateErrors = doInParallel_aggregateErrors;
exports.withTimeout = withTimeout;
exports.waitForAll = waitForAll;
exports.ERROR_TIMEOUT = ERROR_TIMEOUT;
exports.chain = chain;
exports.PromiseQueue = PromiseQueue;
});