build(deps-dev): apply updates
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fc3e6586 1import {
7d91a8cd 2 type TransferListItem,
c3719753 3 type Worker,
65d7a1c9 4 isMainThread
fc3e6586 5} from 'node:worker_threads'
d35e5717
JB
6import type { MessageValue } from '../../utility-types.js'
7import { AbstractPool } from '../abstract-pool.js'
8import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js'
9import { type WorkerType, WorkerTypes } from '../worker.js'
4ade5f1f 10
2889bd70
JB
11/**
12 * Options for a poolifier thread pool.
13 */
14export type ThreadPoolOptions = PoolOptions<Worker>
15
4ade5f1f 16/**
729c563d
S
17 * A thread pool with a fixed number of threads.
18 *
e102732c
JB
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
4ade5f1f
S
21 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
22 * @since 0.0.1
23 */
d3c8a1a8 24export class FixedThreadPool<
deb85c12
JB
25 Data = unknown,
26 Response = unknown
e102732c 27> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 28 /**
729c563d
S
29 * Constructs a new poolifier fixed thread pool.
30 *
38e795c1
JB
31 * @param numberOfThreads - Number of threads for this pool.
32 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
33 * @param opts - Options for this fixed thread pool.
4ade5f1f
S
34 */
35 public constructor (
5c5a1fb7 36 numberOfThreads: number,
c97c7edb 37 filePath: string,
2889bd70 38 opts: ThreadPoolOptions = {},
26ce26ca 39 maximumNumberOfThreads?: number
4ade5f1f 40 ) {
26ce26ca 41 super(numberOfThreads, filePath, opts, maximumNumberOfThreads)
c97c7edb 42 }
4ade5f1f 43
afc003b2 44 /** @inheritDoc */
c97c7edb
S
45 protected isMain (): boolean {
46 return isMainThread
4ade5f1f
S
47 }
48
afc003b2 49 /** @inheritDoc */
aa9eede8
JB
50 protected sendToWorker (
51 workerNodeKey: number,
7d91a8cd
JB
52 message: MessageValue<Data>,
53 transferList?: TransferListItem[]
aa9eede8 54 ): void {
369179f6 55 this.workerNodes[workerNodeKey]?.messageChannel?.port1.postMessage(
fea198e8
JB
56 {
57 ...message,
58 workerId: this.getWorkerInfo(workerNodeKey)?.id
59 } satisfies MessageValue<Data>,
72ae84a2
JB
60 transferList
61 )
85aeb3f3
JB
62 }
63
64 /** @inheritDoc */
aa9eede8 65 protected sendStartupMessageToWorker (workerNodeKey: number): void {
75de9f41 66 const workerNode = this.workerNodes[workerNodeKey]
67f3f2d6 67 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7f0e1334 68 const port2 = workerNode.messageChannel!.port2
e9dd5b66 69 workerNode.worker.postMessage(
85aeb3f3
JB
70 {
71 ready: false,
1851fed0 72 workerId: this.getWorkerInfo(workerNodeKey)?.id,
85aeb3f3 73 port: port2
fea198e8 74 } satisfies MessageValue<Data>,
85aeb3f3
JB
75 [port2]
76 )
77 }
78
79 /** @inheritDoc */
80 protected registerWorkerMessageListener<Message extends Data | Response>(
aa9eede8 81 workerNodeKey: number,
85aeb3f3
JB
82 listener: (message: MessageValue<Message>) => void
83 ): void {
c63a35a0 84 this.workerNodes[workerNodeKey].messageChannel?.port1.on(
fa548cda
JB
85 'message',
86 listener
87 )
4ade5f1f
S
88 }
89
ae036c3e
JB
90 /** @inheritDoc */
91 protected registerOnceWorkerMessageListener<Message extends Data | Response>(
92 workerNodeKey: number,
93 listener: (message: MessageValue<Message>) => void
94 ): void {
c63a35a0 95 this.workerNodes[workerNodeKey].messageChannel?.port1.once(
fa548cda
JB
96 'message',
97 listener
98 )
ae036c3e
JB
99 }
100
101 /** @inheritDoc */
102 protected deregisterWorkerMessageListener<Message extends Data | Response>(
103 workerNodeKey: number,
104 listener: (message: MessageValue<Message>) => void
105 ): void {
c63a35a0 106 this.workerNodes[workerNodeKey].messageChannel?.port1.off(
fa548cda
JB
107 'message',
108 listener
109 )
ae036c3e
JB
110 }
111
9d9fb7b6
JB
112 /** @inheritDoc */
113 protected shallCreateDynamicWorker (): boolean {
114 return false
115 }
116
d0878034 117 /** @inheritDoc */
d1c03778
JB
118 protected checkAndEmitDynamicWorkerCreationEvents (): void {
119 /* noop */
120 }
d0878034 121
afc003b2 122 /** @inheritDoc */
8881ae32 123 protected get type (): PoolType {
6b27d407 124 return PoolTypes.fixed
7c0ba920
JB
125 }
126
184855e6
JB
127 /** @inheritDoc */
128 protected get worker (): WorkerType {
129 return WorkerTypes.thread
130 }
131
afc003b2 132 /** @inheritDoc */
c319c66b 133 protected get busy (): boolean {
c2ade475 134 return this.internalBusy()
7c0ba920 135 }
4ade5f1f 136}