X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fthread-worker.ts;h=7b92caf67201795797f6e3e06fff4fc593f4ee94;hb=103057eedf4f7b18f009333806d0293bfb23e204;hp=dc628bed08fa4b23f29724f88f41484b0097afef;hpb=d655027ba35d40bd823574f630debc7c3a860638;p=poolifier.git diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index dc628bed..7b92caf6 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -7,7 +7,7 @@ import { import type { MessageValue } from '../utility-types' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' -import type { TaskFunctions, WorkerFunction } from './worker-functions' +import type { TaskFunction, TaskFunctions } from './task-functions' /** * A thread worker used by a poolifier `ThreadPool`. @@ -38,9 +38,7 @@ export class ThreadWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { super( @@ -55,14 +53,23 @@ export class ThreadWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { if ( - !this.isMain && message.workerId === this.id && - message.ready != null && + message.ready === false && message.port != null ) { - this.port = message.port - this.port.on('message', this.messageListener.bind(this)) - this.sendToMainWorker({ ready: true, workerId: this.id }) + try { + this.port = message.port + this.port.on('message', this.messageListener.bind(this)) + this.sendToMainWorker({ + ready: true, + taskFunctionNames: this.listTaskFunctionNames() + }) + } catch { + this.sendToMainWorker({ + ready: false, + taskFunctionNames: this.listTaskFunctionNames() + }) + } } } @@ -80,11 +87,11 @@ export class ThreadWorker< /** @inheritDoc */ protected sendToMainWorker (message: MessageValue): void { - this.port.postMessage(message) + this.port.postMessage({ ...message, workerId: this.id }) } /** @inheritDoc */ - protected handleError (e: Error | string): string { - return e as string + protected handleError (error: Error | string): string { + return error as string } }