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 * Number of failed tasks.
118 * Worker usage statistics.
122 export interface WorkerUsage
{
126 tasks
: TaskStatistics
128 * Tasks runtime statistics.
130 runTime
: MeasurementStatistics
132 * Tasks wait time statistics.
134 waitTime
: MeasurementStatistics
136 * Tasks event loop utilization statistics.
138 elu
: EventLoopUtilizationMeasurementStatistics
144 export interface IWorker
{
146 * Register an event listener.
148 * @param event - The event.
149 * @param handler - The event handler.
151 on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
152 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
153 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
154 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
156 * Register a listener to the exit event that will only be performed once.
158 * @param event - `'exit'`.
159 * @param handler - The exit handler.
161 once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
165 * Worker node interface.
167 * @typeParam Worker - Type of worker.
168 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
171 export interface WorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
173 * Worker node worker.
175 readonly worker
: Worker
177 * Worker node worker usage statistics.
179 workerUsage
: WorkerUsage
181 * Worker node tasks queue.
183 readonly tasksQueue
: Queue
<Task
<Data
>>