fix: register worker message handler only once the worker is ready
[poolifier.git] / 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 })
     }
   }