1 import type { MessageChannel
} from
'node:worker_threads'
2 import type { CircularArray
} from
'../circular-array'
3 import type { Task
} from
'../utility-types'
6 * Callback invoked when the worker has started successfully.
8 * @typeParam Worker - Type of worker.
10 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
13 * Callback invoked if the worker has received a message.
15 * @typeParam Worker - Type of worker.
17 export type MessageHandler
<Worker
extends IWorker
> = (
23 * Callback invoked if the worker raised an error.
25 * @typeParam Worker - Type of worker.
27 export type ErrorHandler
<Worker
extends IWorker
> = (
33 * Callback invoked when the worker exits successfully.
35 * @typeParam Worker - Type of worker.
37 export type ExitHandler
<Worker
extends IWorker
> = (
43 * Measurement statistics.
47 export interface MeasurementStatistics
{
49 * Measurement aggregate.
53 * Measurement minimum.
57 * Measurement maximum.
61 * Measurement average.
69 * Measurement history.
71 readonly history
: CircularArray
<number>
75 * Event loop utilization measurement statistics.
79 export interface EventLoopUtilizationMeasurementStatistics
{
80 readonly idle
: MeasurementStatistics
81 readonly active
: MeasurementStatistics
90 export interface TaskStatistics
{
92 * Number of executed tasks.
96 * Number of executing tasks.
100 * Number of queued tasks.
102 readonly queued
: number
104 * Maximum number of queued tasks.
106 readonly maxQueued
?: number
108 * Number of stolen tasks.
112 * Number of failed tasks.
118 * Enumeration of worker types.
120 export const WorkerTypes
= Object.freeze({
128 export type WorkerType
= keyof
typeof WorkerTypes
131 * Worker information.
135 export interface WorkerInfo
{
139 readonly id
: number | undefined
143 readonly type: WorkerType
153 * Task function names.
155 taskFunctionNames
?: string[]
159 * Worker usage statistics.
163 export interface WorkerUsage
{
167 readonly tasks
: TaskStatistics
169 * Tasks runtime statistics.
171 readonly runTime
: MeasurementStatistics
173 * Tasks wait time statistics.
175 readonly waitTime
: MeasurementStatistics
177 * Tasks event loop utilization statistics.
179 readonly elu
: EventLoopUtilizationMeasurementStatistics
183 * Worker choice strategy data.
187 export interface StrategyData
{
188 virtualTaskEndTimestamp
?: number
194 export interface IWorker
{
199 readonly threadId
?: number
201 * Registers an event listener.
203 * @param event - The event.
204 * @param handler - The event handler.
206 readonly on
: ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
207 ((event
: 'message', handler
: MessageHandler
<this>) => void) &
208 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
209 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
211 * Registers a listener to the exit event that will only be performed once.
213 * @param event - The `'exit'` event.
214 * @param handler - The exit handler.
216 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
220 * Worker node event detail.
224 export interface WorkerNodeEventDetail
{
229 * Worker node interface.
231 * @typeParam Worker - Type of worker.
232 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
235 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
236 extends EventTarget
{
240 readonly worker
: Worker
244 readonly info
: WorkerInfo
246 * Worker usage statistics.
248 readonly usage
: WorkerUsage
250 * Worker choice strategy data.
251 * This is used to store data that are specific to the worker choice strategy.
253 strategyData
?: StrategyData
255 * Message channel (worker_threads only).
257 readonly messageChannel
?: MessageChannel
259 * Tasks queue back pressure size.
260 * This is the number of tasks that can be enqueued before the worker node has back pressure.
262 tasksQueueBackPressureSize
: number
266 * @returns The tasks queue size.
268 readonly tasksQueueSize
: () => number
272 * @param task - The task to queue.
273 * @returns The tasks queue size.
275 readonly enqueueTask
: (task
: Task
<Data
>) => number
277 * Prepends a task to the tasks queue.
279 * @param task - The task to prepend.
280 * @returns The tasks queue size.
282 readonly unshiftTask
: (task
: Task
<Data
>) => number
286 * @returns The dequeued task.
288 readonly dequeueTask
: () => Task
<Data
> | undefined
290 * Pops a task from the tasks queue.
292 * @returns The popped task.
294 readonly popTask
: () => Task
<Data
> | undefined
296 * Clears tasks queue.
298 readonly clearTasksQueue
: () => void
300 * Whether the worker node has back pressure (i.e. its tasks queue is full).
302 * @returns `true` if the worker node has back pressure, `false` otherwise.
304 readonly hasBackPressure
: () => boolean
306 * Resets usage statistics.
308 readonly resetUsage
: () => void
310 * Closes communication channel.
312 readonly closeChannel
: () => void
314 * Gets task function worker usage statistics.
316 * @param name - The task function name.
317 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
319 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
321 * Deletes task function worker usage statistics.
323 * @param name - The task function name.
324 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
326 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean