build(deps-dev): apply updates
[poolifier.git] / src / worker / thread-worker.ts
CommitLineData
f59e1027 1import {
f59e1027 2 isMainThread,
ded253e2 3 type MessagePort,
f59e1027 4 parentPort,
3a502712 5 threadId,
f59e1027 6} from 'node:worker_threads'
ded253e2 7
d35e5717
JB
8import type { MessageValue } from '../utility-types.js'
9import { AbstractWorker } from './abstract-worker.js'
d35e5717 10import type { TaskFunction, TaskFunctions } from './task-functions.js'
ded253e2 11import type { WorkerOptions } from './worker-options.js'
a32e02ba 12
a32e02ba 13/**
729c563d 14 * A thread worker used by a poolifier `ThreadPool`.
4ade5f1f 15 *
729c563d
S
16 * When this worker is inactive for more than the given `maxInactiveTime`,
17 * it will send a termination request to its main thread.
18 *
19 * If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
20 * but the minimum number of workers will be guaranteed.
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 32 */
9b5c72ff 33 private port?: MessagePort
49890030 34
729c563d
S
35 /**
36 * Constructs a new poolifier thread worker.
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 (
82ea6492 41 taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
d4aeae5a
JB
42 opts: WorkerOptions = {}
43 ) {
c63a35a0 44 super(isMainThread, parentPort, taskFunctions, opts)
f59e1027
JB
45 }
46
85aeb3f3
JB
47 /** @inheritDoc */
48 protected handleReadyMessage (message: MessageValue<Data>): void {
49 if (
50 message.workerId === this.id &&
f05ed93c 51 message.ready === false &&
85aeb3f3
JB
52 message.port != null
53 ) {
f05ed93c
JB
54 try {
55 this.port = message.port
56 this.port.on('message', this.messageListener.bind(this))
a5d15204
JB
57 this.sendToMainWorker({
58 ready: true,
3a502712 59 taskFunctionsProperties: this.listTaskFunctionsProperties(),
a5d15204 60 })
f05ed93c 61 } catch {
a5d15204
JB
62 this.sendToMainWorker({
63 ready: false,
3a502712 64 taskFunctionsProperties: this.listTaskFunctionsProperties(),
a5d15204 65 })
f05ed93c 66 }
85aeb3f3
JB
67 }
68 }
69
984dc9c8 70 /** @inheritDoc */
d655027b 71 protected handleKillMessage (message: MessageValue<Data>): void {
984dc9c8
JB
72 super.handleKillMessage(message)
73 this.port?.unref()
74 this.port?.close()
75 }
76
85aeb3f3 77 /** @inheritDoc */
f59e1027
JB
78 protected get id (): number {
79 return threadId
106744f7 80 }
7784f548 81
afc003b2 82 /** @inheritDoc */
803f948f
JB
83 protected readonly sendToMainWorker = (
84 message: MessageValue<Response>
85 ): void => {
fea198e8
JB
86 this.port?.postMessage({
87 ...message,
3a502712 88 workerId: this.id,
fea198e8 89 } satisfies MessageValue<Response>)
7784f548 90 }
985d0e79 91
34d8846f
JB
92 /**
93 * @inheritDoc
34d8846f 94 */
646bced2
JB
95 protected handleError (error: Error | string): string {
96 return error as string
985d0e79 97 }
a32e02ba 98}