import {
- type MessagePort,
isMainThread,
+ type MessagePort,
parentPort,
threadId
} from 'node:worker_threads'
-import type { MessageValue } from '../utility-types'
-import { AbstractWorker } from './abstract-worker'
-import type { WorkerOptions } from './worker-options'
-import type { TaskFunction, TaskFunctions } from './task-functions'
+
+import type { MessageValue } from '../utility-types.js'
+import { AbstractWorker } from './abstract-worker.js'
+import type { TaskFunction, TaskFunctions } from './task-functions.js'
+import type { WorkerOptions } from './worker-options.js'
/**
* A thread worker used by a poolifier `ThreadPool`.
/**
* Message port used to communicate with the main worker.
*/
- private port!: MessagePort
+ private port?: MessagePort
+
/**
* Constructs a new poolifier thread worker.
*
taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
opts: WorkerOptions = {}
) {
- super(
- 'worker-thread-pool:poolifier',
- isMainThread,
- parentPort as MessagePort,
- taskFunctions,
- opts
- )
+ super(isMainThread, parentPort, taskFunctions, opts)
}
/** @inheritDoc */
this.port.on('message', this.messageListener.bind(this))
this.sendToMainWorker({
ready: true,
- taskFunctionNames: this.listTaskFunctionNames()
+ taskFunctionsProperties: this.listTaskFunctionsProperties()
})
} catch {
this.sendToMainWorker({
ready: false,
- taskFunctionNames: this.listTaskFunctionNames()
+ taskFunctionsProperties: this.listTaskFunctionsProperties()
})
}
}
}
/** @inheritDoc */
- protected sendToMainWorker (message: MessageValue<Response>): void {
- this.port.postMessage({ ...message, workerId: this.id })
+ protected readonly sendToMainWorker = (
+ message: MessageValue<Response>
+ ): void => {
+ this.port?.postMessage({
+ ...message,
+ workerId: this.id
+ } satisfies MessageValue<Response>)
}
- /** @inheritDoc */
+ /**
+ * @inheritDoc
+ * @override
+ */
protected handleError (error: Error | string): string {
return error as string
}