fix: register worker message handler only once the worker is ready
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 20 Jul 2023 16:14:09 +0000 (18:14 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 20 Jul 2023 16:14:09 +0000 (18:14 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/cluster-worker.ts

index 0e3bccd3a7c1dad88019f582e9d1cfe97be663cf..4c1e118789d53f7228e51a5b974b80575985705f 100644 (file)
@@ -41,14 +41,12 @@ export class ClusterWorker<
       taskFunctions,
       opts
     )
-    if (!this.isMain) {
-      this.getMainWorker()?.on('message', this.messageListener.bind(this))
-    }
   }
 
   /** @inheritDoc */
   protected handleReadyMessage (message: MessageValue<Data>): void {
     if (!this.isMain && message.workerId === this.id && message.ready != null) {
+      this.getMainWorker()?.on('message', this.messageListener.bind(this))
       this.sendToMainWorker({ ready: true, workerId: this.id })
     }
   }