Throw error if worker was not in tasks
authorShinigami92 <chrissi92@hotmail.de>
Mon, 8 Feb 2021 16:44:54 +0000 (17:44 +0100)
committerShinigami92 <chrissi92@hotmail.de>
Mon, 8 Feb 2021 17:26:53 +0000 (18:26 +0100)
src/fixed.ts

index 4317100830e1a94aa19834c4f3764359ac97960d..3ca43cdb1e5e13f0033427e153cdfa80025abca0 100644 (file)
@@ -91,7 +91,12 @@ export default class FixedThreadPool<Data = any, Response = any> {
   public execute (data: Data): Promise<Response> {
     // 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<Data = any, Response = any> {
       }): 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)
         }