refactor: cleanup worket set message handling
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 22 Mar 2024 23:56:50 +0000 (00:56 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 22 Mar 2024 23:56:50 +0000 (00:56 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/WorkerSet.ts

index 7636a167a5548993b68f6a17791f22dc33bfbd5d..1e3000cc2f719722fe6ddded93cae1ca4acc3e45 100644 (file)
@@ -153,16 +153,17 @@ export class WorkerSet<D extends WorkerData, R extends WorkerData> extends Worke
     })
     worker.on('message', this.workerOptions.poolOptions?.messageHandler ?? EMPTY_FUNCTION)
     worker.on('message', (message: WorkerMessage<R>) => {
-      if (this.promiseResponseMap.has(message.uuid)) {
+      const { uuid, event, data } = message
+      if (this.promiseResponseMap.has(uuid)) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        const { resolve, reject, workerSetElement } = this.promiseResponseMap.get(message.uuid)!
-        if (message.event === WorkerMessageEvents.addedWorkerElement) {
+        const { resolve, reject, workerSetElement } = this.promiseResponseMap.get(uuid)!
+        if (event === WorkerMessageEvents.addedWorkerElement) {
           this.emitter?.emit(WorkerSetEvents.elementAdded, this.info)
           workerSetElement.numberOfWorkerElements++
-          resolve(message.data)
-        } else if (message.event === WorkerMessageEvents.workerElementError) {
-          this.emitter?.emit(WorkerSetEvents.elementError, message.data)
-          reject(message.data)
+          resolve(data)
+        } else if (event === WorkerMessageEvents.workerElementError) {
+          this.emitter?.emit(WorkerSetEvents.elementError, data)
+          reject(data)
         }
         this.promiseResponseMap.delete(message.uuid)
       }