From: Shinigami92 Date: Mon, 8 Feb 2021 16:44:54 +0000 (+0100) Subject: Throw error if worker was not in tasks X-Git-Tag: v2.0.0-beta.2~77^2~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d62d9c9721ba6a0dbf4385eacdc3233713ce4903;hp=ee99693bf5c34d3acaf39db6d4506297b183e47d;p=poolifier.git Throw error if worker was not in tasks --- 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) }