1 import type { AsyncResource
} from
'node:async_hooks'
2 import type { EventLoopUtilization
} from
'node:perf_hooks'
3 import type { MessagePort
, TransferListItem
} from
'node:worker_threads'
5 import type { WorkerChoiceStrategy
} from
'./pools/selection-strategies/selection-strategies-types.js'
6 import type { KillBehavior
} from
'./worker/worker-options.js'
11 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
13 export interface WorkerError
<Data
= unknown
> {
15 * Task function name triggering the error.
21 readonly message
: string
23 * Data triggering the error.
33 export interface TaskPerformance
{
39 * Task performance timestamp.
41 readonly timestamp
: number
45 readonly runTime
?: number
47 * Task event loop utilization.
49 readonly elu
?: EventLoopUtilization
53 * Worker task performance statistics computation settings.
57 export interface WorkerStatistics
{
59 * Whether the worker computes the task runtime or not.
61 readonly runTime
: boolean
63 * Whether the worker computes the task event loop utilization (ELU) or not.
69 * Task function properties.
73 export interface TaskFunctionProperties
{
79 * Task function priority. Lower values have higher priority.
83 * Task function worker choice strategy.
85 strategy
?: WorkerChoiceStrategy
89 * Message object that is passed as a task between main worker and worker.
91 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
94 export interface Task
<Data
= unknown
> {
98 readonly name
?: string
100 * Task input data that will be passed to the worker.
104 * Task priority. Lower values have higher priority.
108 readonly priority
?: number
110 * Task worker choice strategy.
112 readonly strategy
?: WorkerChoiceStrategy
114 * Array of transferable objects.
116 readonly transferList
?: readonly TransferListItem
[]
120 readonly timestamp
?: number
124 readonly taskId
?: `${string}-${string}-${string}-${string}-${string}`
128 * Message object that is passed between main worker and worker.
130 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
131 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
134 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
139 readonly workerId
?: number
143 readonly kill
?: KillBehavior
| true | 'success' | 'failure'
147 readonly workerError
?: WorkerError
<ErrorData
>
151 readonly taskPerformance
?: TaskPerformance
153 * Task function operation:
154 * - `'add'` - Add a task function.
155 * - `'remove'` - Remove a task function.
156 * - `'default'` - Set a task function as default.
158 readonly taskFunctionOperation
?: 'add' | 'remove' | 'default'
160 * Whether the task function operation is successful or not.
162 readonly taskFunctionOperationStatus
?: boolean
164 * Task function properties.
166 readonly taskFunctionProperties
?: TaskFunctionProperties
168 * Task function serialized to string.
170 readonly taskFunction
?: string
172 * Task functions properties.
174 readonly taskFunctionsProperties
?: TaskFunctionProperties
[]
176 * Whether the worker computes the given statistics or not.
178 readonly statistics
?: WorkerStatistics
180 * Whether the worker is ready or not.
182 readonly ready
?: boolean
184 * Whether the worker starts or stops its activity check.
186 readonly checkActive
?: boolean
190 readonly port
?: MessagePort
194 * An object holding the task execution response promise resolve/reject callbacks.
196 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
199 export interface PromiseResponseWrapper
<Response
= unknown
> {
201 * Resolve callback to fulfill the promise.
203 readonly resolve
: (value
: Response
| PromiseLike
<Response
>) => void
205 * Reject callback to reject the promise.
207 readonly reject
: (reason
?: unknown
) => void
209 * The worker node key executing the task.
211 readonly workerNodeKey
: number
213 * The asynchronous resource used to track the task execution.
215 readonly asyncResource
?: AsyncResource
218 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }