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