build(deps-dev): apply updates
[poolifier.git] / src / utility-types.ts
CommitLineData
ded253e2 1import type { AsyncResource } from 'node:async_hooks'
62c15a68 2import type { EventLoopUtilization } from 'node:perf_hooks'
7d91a8cd 3import type { MessagePort, TransferListItem } from 'node:worker_threads'
ded253e2 4
31847469 5import type { WorkerChoiceStrategy } from './pools/selection-strategies/selection-strategies-types.js'
d35e5717 6import type { KillBehavior } from './worker/worker-options.js'
838898f1 7
213e817d 8/**
6703b9f4 9 * Worker error.
213e817d 10 *
e102732c 11 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
213e817d 12 */
6703b9f4 13export interface WorkerError<Data = unknown> {
ff128cc9 14 /**
6703b9f4 15 * Task function name triggering the error.
ff128cc9
JB
16 */
17 readonly name: string
82f36766
JB
18 /**
19 * Error message.
20 */
eb7bf744 21 readonly message: string
82f36766 22 /**
ff128cc9 23 * Data triggering the error.
82f36766 24 */
eb7bf744 25 readonly data?: Data
82f36766
JB
26}
27
d715b7bc
JB
28/**
29 * Task performance.
f03062df
JB
30 *
31 * @internal
d715b7bc
JB
32 */
33export interface TaskPerformance {
197b4aa5
JB
34 /**
35 * Task name.
36 */
37 readonly name: string
d715b7bc
JB
38 /**
39 * Task performance timestamp.
40 */
eb7bf744 41 readonly timestamp: number
d715b7bc
JB
42 /**
43 * Task runtime.
44 */
eb7bf744 45 readonly runTime?: number
d715b7bc
JB
46 /**
47 * Task event loop utilization.
48 */
eb7bf744 49 readonly elu?: EventLoopUtilization
d715b7bc
JB
50}
51
b6b32453 52/**
5b49e864 53 * Worker task performance statistics computation settings.
f03062df
JB
54 *
55 * @internal
b6b32453
JB
56 */
57export interface WorkerStatistics {
5b49e864
JB
58 /**
59 * Whether the worker computes the task runtime or not.
60 */
61 readonly runTime: boolean
62 /**
63 * Whether the worker computes the task event loop utilization (ELU) or not.
64 */
65 readonly elu: boolean
b6b32453
JB
66}
67
31847469
JB
68/**
69 * Task function properties.
31847469
JB
70 */
71export interface TaskFunctionProperties {
72 /**
73 * Task function name.
74 */
2be9b405 75 readonly name: string
31847469
JB
76 /**
77 * Task function priority. Lower values have higher priority.
78 */
2be9b405 79 readonly priority?: number
31847469
JB
80 /**
81 * Task function worker choice strategy.
82 */
2be9b405 83 readonly strategy?: WorkerChoiceStrategy
31847469
JB
84}
85
5c4d16da
JB
86/**
87 * Message object that is passed as a task between main worker and worker.
88 *
89 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
90 * @internal
91 */
92export interface Task<Data = unknown> {
5c4d16da
JB
93 /**
94 * Task name.
95 */
96 readonly name?: string
97 /**
98 * Task input data that will be passed to the worker.
99 */
100 readonly data?: Data
95d1a734
JB
101 /**
102 * Task priority. Lower values have higher priority.
103 *
104 * @defaultValue 0
105 */
106 readonly priority?: number
107 /**
108 * Task worker choice strategy.
109 */
110 readonly strategy?: WorkerChoiceStrategy
7d91a8cd
JB
111 /**
112 * Array of transferable objects.
113 */
6a3ecc50 114 readonly transferList?: readonly TransferListItem[]
5c4d16da
JB
115 /**
116 * Timestamp.
117 */
118 readonly timestamp?: number
119 /**
7629bdf1 120 * Task UUID.
5c4d16da 121 */
95d1a734 122 readonly taskId?: `${string}-${string}-${string}-${string}-${string}`
5c4d16da
JB
123}
124
729c563d 125/**
02706357 126 * Message object that is passed between main worker and worker.
c319c66b 127 *
e102732c
JB
128 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
129 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
71ebe93b 130 * @internal
729c563d 131 */
6677a3d3
JB
132export interface MessageValue<Data = unknown, ErrorData = unknown>
133 extends Task<Data> {
7379799c
JB
134 /**
135 * Worker id.
136 */
137 readonly workerId?: number
729c563d
S
138 /**
139 * Kill code.
140 */
1e3214b6 141 readonly kill?: KillBehavior | true | 'success' | 'failure'
729c563d 142 /**
6703b9f4 143 * Worker error.
729c563d 144 */
6703b9f4 145 readonly workerError?: WorkerError<ErrorData>
bf9549ae 146 /**
d715b7bc 147 * Task performance.
62c15a68 148 */
d715b7bc 149 readonly taskPerformance?: TaskPerformance
6703b9f4
JB
150 /**
151 * Task function operation:
6703b9f4 152 * - `'add'` - Add a task function.
16248b23 153 * - `'remove'` - Remove a task function.
6703b9f4
JB
154 * - `'default'` - Set a task function as default.
155 */
edbc15c6
JB
156 readonly taskFunctionOperation?: 'add' | 'remove' | 'default'
157 /**
158 * Whether the task function operation is successful or not.
159 */
6703b9f4
JB
160 readonly taskFunctionOperationStatus?: boolean
161 /**
31847469 162 * Task function properties.
6703b9f4 163 */
31847469 164 readonly taskFunctionProperties?: TaskFunctionProperties
6703b9f4 165 /**
31847469 166 * Task function serialized to string.
6703b9f4 167 */
31847469 168 readonly taskFunction?: string
90d7d101 169 /**
31847469 170 * Task functions properties.
90d7d101 171 */
31847469 172 readonly taskFunctionsProperties?: TaskFunctionProperties[]
b6b32453 173 /**
f59e1027 174 * Whether the worker computes the given statistics or not.
b6b32453
JB
175 */
176 readonly statistics?: WorkerStatistics
f59e1027 177 /**
7c8381eb 178 * Whether the worker is ready or not.
f59e1027 179 */
7c8381eb 180 readonly ready?: boolean
75d3401a 181 /**
b0a4db63 182 * Whether the worker starts or stops its activity check.
75d3401a 183 */
b0a4db63 184 readonly checkActive?: boolean
85aeb3f3
JB
185 /**
186 * Message port.
187 */
188 readonly port?: MessagePort
325f50bc 189}
be0676b3
APA
190
191/**
e5d3809a 192 * An object holding the task execution response promise resolve/reject callbacks.
be0676b3 193 *
e102732c 194 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
c319c66b 195 * @internal
be0676b3 196 */
501aea93 197export interface PromiseResponseWrapper<Response = unknown> {
be0676b3
APA
198 /**
199 * Resolve callback to fulfill the promise.
200 */
d2c73f82 201 readonly resolve: (value: Response | PromiseLike<Response>) => void
be0676b3
APA
202 /**
203 * Reject callback to reject the promise.
204 */
d2c73f82 205 readonly reject: (reason?: unknown) => void
be0676b3 206 /**
e5d3809a 207 * The worker node key executing the task.
be0676b3 208 */
501aea93 209 readonly workerNodeKey: number
f18fd12b
JB
210 /**
211 * The asynchronous resource used to track the task execution.
212 */
213 readonly asyncResource?: AsyncResource
be0676b3 214}
ff3f866a
JB
215
216export type Writable<T> = { -readonly [P in keyof T]: T[P] }