1 import type { EventLoopUtilization
} from
'node:perf_hooks'
2 import type { MessagePort
, TransferListItem
} from
'node:worker_threads'
3 import type { AsyncResource
} from
'node:async_hooks'
4 import type { KillBehavior
} from
'./worker/worker-options.js'
9 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
11 export interface WorkerError
<Data
= unknown
> {
13 * Task function name triggering the error.
19 readonly message
: string
21 * Data triggering the error.
31 export interface TaskPerformance
{
37 * Task performance timestamp.
39 readonly timestamp
: number
43 readonly runTime
?: number
45 * Task event loop utilization.
47 readonly elu
?: EventLoopUtilization
51 * Worker task performance statistics computation settings.
55 export interface WorkerStatistics
{
57 * Whether the worker computes the task runtime or not.
59 readonly runTime
: boolean
61 * Whether the worker computes the task event loop utilization (ELU) or not.
67 * Message object that is passed as a task between main worker and worker.
69 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
72 export interface Task
<Data
= unknown
> {
76 readonly name
?: string
78 * Task input data that will be passed to the worker.
82 * Array of transferable objects.
84 readonly transferList
?: TransferListItem
[]
88 readonly timestamp
?: number
92 readonly taskId
?: string
96 * Message object that is passed between main worker and worker.
98 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
99 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
102 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
107 readonly workerId
?: number
111 readonly kill
?: KillBehavior
| true | 'success' | 'failure'
115 readonly workerError
?: WorkerError
<ErrorData
>
119 readonly taskPerformance
?: TaskPerformance
121 * Task function operation:
122 * - `'add'` - Add a task function.
123 * - `'remove'` - Remove a task function.
124 * - `'default'` - Set a task function as default.
126 readonly taskFunctionOperation
?: 'add' | 'remove' | 'default'
128 * Whether the task function operation is successful or not.
130 readonly taskFunctionOperationStatus
?: boolean
132 * Task function serialized to string.
134 readonly taskFunction
?: string
136 * Task function name.
138 readonly taskFunctionName
?: string
140 * Task function names.
142 readonly taskFunctionNames
?: string[]
144 * Whether the worker computes the given statistics or not.
146 readonly statistics
?: WorkerStatistics
148 * Whether the worker is ready or not.
150 readonly ready
?: boolean
152 * Whether the worker starts or stops its activity check.
154 readonly checkActive
?: boolean
158 readonly port
?: MessagePort
162 * An object holding the task execution response promise resolve/reject callbacks.
164 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
167 export interface PromiseResponseWrapper
<Response
= unknown
> {
169 * Resolve callback to fulfill the promise.
171 readonly resolve
: (value
: Response
| PromiseLike
<Response
>) => void
173 * Reject callback to reject the promise.
175 readonly reject
: (reason
?: unknown
) => void
177 * The worker node key executing the task.
179 readonly workerNodeKey
: number
181 * The asynchronous resource used to track the task execution.
183 readonly asyncResource
?: AsyncResource
186 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }