1 import type { CircularArray
} from
'../circular-array'
4 * Callback invoked if the worker has received a message.
6 export type MessageHandler
<Worker
> = (this: Worker
, m
: unknown
) => void
9 * Callback invoked if the worker raised an error.
11 export type ErrorHandler
<Worker
> = (this: Worker
, e
: Error) => void
14 * Callback invoked when the worker has started successfully.
16 export type OnlineHandler
<Worker
> = (this: Worker
) => void
19 * Callback invoked when the worker exits successfully.
21 export type ExitHandler
<Worker
> = (this: Worker
, code
: number) => void
24 * Worker task interface.
26 export interface Task
<Data
= unknown
> {
32 * Worker tasks usage statistics.
34 export interface TasksUsage
{
38 runTimeHistory
: CircularArray
<number>
47 export interface IWorker
{
49 * Register an event listener.
51 * @param event - The event.
52 * @param handler - The event listener.
54 on
: ((event
: 'message', handler
: MessageHandler
<this>) => void) &
55 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
56 ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
57 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
59 * Register a listener to the exit event that will only performed once.
61 * @param event - `'exit'`.
62 * @param handler - The exit handler.
64 once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
68 * Worker node interface.
70 export interface WorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
72 tasksUsage
: TasksUsage
73 tasksQueue
: Array<Task
<Data
>>