1 import type { CircularArray
} from
'../circular-array'
2 import type { Task
} from
'../utility-types'
5 * Callback invoked if the worker has received a message.
7 export type MessageHandler
<Worker
extends IWorker
> = (
13 * Callback invoked if the worker raised an error.
15 export type ErrorHandler
<Worker
extends IWorker
> = (
21 * Callback invoked when the worker has started successfully.
23 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
26 * Callback invoked when the worker exits successfully.
28 export type ExitHandler
<Worker
extends IWorker
> = (
34 * Measurement statistics.
38 export interface MeasurementStatistics
{
40 * Measurement aggregate.
44 * Measurement minimum.
48 * Measurement maximum.
52 * Measurement average.
60 * Measurement history.
62 readonly history
: CircularArray
<number>
66 * Event loop utilization measurement statistics.
70 export interface EventLoopUtilizationMeasurementStatistics
{
71 readonly idle
: MeasurementStatistics
72 readonly active
: MeasurementStatistics
81 export interface TaskStatistics
{
83 * Number of executed tasks.
87 * Number of executing tasks.
91 * Number of queued tasks.
93 readonly queued
: number
95 * Maximum number of queued tasks.
97 readonly maxQueued
?: number
99 * Number of failed tasks.
105 * Enumeration of worker types.
107 export const WorkerTypes
= Object.freeze({
115 export type WorkerType
= keyof
typeof WorkerTypes
118 * Worker information.
122 export interface WorkerInfo
{
126 readonly id
: number | undefined
142 * Worker usage statistics.
146 export interface WorkerUsage
{
150 readonly tasks
: TaskStatistics
152 * Tasks runtime statistics.
154 readonly runTime
: MeasurementStatistics
156 * Tasks wait time statistics.
158 readonly waitTime
: MeasurementStatistics
160 * Tasks event loop utilization statistics.
162 readonly elu
: EventLoopUtilizationMeasurementStatistics
168 export interface IWorker
{
173 readonly threadId
?: number
175 * Registers an event listener.
177 * @param event - The event.
178 * @param handler - The event handler.
180 readonly on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
181 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
182 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
183 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
185 * Registers a listener to the exit event that will only be performed once.
187 * @param event - `'exit'`.
188 * @param handler - The exit handler.
190 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
194 * Worker node interface.
196 * @typeParam Worker - Type of worker.
197 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
200 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
204 readonly worker
: Worker
208 readonly info
: WorkerInfo
210 * Worker usage statistics.
216 * @returns The tasks queue size.
218 readonly tasksQueueSize
: () => number
222 * @param task - The task to queue.
223 * @returns The task queue size.
225 readonly enqueueTask
: (task
: Task
<Data
>) => number
229 * @returns The dequeued task.
231 readonly dequeueTask
: () => Task
<Data
> | undefined
233 * Clears tasks queue.
235 readonly clearTasksQueue
: () => void
237 * Resets usage statistics .
239 readonly resetUsage
: () => void
241 * Gets task worker usage statistics.
243 readonly getTaskWorkerUsage
: (name
: string) => WorkerUsage
| undefined