forked from BalassaMarton/sequential-task-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential-task-queue.ts
More file actions
386 lines (341 loc) · 13.1 KB
/
sequential-task-queue.ts
File metadata and controls
386 lines (341 loc) · 13.1 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
/**
* Represents an object that schedules a function for asynchronous execution.
* The default implementation used by {@link SequentialTaskQueue} calls {@link setImmediate} when available,
* and {@link setTimeout} otherwise.
* @see {@link SequentialTaskQueue.defaultScheduler}
* @see {@link TaskQueueOptions.scheduler}
*/
export interface Scheduler {
/**
* Schedules a callback for asynchronous execution.
*/
schedule(callback: Function): void;
}
/**
* Object used for passing configuration options to the {@link SequentialTaskQueue} constructor.
*/
export interface SequentialTaskQueueOptions {
/**
* Assigns a name to the task queue for diagnostic purposes. The name does not need to be unique.
*/
name?: string;
/**
* Default timeout (in milliseconds) for tasks pushed to the queue. Default is 0 (no timeout).
* */
timeout?: number;
/**
* Scheduler used by the queue. Defaults to {@link SequentialTaskQueue.defaultScheduler}.
*/
scheduler?: Scheduler;
}
/**
* Options object for individual tasks.
*/
export interface TaskOptions {
/**
* Timeout for the task, in milliseconds.
* */
timeout?: number;
/**
* Arguments to pass to the task. Useful for minimalising the number of Function objects and closures created
* when pushing the same task multiple times, with different arguments.
*
* @example
* // The following code creates a single Function object and no closures:
* for (let i = 0; i < 100; i++)
* queue.push(process, {args: [i]});
* function process(n) {
* console.log(n);
* }
*/
args?: any;
}
/**
* Provides the API for querying and invoking task cancellation.
*/
export interface CancellationToken {
/**
* When `true`, indicates that the task has been cancelled.
*/
cancelled?: boolean;
/**
* An arbitrary object representing the reason of the cancellation. Can be a member of the {@link cancellationTokenReasons} object or an `Error`, etc.
*/
reason?: any;
/**
* Cancels the task for which the cancellation token was created.
* @param reason - The reason of the cancellation, see {@link CancellationToken.reason}
*/
cancel(reason?: any);
}
/**
* Standard cancellation reasons. {@link SequentialTaskQueue} sets {@link CancellationToken.reason}
* to one of these values when cancelling a task for a reason other than the user code calling
* {@link CancellationToken.cancel}.
*/
export var cancellationTokenReasons = {
/** Used when the task was cancelled in response to a call to {@link SequentialTaskQueue.cancel} */
cancel: Object.create(null),
/** Used when the task was cancelled after its timeout has passed */
timeout: Object.create(null)
}
/**
* Standard event names used by {@link SequentialTaskQueue}
*/
export var sequentialTaskQueueEvents = {
drained: "drained",
error: "error",
timeout: "timeout"
}
/**
* Promise interface with the ability to cancel.
*/
export interface CancellablePromiseLike<T> extends PromiseLike<T> {
/**
* Cancels (and consequently, rejects) the task associated with the Promise.
* @param reason - Reason of the cancellation. This value will be passed when rejecting this Promise.
*/
cancel(reason?: any): void;
}
/**
* FIFO task queue to run tasks in predictable order, without concurrency.
*/
export class SequentialTaskQueue {
static defaultScheduler: Scheduler = {
schedule: callback => setTimeout(<any>callback, 0)
};
private queue: TaskEntry[] = [];
private _isClosed: boolean = false;
private waiters: Function[] = [];
private defaultTimeout: number;
private currentTask: TaskEntry;
private scheduler: Scheduler;
private events: { [key: string]: Function[] };
name: string;
/** Indicates if the queue has been closed. Calling {@link SequentialTaskQueue.push} on a closed queue will result in an exception. */
get isClosed() {
return this._isClosed;
}
/**
* Creates a new instance of {@link SequentialTaskQueue}
* @param options - Configuration options for the task queue.
*/
constructor(options?: SequentialTaskQueueOptions) {
if (!options)
options = {};
this.defaultTimeout = options.timeout;
this.name = options.name || "SequentialTaskQueue";
this.scheduler = options.scheduler || SequentialTaskQueue.defaultScheduler;
}
/**
* Adds a new task to the queue.
* @param {Function} task - The function to call when the task is run
* @param {TaskOptions} options - An object containing arguments and options for the task.
* @returns {CancellablePromiseLike<any>} A promise that can be used to await or cancel the task.
*/
push(task: Function, options?: TaskOptions): CancellablePromiseLike<any> {
if (this._isClosed)
throw new Error(`${this.name} has been previously closed`);
var taskEntry: TaskEntry = {
callback: task,
args: options && options.args ? (Array.isArray(options.args) ? options.args.slice() : [options.args]) : [],
timeout: options && options.timeout !== undefined ? options.timeout : this.defaultTimeout,
cancellationToken: {
cancel: (reason?) => this.cancelTask(taskEntry, reason)
},
resolve: undefined,
reject: undefined
};
taskEntry.args.push(taskEntry.cancellationToken);
this.queue.push(taskEntry);
this.scheduler.schedule(() => this.next());
var result = (new Promise((resolve, reject) => {
taskEntry.resolve = resolve;
taskEntry.reject = reject;
}) as any) as CancellablePromiseLike<any>;
result.cancel = (reason?: any) => taskEntry.cancellationToken.cancel(reason);
return result;
}
/**
* Cancels the currently running task (if any), and clears the queue.
* @returns {Promise} A Promise that is fulfilled when the queue is empty and the current task has been cancelled.
*/
cancel(): PromiseLike<any> {
if (this.currentTask)
this.cancelTask(this.currentTask, cancellationTokenReasons.cancel);
var queue = this.queue.splice(0);
// Cancel all and emit a drained event if there were tasks waiting in the queue
if (queue.length) {
queue.forEach(task => this.cancelTask(task, cancellationTokenReasons.cancel));
this.emit(sequentialTaskQueueEvents.drained);
}
return this.wait();
}
/**
* Closes the queue, preventing new tasks to be added.
* Any calls to {@link SequentialTaskQueue.push} after closing the queue will result in an exception.
* @param {boolean} cancel - Indicates that the queue should also be cancelled.
* @returns {Promise} A Promise that is fulfilled when the queue has finished executing remaining tasks.
*/
close(cancel?: boolean): PromiseLike<any> {
if (!this._isClosed) {
this._isClosed = true;
if (cancel)
return this.cancel();
}
return this.wait();
}
/**
* Returns a promise that is fulfilled when the queue is empty.
* @returns {Promise}
*/
wait(): PromiseLike<any> {
if (!this.currentTask && this.queue.length === 0)
return Promise.resolve();
return new Promise(resolve => {
this.waiters.push(resolve);
});
}
/**
* Adds an event handler for a named event.
* @param {string} evt - Event name. See the readme for a list of valid events.
* @param {Function} handler - Event handler. When invoking the handler, the queue will set itself as the `this` argument of the call.
*/
on(evt: string, handler: Function) {
this.events = this.events || {};
(this.events[evt] || (this.events[evt] = [])).push(handler);
}
/**
* Adds a single-shot event handler for a named event.
* @param {string} evt - Event name. See the readme for a list of valid events.
* @param {Function} handler - Event handler. When invoking the handler, the queue will set itself as the `this` argument of the call.
*/
once(evt: string, handler: Function) {
var cb = (...args: any[]) => {
this.removeListener(evt, cb);
handler.apply(this, args);
};
this.on(evt, cb);
}
/**
* Removes an event handler.
* @param {string} evt - Event name
* @param {Function} handler - Event handler to be removed
*/
removeListener(evt: string, handler: Function) {
if (this.events) {
var list = this.events[evt];
if (list) {
var i = 0;
while (i < list.length) {
if (list[i] === handler)
list.splice(i, 1);
else
i++;
}
}
}
}
/** @see {@link SequentialTaskQueue.removeListener} */
off(evt: string, handler: Function) {
return this.removeListener(evt, handler);
}
protected emit(evt: string, ...args: any[]) {
if (this.events && this.events[evt])
try {
this.events[evt].forEach(fn => fn.apply(this, args));
} catch (e) {
console.error(`${this.name}: Exception in '${evt}' event handler`, e);
}
}
protected next() {
// Try running the next task, if not currently running one
if (!this.currentTask) {
var task = this.queue.shift();
// skip cancelled tasks
while (task && task.cancellationToken.cancelled)
task = this.queue.shift();
if (task) {
try {
this.currentTask = task;
if (task.timeout) {
task.timeoutHandle = setTimeout(
() => {
this.emit(sequentialTaskQueueEvents.timeout);
this.cancelTask(task, cancellationTokenReasons.timeout);
},
task.timeout);
}
let res = task.callback.apply(undefined, task.args);
if (res && isPromise(res)) {
res.then(result => {
task.result = result;
this.doneTask(task);
},
err => {
this.doneTask(task, err);
});
} else {
task.result = res;
this.doneTask(task);
}
} catch (e) {
this.doneTask(task, e);
}
} else {
// queue is empty, call waiters
this.callWaiters();
}
}
}
private cancelTask(task: TaskEntry, reason?: any) {
task.cancellationToken.cancelled = true;
task.cancellationToken.reason = reason;
this.doneTask(task);
}
private doneTask(task: TaskEntry, error?: any) {
if (task.timeoutHandle)
clearTimeout(task.timeoutHandle);
task.cancellationToken.cancel = noop;
if (error) {
this.emit(sequentialTaskQueueEvents.error, error);
task.reject.call(undefined, error);
} else if (task.cancellationToken.cancelled)
task.reject.call(undefined, task.cancellationToken.reason)
else
task.resolve.call(undefined, task.result);
if (this.currentTask === task) {
this.currentTask = undefined;
if (!this.queue.length) {
this.emit(sequentialTaskQueueEvents.drained);
this.callWaiters();
}
else
this.scheduler.schedule(() => this.next());
}
}
private callWaiters() {
let waiters = this.waiters.splice(0);
waiters.forEach(waiter => waiter());
}
}
interface TaskEntry {
args: any[];
callback: Function;
timeout?: number;
timeoutHandle?: any;
cancellationToken: CancellationToken;
result?: any;
resolve: (value: any | PromiseLike<any>) => void;
reject: (reason?: any) => void;
}
function noop() {
}
function isPromise(obj: any): obj is PromiseLike<any> {
return (obj && typeof obj.then === "function");
}
SequentialTaskQueue.defaultScheduler = {
schedule: typeof setImmediate === "function"
? callback => setImmediate(<(...args: any[]) => void>callback)
: callback => setTimeout(<(...args: any[]) => void>callback, 0)
};