-
Notifications
You must be signed in to change notification settings - Fork 79
/
queue.ts
82 lines (71 loc) 路 2.11 KB
/
queue.ts
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
import {EventEmitter} from 'events';
export interface QueueOptions {
concurrency: number;
}
export interface QueueItemOptions {
delay?: number;
}
interface QueueItem {
fn: AsyncFunction;
timeToRun: number;
}
export declare interface Queue {
on(event: 'done', listener: () => void): this;
}
export type AsyncFunction = () => Promise<void>;
export class Queue extends EventEmitter {
private q: Array<QueueItem> = [];
private activeFunctions = 0;
private concurrency: number;
constructor(options: QueueOptions) {
super();
this.concurrency = options.concurrency;
}
add(fn: AsyncFunction, options?: QueueItemOptions) {
const delay = options?.delay || 0;
const timeToRun = Date.now() + delay;
this.q.push({
fn,
timeToRun,
});
setTimeout(() => this.tick(), delay);
}
private tick() {
// Check if we're complete
if (this.activeFunctions === 0 && this.q.length === 0) {
this.emit('done');
return;
}
for (let i = 0; i < this.q.length; i++) {
// Check if we have too many concurrent functions executing
if (this.activeFunctions >= this.concurrency) {
return;
}
// grab the element at the front of the array
const item = this.q.shift()!;
// Depending on CPU load and other factors setTimeout() is not guranteed to run exactly
// when scheduled. This causes problems if there is only one item in the queue, as
// there's a chance it will never be processed. Allow for a small delta to address this:
const delta = 150;
const readyToExecute =
Math.abs(item.timeToRun - Date.now()) < delta ||
item.timeToRun < Date.now();
// make sure this element is ready to execute - if not, to the back of the stack
if (readyToExecute) {
// this function is ready to go!
this.activeFunctions++;
item.fn().finally(() => {
this.activeFunctions--;
this.tick();
});
} else {
this.q.push(item);
}
}
}
async onIdle() {
return new Promise<void>(resolve => {
this.on('done', () => resolve());
});
}
}