refactor: cleanup ready message conditions check in worker code
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 22:03:59 +0000 (00:03 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 22:03:59 +0000 (00:03 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/abstract-worker.ts
src/worker/cluster-worker.ts
src/worker/thread-worker.ts

index 77d2e0076f58abbbdf05c33ae994ec4f7cadf44e..36b7d03e379415aa3ec24aa861ae40e12d7f268b 100644 (file)
@@ -73,7 +73,7 @@ export abstract class AbstractWorker<
   public constructor (
     type: string,
     protected readonly isMain: boolean,
-    protected readonly mainWorker: MainWorker,
+    private readonly mainWorker: MainWorker,
     taskFunctions:
     | WorkerFunction<Data, Response>
     | TaskFunctions<Data, Response>,
index c820641ddbb10319b454bf5b25154312703cc446..0e3bccd3a7c1dad88019f582e9d1cfe97be663cf 100644 (file)
@@ -48,8 +48,8 @@ export class ClusterWorker<
 
   /** @inheritDoc */
   protected handleReadyMessage (message: MessageValue<Data>): void {
-    if (message.workerId === this.id && message.ready != null) {
-      !this.isMain && this.sendToMainWorker({ ready: true, workerId: this.id })
+    if (!this.isMain && message.workerId === this.id && message.ready != null) {
+      this.sendToMainWorker({ ready: true, workerId: this.id })
     }
   }
 
index a8658f03a797ed649cbd583a14ed5d924949a235..98d58b5d98162ba2f09ca21a6abaca114123355d 100644 (file)
@@ -55,15 +55,14 @@ export class ThreadWorker<
   /** @inheritDoc */
   protected handleReadyMessage (message: MessageValue<Data>): void {
     if (
+      !this.isMain &&
       message.workerId === this.id &&
       message.ready != null &&
       message.port != null
     ) {
-      if (!this.isMain) {
-        this.port = message.port
-        this.port.on('message', this.messageListener.bind(this))
-        this.sendToMainWorker({ ready: true, workerId: this.id })
-      }
+      this.port = message.port
+      this.port.on('message', this.messageListener.bind(this))
+      this.sendToMainWorker({ ready: true, workerId: this.id })
     }
   }