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