X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Ffixed.ts;h=3ca43cdb1e5e13f0033427e153cdfa80025abca0;hb=d62d9c9721ba6a0dbf4385eacdc3233713ce4903;hp=4317100830e1a94aa19834c4f3764359ac97960d;hpb=ee99693bf5c34d3acaf39db6d4506297b183e47d;p=poolifier.git 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) }