1 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2 | if (kind === "m") throw new TypeError("Private method is not writable");
|
3 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6 | };
|
7 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11 | };
|
12 | var _PQueue_instances, _PQueue_carryoverConcurrencyCount, _PQueue_isIntervalIgnored, _PQueue_intervalCount, _PQueue_intervalCap, _PQueue_interval, _PQueue_intervalEnd, _PQueue_intervalId, _PQueue_timeoutId, _PQueue_queue, _PQueue_queueClass, _PQueue_pendingCount, _PQueue_concurrency, _PQueue_isPaused, _PQueue_throwOnTimeout, _PQueue_doesIntervalAllowAnother_get, _PQueue_doesConcurrentAllowAnother_get, _PQueue_next, _PQueue_emitEvents, _PQueue_onResumeInterval, _PQueue_isIntervalPaused_get, _PQueue_tryToStartAnother, _PQueue_initializeIntervalIfNeeded, _PQueue_onInterval, _PQueue_processQueue, _PQueue_onEvent;
|
13 | import EventEmitter from 'eventemitter3';
|
14 | import pTimeout, { TimeoutError } from 'p-timeout';
|
15 | import PriorityQueue from './priority-queue.js';
|
16 | const timeoutError = new TimeoutError();
|
17 |
|
18 |
|
19 |
|
20 | export class AbortError extends Error {
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 | export default class PQueue extends EventEmitter {
|
26 | constructor(options) {
|
27 | var _a, _b, _c, _d;
|
28 | super();
|
29 | _PQueue_instances.add(this);
|
30 | _PQueue_carryoverConcurrencyCount.set(this, void 0);
|
31 | _PQueue_isIntervalIgnored.set(this, void 0);
|
32 | _PQueue_intervalCount.set(this, 0);
|
33 | _PQueue_intervalCap.set(this, void 0);
|
34 | _PQueue_interval.set(this, void 0);
|
35 | _PQueue_intervalEnd.set(this, 0);
|
36 | _PQueue_intervalId.set(this, void 0);
|
37 | _PQueue_timeoutId.set(this, void 0);
|
38 | _PQueue_queue.set(this, void 0);
|
39 | _PQueue_queueClass.set(this, void 0);
|
40 | _PQueue_pendingCount.set(this, 0);
|
41 |
|
42 | _PQueue_concurrency.set(this, void 0);
|
43 | _PQueue_isPaused.set(this, void 0);
|
44 | _PQueue_throwOnTimeout.set(this, void 0);
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | Object.defineProperty(this, "timeout", {
|
51 | enumerable: true,
|
52 | configurable: true,
|
53 | writable: true,
|
54 | value: void 0
|
55 | });
|
56 |
|
57 | options = {
|
58 | carryoverConcurrencyCount: false,
|
59 | intervalCap: Number.POSITIVE_INFINITY,
|
60 | interval: 0,
|
61 | concurrency: Number.POSITIVE_INFINITY,
|
62 | autoStart: true,
|
63 | queueClass: PriorityQueue,
|
64 | ...options,
|
65 | };
|
66 | if (!(typeof options.intervalCap === 'number' && options.intervalCap >= 1)) {
|
67 | throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${(_b = (_a = options.intervalCap) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''}\` (${typeof options.intervalCap})`);
|
68 | }
|
69 | if (options.interval === undefined || !(Number.isFinite(options.interval) && options.interval >= 0)) {
|
70 | throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${(_d = (_c = options.interval) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : ''}\` (${typeof options.interval})`);
|
71 | }
|
72 | __classPrivateFieldSet(this, _PQueue_carryoverConcurrencyCount, options.carryoverConcurrencyCount, "f");
|
73 | __classPrivateFieldSet(this, _PQueue_isIntervalIgnored, options.intervalCap === Number.POSITIVE_INFINITY || options.interval === 0, "f");
|
74 | __classPrivateFieldSet(this, _PQueue_intervalCap, options.intervalCap, "f");
|
75 | __classPrivateFieldSet(this, _PQueue_interval, options.interval, "f");
|
76 | __classPrivateFieldSet(this, _PQueue_queue, new options.queueClass(), "f");
|
77 | __classPrivateFieldSet(this, _PQueue_queueClass, options.queueClass, "f");
|
78 | this.concurrency = options.concurrency;
|
79 | this.timeout = options.timeout;
|
80 | __classPrivateFieldSet(this, _PQueue_throwOnTimeout, options.throwOnTimeout === true, "f");
|
81 | __classPrivateFieldSet(this, _PQueue_isPaused, options.autoStart === false, "f");
|
82 | }
|
83 | get concurrency() {
|
84 | return __classPrivateFieldGet(this, _PQueue_concurrency, "f");
|
85 | }
|
86 | set concurrency(newConcurrency) {
|
87 | if (!(typeof newConcurrency === 'number' && newConcurrency >= 1)) {
|
88 | throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${newConcurrency}\` (${typeof newConcurrency})`);
|
89 | }
|
90 | __classPrivateFieldSet(this, _PQueue_concurrency, newConcurrency, "f");
|
91 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this);
|
92 | }
|
93 | |
94 |
|
95 |
|
96 | async add(fn, options = {}) {
|
97 | return new Promise((resolve, reject) => {
|
98 | const run = async () => {
|
99 | var _a;
|
100 | var _b, _c;
|
101 | __classPrivateFieldSet(this, _PQueue_pendingCount, (_b = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _b++, _b), "f");
|
102 | __classPrivateFieldSet(this, _PQueue_intervalCount, (_c = __classPrivateFieldGet(this, _PQueue_intervalCount, "f"), _c++, _c), "f");
|
103 | try {
|
104 | if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
105 |
|
106 | reject(new AbortError('The task was aborted.'));
|
107 | return;
|
108 | }
|
109 | const operation = (this.timeout === undefined && options.timeout === undefined) ? fn({ signal: options.signal }) : pTimeout(Promise.resolve(fn({ signal: options.signal })), (options.timeout === undefined ? this.timeout : options.timeout), () => {
|
110 | if (options.throwOnTimeout === undefined ? __classPrivateFieldGet(this, _PQueue_throwOnTimeout, "f") : options.throwOnTimeout) {
|
111 | reject(timeoutError);
|
112 | }
|
113 | return undefined;
|
114 | });
|
115 | const result = await operation;
|
116 | resolve(result);
|
117 | this.emit('completed', result);
|
118 | }
|
119 | catch (error) {
|
120 | reject(error);
|
121 | this.emit('error', error);
|
122 | }
|
123 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_next).call(this);
|
124 | };
|
125 | __classPrivateFieldGet(this, _PQueue_queue, "f").enqueue(run, options);
|
126 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this);
|
127 | this.emit('add');
|
128 | });
|
129 | }
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 | async addAll(functions, options) {
|
136 | return Promise.all(functions.map(async (function_) => this.add(function_, options)));
|
137 | }
|
138 | |
139 |
|
140 |
|
141 | start() {
|
142 | if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) {
|
143 | return this;
|
144 | }
|
145 | __classPrivateFieldSet(this, _PQueue_isPaused, false, "f");
|
146 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this);
|
147 | return this;
|
148 | }
|
149 | |
150 |
|
151 |
|
152 | pause() {
|
153 | __classPrivateFieldSet(this, _PQueue_isPaused, true, "f");
|
154 | }
|
155 | |
156 |
|
157 |
|
158 | clear() {
|
159 | __classPrivateFieldSet(this, _PQueue_queue, new (__classPrivateFieldGet(this, _PQueue_queueClass, "f"))(), "f");
|
160 | }
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | async onEmpty() {
|
167 |
|
168 | if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) {
|
169 | return;
|
170 | }
|
171 | await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'empty');
|
172 | }
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | async onSizeLessThan(limit) {
|
181 |
|
182 | if (__classPrivateFieldGet(this, _PQueue_queue, "f").size < limit) {
|
183 | return;
|
184 | }
|
185 | await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'next', () => __classPrivateFieldGet(this, _PQueue_queue, "f").size < limit);
|
186 | }
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 | async onIdle() {
|
193 |
|
194 | if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) {
|
195 | return;
|
196 | }
|
197 | await __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onEvent).call(this, 'idle');
|
198 | }
|
199 | |
200 |
|
201 |
|
202 | get size() {
|
203 | return __classPrivateFieldGet(this, _PQueue_queue, "f").size;
|
204 | }
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 | sizeBy(options) {
|
211 |
|
212 | return __classPrivateFieldGet(this, _PQueue_queue, "f").filter(options).length;
|
213 | }
|
214 | |
215 |
|
216 |
|
217 | get pending() {
|
218 | return __classPrivateFieldGet(this, _PQueue_pendingCount, "f");
|
219 | }
|
220 | |
221 |
|
222 |
|
223 | get isPaused() {
|
224 | return __classPrivateFieldGet(this, _PQueue_isPaused, "f");
|
225 | }
|
226 | }
|
227 | _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = new WeakMap(), _PQueue_intervalCount = new WeakMap(), _PQueue_intervalCap = new WeakMap(), _PQueue_interval = new WeakMap(), _PQueue_intervalEnd = new WeakMap(), _PQueue_intervalId = new WeakMap(), _PQueue_timeoutId = new WeakMap(), _PQueue_queue = new WeakMap(), _PQueue_queueClass = new WeakMap(), _PQueue_pendingCount = new WeakMap(), _PQueue_concurrency = new WeakMap(), _PQueue_isPaused = new WeakMap(), _PQueue_throwOnTimeout = new WeakMap(), _PQueue_instances = new WeakSet(), _PQueue_doesIntervalAllowAnother_get = function _PQueue_doesIntervalAllowAnother_get() {
|
228 | return __classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalCount, "f") < __classPrivateFieldGet(this, _PQueue_intervalCap, "f");
|
229 | }, _PQueue_doesConcurrentAllowAnother_get = function _PQueue_doesConcurrentAllowAnother_get() {
|
230 | return __classPrivateFieldGet(this, _PQueue_pendingCount, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f");
|
231 | }, _PQueue_next = function _PQueue_next() {
|
232 | var _a;
|
233 | __classPrivateFieldSet(this, _PQueue_pendingCount, (_a = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _a--, _a), "f");
|
234 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this);
|
235 | this.emit('next');
|
236 | }, _PQueue_emitEvents = function _PQueue_emitEvents() {
|
237 | this.emit('empty');
|
238 | if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0) {
|
239 | this.emit('idle');
|
240 | }
|
241 | }, _PQueue_onResumeInterval = function _PQueue_onResumeInterval() {
|
242 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this);
|
243 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this);
|
244 | __classPrivateFieldSet(this, _PQueue_timeoutId, undefined, "f");
|
245 | }, _PQueue_isIntervalPaused_get = function _PQueue_isIntervalPaused_get() {
|
246 | const now = Date.now();
|
247 | if (__classPrivateFieldGet(this, _PQueue_intervalId, "f") === undefined) {
|
248 | const delay = __classPrivateFieldGet(this, _PQueue_intervalEnd, "f") - now;
|
249 | if (delay < 0) {
|
250 |
|
251 |
|
252 | __classPrivateFieldSet(this, _PQueue_intervalCount, (__classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f")) ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f");
|
253 | }
|
254 | else {
|
255 |
|
256 | if (__classPrivateFieldGet(this, _PQueue_timeoutId, "f") === undefined) {
|
257 | __classPrivateFieldSet(this, _PQueue_timeoutId, setTimeout(() => {
|
258 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onResumeInterval).call(this);
|
259 | }, delay), "f");
|
260 | }
|
261 | return true;
|
262 | }
|
263 | }
|
264 | return false;
|
265 | }, _PQueue_tryToStartAnother = function _PQueue_tryToStartAnother() {
|
266 | if (__classPrivateFieldGet(this, _PQueue_queue, "f").size === 0) {
|
267 |
|
268 |
|
269 | if (__classPrivateFieldGet(this, _PQueue_intervalId, "f")) {
|
270 | clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f"));
|
271 | }
|
272 | __classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f");
|
273 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_emitEvents).call(this);
|
274 | return false;
|
275 | }
|
276 | if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) {
|
277 | const canInitializeInterval = !__classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_isIntervalPaused_get);
|
278 | if (__classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_doesIntervalAllowAnother_get) && __classPrivateFieldGet(this, _PQueue_instances, "a", _PQueue_doesConcurrentAllowAnother_get)) {
|
279 | const job = __classPrivateFieldGet(this, _PQueue_queue, "f").dequeue();
|
280 | if (!job) {
|
281 | return false;
|
282 | }
|
283 | this.emit('active');
|
284 | job();
|
285 | if (canInitializeInterval) {
|
286 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this);
|
287 | }
|
288 | return true;
|
289 | }
|
290 | }
|
291 | return false;
|
292 | }, _PQueue_initializeIntervalIfNeeded = function _PQueue_initializeIntervalIfNeeded() {
|
293 | if (__classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalId, "f") !== undefined) {
|
294 | return;
|
295 | }
|
296 | __classPrivateFieldSet(this, _PQueue_intervalId, setInterval(() => {
|
297 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this);
|
298 | }, __classPrivateFieldGet(this, _PQueue_interval, "f")), "f");
|
299 | __classPrivateFieldSet(this, _PQueue_intervalEnd, Date.now() + __classPrivateFieldGet(this, _PQueue_interval, "f"), "f");
|
300 | }, _PQueue_onInterval = function _PQueue_onInterval() {
|
301 | if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) {
|
302 | clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f"));
|
303 | __classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f");
|
304 | }
|
305 | __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f");
|
306 | __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this);
|
307 | }, _PQueue_processQueue = function _PQueue_processQueue() {
|
308 |
|
309 | while (__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this)) { }
|
310 | }, _PQueue_onEvent = async function _PQueue_onEvent(event, filter) {
|
311 | return new Promise(resolve => {
|
312 | const listener = () => {
|
313 | if (filter && !filter()) {
|
314 | return;
|
315 | }
|
316 | this.off(event, listener);
|
317 | resolve();
|
318 | };
|
319 | this.on(event, listener);
|
320 | });
|
321 | };
|