-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·315 lines (278 loc) · 7.57 KB
/
Copy pathindex.js
File metadata and controls
executable file
·315 lines (278 loc) · 7.57 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
// @flow
import EventEmitter from "events";
const State = {
IDLE: 0,
RUNNING: 1,
STOPPED: 2,
};
type Options = {
concurrent: number,
interval: number,
start: boolean,
};
/**
* A small and simple library for promise-based queues. It will execute enqueued
* functions concurrently at a specified speed. When a task is being resolved or
* rejected, an event is emitted.
*
* @example
* const queue = new Queue({
* concurrent: 1,
* interval: 2000
* });
*
* queue.on("resolve", data => console.log(data));
* queue.on("reject", error => console.error(error));
*
* queue.enqueue(asyncTaskA);
* queue.enqueue([asyncTaskB, asyncTaskC]);
*
* @class Queue
* @extends EventEmitter
*/
export default class Queue extends EventEmitter {
/**
* A collection to store unresolved tasks. We use a Map here because V8 uses a
* variant of hash tables that generally have O(1) complexity for retrieval
* and lookup.
*
* @see https://codereview.chromium.org/220293002/
* @type {Map}
* @access private
*/
tasks: Map<number, Function> = new Map();
/**
* @type {number} Used to generate a unique id for each task
* @access private
*/
uniqueId: number = 0;
/**
* @type {number}
* @access private
*/
lastRan: number = 0;
/**
* @type {TimeoutID}
* @access private
*/
timeoutId: TimeoutID;
/**
* @type {number} Amount of tasks currently handled by the queue
* @access private
*/
currentlyHandled: number = 0;
/**
* @type {State}
* @access public
*/
state: $Values<typeof State> = State.IDLE;
/**
* @type {Object} options
* @type {number} options.concurrent How many tasks should be executed in parallel
* @type {number} options.interval How often should new tasks be executed (in ms)
* @type {boolean} options.start Whether it should automatically execute new tasks as soon as they are added
* @access public
*/
options: Options = {
concurrent: 5,
interval: 500,
start: true,
};
/**
* Initializes a new queue instance with provided options.
*
* @param {Object} options
* @param {number} options.concurrent How many tasks should be executed in parallel
* @param {number} options.interval How often should new tasks be executed (in ms)
* @param {boolean} options.start Whether it should automatically execute new tasks as soon as they are added
* @return {Queue}
*/
constructor(options: Options = {}) {
super();
this.options = { ...this.options, ...options };
this.options.interval = parseInt(this.options.interval, 10);
this.options.concurrent = parseInt(this.options.concurrent, 10);
}
/**
* Starts the queue if it has not been started yet.
*
* @emits start
* @return {void}
* @access public
*/
start(): void {
if (this.state !== State.RUNNING && !this.isEmpty) {
this.state = State.RUNNING;
this.emit("start");
(async () => {
while (this.shouldRun) {
await this.dequeue();
}
})();
}
}
/**
* Forces the queue to stop. New tasks will not be executed automatically even
* if `options.start` was set to `true`.
*
* @emits stop
* @return {void}
* @access public
*/
stop(): void {
clearTimeout(this.timeoutId);
this.state = State.STOPPED;
this.emit("stop");
}
/**
* Goes to the next request and stops the loop if there are no requests left.
*
* @emits end
* @return {void}
* @access private
*/
finalize(): void {
this.currentlyHandled -= 1;
if (this.currentlyHandled === 0 && this.isEmpty) {
this.stop();
// Finalize doesn't force queue to stop as `Queue.stop()` does. Therefore,
// new tasks should be still resolved automatically if `options.start` was
// set to `true` (see `Queue.enqueue`):
this.state = State.IDLE;
this.emit("end");
}
}
/**
* Executes _n_ concurrent (based od `options.concurrent`) promises from the
* queue.
*
* @return {Promise<any>}
* @emits resolve
* @emits reject
* @emits dequeue
* @access private
*/
async execute(): Promise<*> {
const promises = [];
this.tasks.forEach((promise, id) => {
// Maximum amount of parallel tasks:
if (this.currentlyHandled < this.options.concurrent) {
this.currentlyHandled++;
this.tasks.delete(id);
promises.push(
Promise.resolve(promise())
.then((value) => {
this.emit("resolve", value);
return value;
})
.catch((error) => {
this.emit("reject", error);
return error;
})
.finally(() => {
this.emit("dequeue", id);
this.finalize();
})
);
}
});
// Note: Promise.all will reject if any of the concurrent promises fail,
// regardless if they are all finished yet! This is why we are emitting
// events per task (and not per batch of tasks with respect to
// `concurrent`):
const output = await Promise.all(promises);
return this.options.concurrent === 1 ? output[0] : output;
}
/**
* Executes _n_ concurrent (based od `options.concurrent`) promises from the
* queue.
*
* @return {Promise<any>}
* @emits resolve
* @emits reject
* @emits dequeue
* @access public
*/
dequeue(): Promise<*> {
const { interval } = this.options;
return new Promise<*>((resolve, reject) => {
const timeout = Math.max(0, interval - (Date.now() - this.lastRan));
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(() => {
this.lastRan = Date.now();
this.execute().then(resolve);
}, timeout);
});
}
/**
* Adds tasks to the queue.
*
* @param {Function|Array} tasks Tasks to add to the queue
* @throws {Error} When task is not a function
* @return {number[]} Array of task ids
* @access public
*/
enqueue(tasks: Function | Array<Function>): number[] {
if (Array.isArray(tasks)) {
const emptyNumberArray: number[] = [];
const taskIds = emptyNumberArray.concat(
tasks.map((task) => this.enqueue(task)[0])
);
return taskIds;
}
if (typeof tasks !== "function") {
throw new Error(`You must provide a function, not ${typeof tasks}.`);
}
this.uniqueId = (this.uniqueId + 1) % Number.MAX_SAFE_INTEGER;
this.tasks.set(this.uniqueId, tasks);
// Start the queue if the queue should resolve new tasks automatically and
// hasn't been forced to stop:
if (this.options.start && this.state !== State.STOPPED) {
this.start();
}
return [this.uniqueId];
}
/**
* @see enqueue
* @access public
*/
add(tasks: Function | Array<Function>): void {
this.enqueue(tasks);
}
/**
* Removes all tasks from the queue.
*
* @return {void}
* @access public
*/
clear(): void {
this.tasks.clear();
}
/**
* Size of the queue.
*
* @type {number}
* @access public
*/
get size(): number {
return this.tasks.size;
}
/**
* Checks whether the queue is empty, i.e. there's no tasks.
*
* @type {boolean}
* @access public
*/
get isEmpty(): boolean {
return this.size === 0;
}
/**
* Checks whether the queue is not empty and not stopped.
*
* @type {boolean}
* @access public
*/
get shouldRun(): boolean {
return !this.isEmpty && this.state !== State.STOPPED;
}
}