-
Notifications
You must be signed in to change notification settings - Fork 17
/
AutoQueue.js
41 lines (37 loc) · 1.17 KB
/
AutoQueue.js
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
import FunctionQueue from './FunctionQueue';
/**
* This data structure is a queue that will run every function one by one sequentially.
* It will run indifferently synchronous and asynchronous functions. Making sure the previous function is over before starting the next one. It will essentially wait for the previous function to be finished before running the next one.
* It has no return value, it will just run the function added sometimes in the future.
*
* The queue can be auto executed on add or the execution can be delayed.
*
* @KhaaZ
*
* @class AutoQueue
* @extends FunctionQueue
*/
class AutoQueue extends FunctionQueue {
/**
* Execute the queue.
* @memberof AutoQueue
*/
async exec() {
if (this._functions.size > 0) {
this._running = true;
const func = this._functions.dequeue();
try {
await func();
} catch (err) {
if (this.stopOnError) {
throw err;
}
console.log(err);
}
this.exec();
} else {
this._running = false;
}
}
}
export default AutoQueue;