From d62d9c9721ba6a0dbf4385eacdc3233713ce4903 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 8 Feb 2021 17:44:54 +0100 Subject: [PATCH] Throw error if worker was not in tasks --- src/fixed.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/fixed.ts b/src/fixed.ts index 43171008..3ca43cdb 100644 --- a/src/fixed.ts +++ b/src/fixed.ts @@ -91,7 +91,12 @@ export default class FixedThreadPool { public execute (data: Data): Promise { // configure worker to handle message with the specified task const worker = this._chooseWorker() - this.tasks.set(worker, (this.tasks.get(worker) ?? 0) + 1) + const previousWorkerIndex = this.tasks.get(worker) + if (previousWorkerIndex !== undefined) { + this.tasks.set(worker, previousWorkerIndex + 1) + } else { + throw Error('Worker could not be found in tasks map') + } const id = ++this._id const res = this._execute(worker, id) worker.postMessage({ data: data || _void, _id: id }) @@ -111,7 +116,12 @@ export default class FixedThreadPool { }): void => { if (message._id === id) { worker.port2?.removeListener('message', listener) - this.tasks.set(worker, (this.tasks.get(worker) ?? 0) - 1) + const previousWorkerIndex = this.tasks.get(worker) + if (previousWorkerIndex !== undefined) { + this.tasks.set(worker, previousWorkerIndex + 1) + } else { + throw Error('Worker could not be found in tasks map') + } if (message.error) reject(message.error) else resolve(message.data) } -- 2.34.1