async-worker-queue
Advanced tools
+21
-2
@@ -115,4 +115,6 @@ "use strict"; | ||
| for (let i = 0; i < this.concurrency; i++) { | ||
| const worker = yield __privateGet(this, _createWorker).call(this, i); | ||
| __privateGet(this, _workers).add({ | ||
| execute: yield __privateGet(this, _createWorker).call(this, i), | ||
| execute: worker.execute, | ||
| dispose: worker.dispose, | ||
| busy: false, | ||
@@ -139,2 +141,16 @@ index: i | ||
| } | ||
| /** | ||
| * Destroys the queue and all workers. | ||
| * | ||
| * You should not use this queue after calling this function. | ||
| */ | ||
| dispose() { | ||
| return __async(this, null, function* () { | ||
| for (const worker of __privateGet(this, _workers)) { | ||
| yield worker.dispose(); | ||
| } | ||
| __privateSet(this, _workers, /* @__PURE__ */ new Set()); | ||
| __privateSet(this, _queue, new Queue()); | ||
| }); | ||
| } | ||
| }; | ||
@@ -166,7 +182,10 @@ _queue = new WeakMap(); | ||
| if (__privateGet(this, _options).removeWorkerOnError || __privateGet(this, _options).recreateWorkerOnError) { | ||
| yield worker.dispose(); | ||
| __privateGet(this, _workers).delete(worker); | ||
| } | ||
| if (__privateGet(this, _options).recreateWorkerOnError) { | ||
| const newWorker = yield __privateGet(this, _createWorker).call(this, worker.index); | ||
| __privateGet(this, _workers).add({ | ||
| execute: yield __privateGet(this, _createWorker).call(this, worker.index), | ||
| execute: newWorker.execute, | ||
| dispose: newWorker.dispose, | ||
| busy: false, | ||
@@ -173,0 +192,0 @@ index: worker.index |
+22
-2
@@ -1,4 +0,18 @@ | ||
| type Execute<T, R> = (payload: T) => Promise<R>; | ||
| type Execute<T, R> = (payload: T) => Promise<R> | R; | ||
| type Dispose = () => Promise<void> | void; | ||
| interface CreateWorkerResult<T, R> { | ||
| execute: Execute<T, R>; | ||
| dispose: Dispose; | ||
| } | ||
| interface AsyncWorkerQueueOptions { | ||
| /** | ||
| * If true, the worker will be removed from the list of workers when it errors. | ||
| * It will also dispose the worker. | ||
| * No new worker will be created to replace it, unless `recreateWorkerOnError` is also set to true. | ||
| */ | ||
| removeWorkerOnError?: boolean; | ||
| /** | ||
| * If true, the worker will be recreated when it errors. | ||
| * The original worker will remain unless `removeWorkerOnError` is also set to true. | ||
| */ | ||
| recreateWorkerOnError?: boolean; | ||
@@ -9,3 +23,3 @@ } | ||
| concurrency: number; | ||
| constructor(createWorker: (i: number) => Promise<Execute<T, R>>, concurrency: number, options?: AsyncWorkerQueueOptions); | ||
| constructor(createWorker: (i: number) => Promise<CreateWorkerResult<T, R>> | CreateWorkerResult<T, R>, concurrency: number, options?: AsyncWorkerQueueOptions); | ||
| /** | ||
@@ -20,4 +34,10 @@ * Initialised the queue if it hasn't been initialised yet. | ||
| enqueue(payload: T): Promise<R>; | ||
| /** | ||
| * Destroys the queue and all workers. | ||
| * | ||
| * You should not use this queue after calling this function. | ||
| */ | ||
| dispose(): Promise<void>; | ||
| } | ||
| export { AsyncWorkerQueue }; |
+21
-2
@@ -90,4 +90,6 @@ var __accessCheck = (obj, member, msg) => { | ||
| for (let i = 0; i < this.concurrency; i++) { | ||
| const worker = yield __privateGet(this, _createWorker).call(this, i); | ||
| __privateGet(this, _workers).add({ | ||
| execute: yield __privateGet(this, _createWorker).call(this, i), | ||
| execute: worker.execute, | ||
| dispose: worker.dispose, | ||
| busy: false, | ||
@@ -114,2 +116,16 @@ index: i | ||
| } | ||
| /** | ||
| * Destroys the queue and all workers. | ||
| * | ||
| * You should not use this queue after calling this function. | ||
| */ | ||
| dispose() { | ||
| return __async(this, null, function* () { | ||
| for (const worker of __privateGet(this, _workers)) { | ||
| yield worker.dispose(); | ||
| } | ||
| __privateSet(this, _workers, /* @__PURE__ */ new Set()); | ||
| __privateSet(this, _queue, new Queue()); | ||
| }); | ||
| } | ||
| }; | ||
@@ -141,7 +157,10 @@ _queue = new WeakMap(); | ||
| if (__privateGet(this, _options).removeWorkerOnError || __privateGet(this, _options).recreateWorkerOnError) { | ||
| yield worker.dispose(); | ||
| __privateGet(this, _workers).delete(worker); | ||
| } | ||
| if (__privateGet(this, _options).recreateWorkerOnError) { | ||
| const newWorker = yield __privateGet(this, _createWorker).call(this, worker.index); | ||
| __privateGet(this, _workers).add({ | ||
| execute: yield __privateGet(this, _createWorker).call(this, worker.index), | ||
| execute: newWorker.execute, | ||
| dispose: newWorker.dispose, | ||
| busy: false, | ||
@@ -148,0 +167,0 @@ index: worker.index |
+1
-1
| { | ||
| "name": "async-worker-queue", | ||
| "version": "0.1.2", | ||
| "version": "0.2.0", | ||
| "description": "A queue for async tasks that can be run in parallel with a maximum concurrency", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
19092
11.25%415
16.25%