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 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
11 * Callback invoked if the worker has received a message.
13 export type MessageHandler
<Worker
extends IWorker
> = (
19 * Callback invoked if the worker raised an error.
21 export type ErrorHandler
<Worker
extends IWorker
> = (
27 * Callback invoked when the worker exits successfully.
29 export type ExitHandler
<Worker
extends IWorker
> = (
35 * Measurement statistics.
39 export interface MeasurementStatistics
{
41 * Measurement aggregate.
45 * Measurement minimum.
49 * Measurement maximum.
53 * Measurement average.
61 * Measurement history.
63 readonly history
: CircularArray
<number>
67 * Event loop utilization measurement statistics.
71 export interface EventLoopUtilizationMeasurementStatistics
{
72 readonly idle
: MeasurementStatistics
73 readonly active
: MeasurementStatistics
82 export interface TaskStatistics
{
84 * Number of executed tasks.
88 * Number of executing tasks.
92 * Number of queued tasks.
94 readonly queued
: number
96 * Maximum number of queued tasks.
98 readonly maxQueued
?: number
100 * Number of stolen tasks.
104 * Number of failed tasks.
110 * Enumeration of worker types.
112 export const WorkerTypes
= Object.freeze({
120 export type WorkerType
= keyof
typeof WorkerTypes
123 * Worker information.
127 export interface WorkerInfo
{
131 readonly id
: number | undefined
135 readonly type: WorkerType
145 * Task function names.
147 taskFunctionNames
?: string[]
151 * Worker usage statistics.
155 export interface WorkerUsage
{
159 readonly tasks
: TaskStatistics
161 * Tasks runtime statistics.
163 readonly runTime
: MeasurementStatistics
165 * Tasks wait time statistics.
167 readonly waitTime
: MeasurementStatistics
169 * Tasks event loop utilization statistics.
171 readonly elu
: EventLoopUtilizationMeasurementStatistics
175 * Worker choice strategy data.
179 export interface StrategyData
{
180 virtualTaskEndTimestamp
?: number
186 export interface IWorker
{
191 readonly threadId
?: number
193 * Registers an event listener.
195 * @param event - The event.
196 * @param handler - The event handler.
198 readonly on
: ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
199 ((event
: 'message', handler
: MessageHandler
<this>) => void) &
200 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
201 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
203 * Registers a listener to the exit event that will only be performed once.
205 * @param event - The `'exit'` event.
206 * @param handler - The exit handler.
208 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
212 * Worker node event detail.
216 export interface WorkerNodeEventDetail
{
221 * Worker node interface.
223 * @typeParam Worker - Type of worker.
224 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
227 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
228 extends EventTarget
{
232 readonly worker
: Worker
236 readonly info
: WorkerInfo
238 * Worker usage statistics.
240 readonly usage
: WorkerUsage
242 * Worker choice strategy data.
243 * This is used to store data that is specific to the worker choice strategy.
245 strategyData
?: StrategyData
247 * Message channel (worker_threads only).
249 readonly messageChannel
?: MessageChannel
251 * Tasks queue back pressure size.
252 * This is the number of tasks that can be enqueued before the worker node has back pressure.
254 tasksQueueBackPressureSize
: number
258 * @returns The tasks queue size.
260 readonly tasksQueueSize
: () => number
264 * @param task - The task to queue.
265 * @returns The tasks queue size.
267 readonly enqueueTask
: (task
: Task
<Data
>) => number
269 * Prepends a task to the tasks queue.
271 * @param task - The task to prepend.
272 * @returns The tasks queue size.
274 readonly unshiftTask
: (task
: Task
<Data
>) => number
278 * @returns The dequeued task.
280 readonly dequeueTask
: () => Task
<Data
> | undefined
282 * Pops a task from the tasks queue.
284 * @returns The popped task.
286 readonly popTask
: () => Task
<Data
> | undefined
288 * Clears tasks queue.
290 readonly clearTasksQueue
: () => void
292 * Whether the worker node has back pressure (i.e. its tasks queue is full).
294 * @returns `true` if the worker node has back pressure, `false` otherwise.
296 readonly hasBackPressure
: () => boolean
298 * Resets usage statistics.
300 readonly resetUsage
: () => void
302 * Closes communication channel.
304 readonly closeChannel
: () => void
306 * Gets task function worker usage statistics.
308 * @param name - The task function name.
309 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
311 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
313 * Deletes task function worker usage statistics.
315 * @param name - The task function name.
316 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
318 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean