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