1 import type { CircularArray
} from
'../circular-array'
4 * Callback invoked if the worker has received a message.
6 export type MessageHandler
<Worker
extends IWorker
> = (
12 * Callback invoked if the worker raised an error.
14 export type ErrorHandler
<Worker
extends IWorker
> = (
20 * Callback invoked when the worker has started successfully.
22 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
25 * Callback invoked when the worker exits successfully.
27 export type ExitHandler
<Worker
extends IWorker
> = (
33 * Message object that is passed as a task between main worker and worker.
35 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
38 export interface Task
<Data
= unknown
> {
42 readonly name
?: string
44 * Task input data that will be passed to the worker.
50 readonly timestamp
?: number
58 * Measurement statistics.
62 export interface MeasurementStatistics
{
64 * Measurement aggregate.
68 * Measurement minimum.
72 * Measurement maximum.
76 * Measurement average.
84 * Measurement history.
86 readonly history
: CircularArray
<number>
90 * Event loop utilization measurement statistics.
94 export interface EventLoopUtilizationMeasurementStatistics
{
95 readonly idle
: MeasurementStatistics
96 readonly active
: MeasurementStatistics
105 export interface TaskStatistics
{
107 * Number of executed tasks.
111 * Number of executing tasks.
115 * Number of queued tasks.
117 readonly queued
: number
119 * Maximum number of queued tasks.
121 readonly maxQueued
: number
123 * Number of failed tasks.
129 * Enumeration of worker types.
131 export const WorkerTypes
= Object.freeze({
139 export type WorkerType
= keyof
typeof WorkerTypes
142 * Worker information.
146 export interface WorkerInfo
{
150 readonly id
: number | undefined
166 * Worker usage statistics.
170 export interface WorkerUsage
{
174 readonly tasks
: TaskStatistics
176 * Tasks runtime statistics.
178 readonly runTime
: MeasurementStatistics
180 * Tasks wait time statistics.
182 readonly waitTime
: MeasurementStatistics
184 * Tasks event loop utilization statistics.
186 readonly elu
: EventLoopUtilizationMeasurementStatistics
192 export interface IWorker
{
197 readonly threadId
?: number
199 * Registers an event listener.
201 * @param event - The event.
202 * @param handler - The event handler.
204 readonly on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
205 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
206 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
207 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
209 * Registers a listener to the exit event that will only be performed once.
211 * @param event - `'exit'`.
212 * @param handler - The exit handler.
214 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
218 * Worker node interface.
220 * @typeParam Worker - Type of worker.
221 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
224 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
226 * Worker node worker.
228 readonly worker
: Worker
230 * Worker node worker info.
232 readonly info
: WorkerInfo
234 * Worker node worker usage statistics.
238 * Worker node tasks queue size.
240 * @returns The tasks queue size.
242 readonly tasksQueueSize
: () => number
244 * Worker node enqueue task.
246 * @param task - The task to queue.
247 * @returns The task queue size.
249 readonly enqueueTask
: (task
: Task
<Data
>) => number
251 * Worker node dequeue task.
253 * @returns The dequeued task.
255 readonly dequeueTask
: () => Task
<Data
> | undefined
257 * Worker node clear tasks queue.
259 readonly clearTasksQueue
: () => void
261 * Worker node reset usage statistics .
263 readonly resetUsage
: () => void