-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (145 loc) · 3.06 KB
/
index.js
File metadata and controls
171 lines (145 loc) · 3.06 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
const { EventEmitter } = require('node:events');
class Queue extends EventEmitter {
#_storage = new Set();
#_paused = false;
#_running = false;
#_timeout = 0;
#_worker = async task => task;
// eslint-disable-next-line no-unused-vars
#_filter = task => true;
/**
* Create a queue
* @param {Function} worker
* @param {Object} [params]
* @param {number} [params.timeout] timeout
* @param {Function} [params.filter] function(task) which filters incoming tasks
*/
constructor(worker, { timeout = 0, filter } = { }) {
super();
if (typeof worker !== "function")
throw new Error("Worker is not a function");
if (timeout && !Number.isFinite(timeout))
throw new Error("Timeout is not a number");
this.#_worker = worker;
if (timeout)
this.#_timeout = timeout;
if (filter)
this.setFilter(filter);
}
/**
* a function that pauses the processing of tasks
*/
pause() {
this.#_paused = true;
}
/**
* a function that resumes the processing of the queued tasks
*/
resume() {
if (!this.#_paused)
return;
this.#_paused = false;
this.#_next();
}
/**
* add new task(s) to the queue
* @param {...any} tasks
*/
push(...args) {
for (const task of args) {
if (this.#_filter(task)) {
this.#_storage.add(task);
this.#_next();
}
}
}
/**
* a function that empties
* remaining tasks from the queue
*/
kill() {
this.#_storage.clear();
}
/**
* a function returning execution state of the queue
* @return {Boolean} is running
*/
running() {
return this.#_running;
}
/**
* a function returning the queue's size
* @return {integer} count of tasks
*/
length() {
return this.#_storage.size;
}
/**
* a function set a custom filter
* @param {Function} fn function(task) which filters incoming tasks
*/
setFilter(fn) {
if (typeof fn !== "function")
throw new Error("Filter is not a function");
this.#_filter = fn;
}
/**
* @private
*/
#_next() {
if (this.#_paused || this.#_running || this.#_storage.size === 0) {
return;
}
this.#_running = true;
const [task] = this.#_storage;
this.#_storage.delete(task);
if (this.#_storage.size === 0) {
this.#_storage.clear();
this.emit('empty');
}
this.#_execute(task);
}
/**
* @private
* @param {*} task
* @returns {Promise<null>}
*/
async #_execute(task) {
let r, timeout;
try {
if (this.#_timeout) {
await Promise.race([
new Promise(resolve => {
r = resolve;
timeout = setTimeout(() => {
this.emit('timeout', task);
r = null;
resolve();
}, this.#_timeout);
}),
(async() => {
const res = await this.#_worker(task);
this.emit('done', res, task);
})()
]);
} else {
const res = await this.#_worker(task);
this.emit('done', res, task);
}
} catch (error) {
this.emit('error', error, task);
} finally {
this.#_running = false;
if (r) {
clearTimeout(timeout);
r();
}
if (this.#_storage.size === 0) {
this.emit('drain');
} else {
this.#_next();
}
}
}
}
module.exports.Queue = Queue;