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.
71 export interface TaskFunctionProperties
{
77 * Task function priority. Lower values have higher priority.
79 readonly priority
?: number
81 * Task function worker choice strategy.
83 readonly strategy
?: WorkerChoiceStrategy
87 * Message object that is passed as a task between main worker and worker.
89 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
92 export interface Task
<Data
= unknown
> {
96 readonly name
?: string
98 * Task input data that will be passed to the worker.
102 * Task priority. Lower values have higher priority.
106 readonly priority
?: number
108 * Task worker choice strategy.
110 readonly strategy
?: WorkerChoiceStrategy
112 * Array of transferable objects.
114 readonly transferList
?: readonly TransferListItem
[]
118 readonly timestamp
?: number
122 readonly taskId
?: `${string}-${string}-${string}-${string}-${string}`
126 * Message object that is passed between main worker and worker.
128 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
129 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
132 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
137 readonly workerId
?: number
141 readonly kill
?: KillBehavior
| true | 'success' | 'failure'
145 readonly workerError
?: WorkerError
<ErrorData
>
149 readonly taskPerformance
?: TaskPerformance
151 * Task function operation:
152 * - `'add'` - Add a task function.
153 * - `'remove'` - Remove a task function.
154 * - `'default'` - Set a task function as default.
156 readonly taskFunctionOperation
?: 'add' | 'remove' | 'default'
158 * Whether the task function operation is successful or not.
160 readonly taskFunctionOperationStatus
?: boolean
162 * Task function properties.
164 readonly taskFunctionProperties
?: TaskFunctionProperties
166 * Task function serialized to string.
168 readonly taskFunction
?: string
170 * Task functions properties.
172 readonly taskFunctionsProperties
?: TaskFunctionProperties
[]
174 * Whether the worker computes the given statistics or not.
176 readonly statistics
?: WorkerStatistics
178 * Whether the worker is ready or not.
180 readonly ready
?: boolean
182 * Whether the worker starts or stops its activity check.
184 readonly checkActive
?: boolean
188 readonly port
?: MessagePort
192 * An object holding the task execution response promise resolve/reject callbacks.
194 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
197 export interface PromiseResponseWrapper
<Response
= unknown
> {
199 * Resolve callback to fulfill the promise.
201 readonly resolve
: (value
: Response
| PromiseLike
<Response
>) => void
203 * Reject callback to reject the promise.
205 readonly reject
: (reason
?: unknown
) => void
207 * The worker node key executing the task.
209 readonly workerNodeKey
: number
211 * The asynchronous resource used to track the task execution.
213 readonly asyncResource
?: AsyncResource
216 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }