build(deps-dev): apply updates
[poolifier.git] / src / worker / thread-worker.ts
CommitLineData
f59e1027
JB
1import {
2 type MessagePort,
3 isMainThread,
4 parentPort,
5 threadId
6} from 'node:worker_threads'
b6b32453 7import type { MessageValue } from '../utility-types'
c97c7edb 8import { AbstractWorker } from './abstract-worker'
325f50bc 9import type { WorkerOptions } from './worker-options'
b6b32453 10import type { TaskFunctions, WorkerFunction } from './worker-functions'
a32e02ba 11
a32e02ba 12/**
729c563d 13 * A thread worker used by a poolifier `ThreadPool`.
4ade5f1f 14 *
729c563d
S
15 * When this worker is inactive for more than the given `maxInactiveTime`,
16 * it will send a termination request to its main thread.
17 *
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.
20 *
e102732c
JB
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.
4ade5f1f 23 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
a32e02ba 24 * @since 0.0.1
25 */
d3c8a1a8 26export class ThreadWorker<
deb85c12
JB
27 Data = unknown,
28 Response = unknown
d3c8a1a8 29> extends AbstractWorker<MessagePort, Data, Response> {
85aeb3f3 30 /**
2761efb4 31 * Message port used to communicate with the main worker.
85aeb3f3
JB
32 */
33 private port!: MessagePort
729c563d
S
34 /**
35 * Constructs a new poolifier thread worker.
36 *
82888165 37 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
38e795c1 38 * @param opts - Options for the worker.
729c563d 39 */
d4aeae5a 40 public constructor (
82888165
JB
41 taskFunctions:
42 | WorkerFunction<Data, Response>
43 | TaskFunctions<Data, Response>,
d4aeae5a
JB
44 opts: WorkerOptions = {}
45 ) {
82888165
JB
46 super(
47 'worker-thread-pool:poolifier',
48 isMainThread,
e102732c 49 parentPort as MessagePort,
85aeb3f3 50 taskFunctions,
82888165
JB
51 opts
52 )
f59e1027
JB
53 }
54
85aeb3f3
JB
55 /** @inheritDoc */
56 protected handleReadyMessage (message: MessageValue<Data>): void {
57 if (
6c0c538c 58 !this.isMain &&
85aeb3f3
JB
59 message.workerId === this.id &&
60 message.ready != null &&
61 message.port != null
62 ) {
6c0c538c
JB
63 this.port = message.port
64 this.port.on('message', this.messageListener.bind(this))
65 this.sendToMainWorker({ ready: true, workerId: this.id })
85aeb3f3
JB
66 }
67 }
68
984dc9c8
JB
69 /** @inheritDoc */
70 protected handleKillMessage (message: MessageValue<Data, unknown>): void {
71 super.handleKillMessage(message)
72 this.port?.unref()
73 this.port?.close()
74 }
75
85aeb3f3 76 /** @inheritDoc */
f59e1027
JB
77 protected get id (): number {
78 return threadId
106744f7 79 }
7784f548 80
afc003b2 81 /** @inheritDoc */
c97c7edb 82 protected sendToMainWorker (message: MessageValue<Response>): void {
85aeb3f3 83 this.port.postMessage(message)
7784f548 84 }
985d0e79
JB
85
86 /** @inheritDoc */
87 protected handleError (e: Error | string): string {
88 return e as string
89 }
a32e02ba 90}