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 * Array of transferable objects.
106 readonly transferList
?: readonly TransferListItem
[]
110 readonly timestamp
?: number
114 readonly taskId
?: string
118 * Message object that is passed between main worker and worker.
120 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
121 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
124 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
129 readonly workerId
?: number
133 readonly kill
?: KillBehavior
| true | 'success' | 'failure'
137 readonly workerError
?: WorkerError
<ErrorData
>
141 readonly taskPerformance
?: TaskPerformance
143 * Task function operation:
144 * - `'add'` - Add a task function.
145 * - `'remove'` - Remove a task function.
146 * - `'default'` - Set a task function as default.
148 readonly taskFunctionOperation
?: 'add' | 'remove' | 'default'
150 * Whether the task function operation is successful or not.
152 readonly taskFunctionOperationStatus
?: boolean
154 * Task function properties.
156 readonly taskFunctionProperties
?: TaskFunctionProperties
158 * Task function serialized to string.
160 readonly taskFunction
?: string
162 * Task functions properties.
164 readonly taskFunctionsProperties
?: TaskFunctionProperties
[]
166 * Whether the worker computes the given statistics or not.
168 readonly statistics
?: WorkerStatistics
170 * Whether the worker is ready or not.
172 readonly ready
?: boolean
174 * Whether the worker starts or stops its activity check.
176 readonly checkActive
?: boolean
180 readonly port
?: MessagePort
184 * An object holding the task execution response promise resolve/reject callbacks.
186 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
189 export interface PromiseResponseWrapper
<Response
= unknown
> {
191 * Resolve callback to fulfill the promise.
193 readonly resolve
: (value
: Response
| PromiseLike
<Response
>) => void
195 * Reject callback to reject the promise.
197 readonly reject
: (reason
?: unknown
) => void
199 * The worker node key executing the task.
201 readonly workerNodeKey
: number
203 * The asynchronous resource used to track the task execution.
205 readonly asyncResource
?: AsyncResource
208 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }