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