From 6c0c538c652e072ffa0516bb2826c39b58b9592f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 20 Jul 2023 00:03:59 +0200 Subject: [PATCH] refactor: cleanup ready message conditions check in worker code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 2 +- src/worker/cluster-worker.ts | 4 ++-- src/worker/thread-worker.ts | 9 ++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 77d2e007..36b7d03e 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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 | TaskFunctions, diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index c820641d..0e3bccd3 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -48,8 +48,8 @@ export class ClusterWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): 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 }) } } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index a8658f03..98d58b5d 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -55,15 +55,14 @@ export class ThreadWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): 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 }) } } -- 2.34.1