2b3df58361eb28a9d0ccc2f694208bb0e4426c02
6 } from
'node:worker_threads'
7 import type { MessageValue
} from
'../utility-types'
8 import { AbstractWorker
} from
'./abstract-worker'
9 import type { WorkerOptions
} from
'./worker-options'
10 import type { TaskFunctions
, WorkerFunction
} from
'./worker-functions'
13 * A thread worker used by a poolifier `ThreadPool`.
15 * When this worker is inactive for more than the given `maxInactiveTime`,
16 * it will send a termination request to its main thread.
18 * If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
19 * but the minimum number of workers will be guaranteed.
21 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
22 * @typeParam Response - Type of response the worker sends back to the main thread. This can only be structured-cloneable data.
23 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
26 export class ThreadWorker
<
29 > extends AbstractWorker
<MessagePort
, Data
, Response
> {
31 * Constructs a new poolifier thread worker.
33 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
34 * @param opts - Options for the worker.
38 | WorkerFunction
<Data
, Response
>
39 | TaskFunctions
<Data
, Response
>,
40 opts
: WorkerOptions
= {}
43 'worker-thread-pool:poolifier',
46 parentPort
as MessagePort
,
51 protected get
id (): number {
56 protected sendToMainWorker (message
: MessageValue
<Response
>): void {
57 this.getMainWorker().postMessage(message
)