X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fthread-worker.ts;h=d6a3699220b63fad5dc3191c1b4c1ca72d112bc8;hb=41e3e08eb7fbcde0a69cea17e697aacb222990a6;hp=ec5dea8ef74cc4a1dbac377f3f50eaad21c10b8f;hpb=2761efb4184ad22cef774c7707023c354e8f8f04;p=poolifier.git diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index ec5dea8e..d6a36992 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,19 +53,30 @@ 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, + taskFunctions: this.listTaskFunctions(), + workerId: this.id + }) + } catch { + this.sendToMainWorker({ + ready: false, + taskFunctions: this.listTaskFunctions(), + workerId: this.id + }) + } } } /** @inheritDoc */ - protected handleKillMessage (message: MessageValue): void { + protected handleKillMessage (message: MessageValue): void { super.handleKillMessage(message) this.port?.unref() this.port?.close() @@ -84,7 +93,7 @@ export class ThreadWorker< } /** @inheritDoc */ - protected handleError (e: Error | string): string { - return e as string + protected handleError (error: Error | string): string { + return error as string } }