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