-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (154 loc) · 3.15 KB
/
Copy pathindex.js
File metadata and controls
182 lines (154 loc) · 3.15 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
const { EventEmitter } = require("node:events");
class Queue extends EventEmitter {
#_storage = new Set();
#_paused = false;
#_running = false;
#_timeout = 0;
#_worker = async task => task;
#_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 != null && (!Number.isFinite(timeout) || !Number.isInteger(timeout) || timeout < 0)) {
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; let timeout;
try {
const worker = (async() => {
try {
const res = await this.#_worker(task);
this.emit("done", res, task);
} catch (error) {
this.emit("error", error, task);
}
})();
if (this.#_timeout) {
await Promise.race([
new Promise(resolve => {
r = resolve;
timeout = setTimeout(() => {
this.emit("timeout", task);
r = null;
resolve();
}, this.#_timeout);
}),
worker
]);
} else {
await worker;
}
} 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;