build: enforce strict deprecation checks
[poolifier.git] / src / worker / thread-worker.ts
CommitLineData
838898f1 1import type { MessagePort } from 'worker_threads'
fa699c42 2import { isMainThread, parentPort } from 'worker_threads'
deb85c12 3import type { MessageValue } from '../utility-types'
4f3c3d89 4import { EMPTY_OBJECT_LITERAL } from '../utils'
c97c7edb 5import { AbstractWorker } from './abstract-worker'
325f50bc 6import type { WorkerOptions } from './worker-options'
a32e02ba 7
a32e02ba 8/**
729c563d 9 * A thread worker used by a poolifier `ThreadPool`.
4ade5f1f 10 *
729c563d
S
11 * When this worker is inactive for more than the given `maxInactiveTime`,
12 * it will send a termination request to its main thread.
13 *
14 * If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
15 * but the minimum number of workers will be guaranteed.
16 *
38e795c1
JB
17 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
18 * @typeParam Response - Type of response the worker sends back to the main thread. This can only be serializable data.
4ade5f1f 19 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
a32e02ba 20 * @since 0.0.1
21 */
d3c8a1a8 22export class ThreadWorker<
deb85c12
JB
23 Data = unknown,
24 Response = unknown
d3c8a1a8 25> extends AbstractWorker<MessagePort, Data, Response> {
729c563d
S
26 /**
27 * Constructs a new poolifier thread worker.
28 *
38e795c1
JB
29 * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
30 * @param opts - Options for the worker.
729c563d 31 */
4f3c3d89
JB
32 public constructor (
33 fn: (data: Data) => Response,
34 opts: WorkerOptions = EMPTY_OBJECT_LITERAL
35 ) {
e088a00c 36 super('worker-thread-pool:poolifier', isMainThread, fn, parentPort, opts)
106744f7 37 }
7784f548 38
38e795c1 39 /** {@inheritDoc} */
c97c7edb
S
40 protected sendToMainWorker (message: MessageValue<Response>): void {
41 this.getMainWorker().postMessage(message)
7784f548 42 }
a32e02ba 43}