3bb0ac9d66ef550f254b8535d3fd23d4f369a4f6
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 * Worker task interface.
35 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
38 export interface Task
<Data
= unknown
> {
50 * Worker tasks usage statistics.
52 export interface TasksUsage
{
56 runTimeHistory
: CircularArray
<number>
65 export interface IWorker
{
67 * Register an event listener.
69 * @param event - The event.
70 * @param handler - The event listener.
72 on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
73 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
74 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
75 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
77 * Register a listener to the exit event that will only performed once.
79 * @param event - `'exit'`.
80 * @param handler - The exit handler.
82 once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
86 * Worker node interface.
88 * @typeParam Worker - Type of worker.
89 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
92 export interface WorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
98 * Worker node tasks usage statistics.
100 tasksUsage
: TasksUsage
102 * Worker node tasks queue.
104 tasksQueue
: Array<Task
<Data
>>