1 import type { CircularArray
} from
'../circular-array'
2 import type { Queue
} from
'../queue'
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 * Message object that is passed as a task between main worker and worker.
36 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
39 export interface Task
<Data
= unknown
> {
43 readonly name
?: string
45 * Task input data that will be passed to the worker.
51 readonly timestamp
?: number
59 * Measurement statistics.
63 export interface MeasurementStatistics
{
65 * Measurement aggregate.
69 * Measurement average.
77 * Measurement history.
79 history
: CircularArray
<number>
83 * Event loop utilization measurement statistics.
87 export interface EventLoopUtilizationMeasurementStatistics
{
88 idle
: MeasurementStatistics
89 active
: MeasurementStatistics
98 export interface TaskStatistics
{
100 * Number of executed tasks.
104 * Number of executing tasks.
108 * Number of queued tasks.
110 readonly queued
: number
112 * Maximum number of queued tasks.
114 readonly maxQueued
: number
116 * Number of failed tasks.
122 * Worker usage statistics.
126 export interface WorkerUsage
{
130 tasks
: TaskStatistics
132 * Tasks runtime statistics.
134 runTime
: MeasurementStatistics
136 * Tasks wait time statistics.
138 waitTime
: MeasurementStatistics
140 * Tasks event loop utilization statistics.
142 elu
: EventLoopUtilizationMeasurementStatistics
148 export interface IWorker
{
150 * Register an event listener.
152 * @param event - The event.
153 * @param handler - The event handler.
155 on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
156 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
157 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
158 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
160 * Register a listener to the exit event that will only be performed once.
162 * @param event - `'exit'`.
163 * @param handler - The exit handler.
165 once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
169 * Worker node interface.
171 * @typeParam Worker - Type of worker.
172 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
175 export interface WorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
177 * Worker node worker.
179 readonly worker
: Worker
181 * Worker node worker usage statistics.
183 workerUsage
: WorkerUsage
185 * Worker node tasks queue.
187 readonly tasksQueue
: Queue
<Task
<Data
>>