-
Notifications
You must be signed in to change notification settings - Fork 279
/
queue.ts
75 lines (62 loc) · 1.89 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
interface Job {
key: string | number;
running: boolean;
start: () => void;
}
class Queue {
private jobs: Job[];
private readonly concurrent: number;
constructor(concurrent = 1) {
this.jobs = [];
this.concurrent = concurrent;
}
public async execute<T>(func: () => Promise<T>, key: string | number = null): Promise<T> {
const job : Job = {key, running: false, start: null};
// Minor optimization/workaround: various tests like the idea that a job that is
// immediately runnable is run without an event loop spin. This also helps with stack
// traces in some cases, so avoid an `await` if we can help it.
this.jobs.push(job);
if (this.getNext() !== job) {
await new Promise((resolve): void => {
job.start = (): void => {
job.running = true;
resolve(null);
};
this.executeNext();
});
} else {
job.running = true;
}
try {
return await func();
} finally {
this.jobs.splice(this.jobs.indexOf(job), 1);
this.executeNext();
}
}
private executeNext(): void {
const job = this.getNext();
if (job) {
job.start();
}
}
private getNext(): Job {
if (this.jobs.filter((j) => j.running).length > (this.concurrent - 1)) {
return null;
}
for (let i = 0; i < this.jobs.length; i++) {
const job = this.jobs[i];
if (!job.running && (!job.key || !this.jobs.find((j) => j.key === job.key && j.running))) {
return job;
}
}
return null;
}
public clear(): void {
this.jobs = [];
}
public count(): number {
return this.jobs.length;
}
}
export default Queue;