-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
70 lines (67 loc) · 1.99 KB
/
index.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
import {EventEmitter} from 'events';
import * as uuid from 'uuid';
enum QueueState {
IDLE,
RUNNING
}
export default class SequentialQueue extends EventEmitter {
public static Event = {
Done: 'Done'
};
private static event = {
Queued: 'Queued',
Start: 'Start',
End: 'End'
};
private queue = [];
private state = QueueState.IDLE;
constructor() {
super();
this.on(SequentialQueue.event.Queued, this.handleQueued);
this.on(SequentialQueue.event.Start, this.handleStart);
this.on(SequentialQueue.event.End, this.handleEnd);
this.on(SequentialQueue.Event.Done, this.handleDone);
}
push(operation: () => Promise<any>): Promise<any> {
const id = uuid();
this.queue.push({id, operation});
this.emit(SequentialQueue.event.Queued);
return new Promise(resolve => this.on(id, resolve));
}
private async run() {
const {id, operation} = this.queue.shift();
let result = null;
this.emit(SequentialQueue.event.Start);
try {
result = await operation();
} catch(ex) {
console.error(`[AsyncQueue] queue operation fail: `, ex);
} finally {
this.emit(SequentialQueue.event.End, id, result);
}
}
private handleQueued() {
// console.log('[AsyncQueue] queued');
if (this.state !== QueueState.RUNNING) {
this.run();
}
}
private handleStart() {
// console.log('[AsyncQueue] start');
if (this.state !== QueueState.RUNNING) {
this.state = QueueState.RUNNING;
}
}
private handleEnd(id, result) {
// console.log('[AsyncQueue] end');
this.emit(id, result);
if (this.queue.length > 0) {
return this.run();
}
this.emit(SequentialQueue.Event.Done);
}
private handleDone() {
// console.log('[AsyncQueue] done');
this.state = QueueState.IDLE;
}
}