1 import type { Worker
as ClusterWorker
} from
'cluster'
2 import type { Worker
as WorkerThread
} from
'worker_threads'
3 import type { Draft
} from
'../utility-types'
6 * Poolifier supported worker type.
8 export type WorkerType
= WorkerThread
& ClusterWorker
& Draft
<MessageChannel
>
11 * Callback invoked if the worker has received a message.
13 export type MessageHandler
<Worker
> = (this: Worker
, m
: unknown
) => void
16 * Callback invoked if the worker raised an error.
18 export type ErrorHandler
<Worker
> = (this: Worker
, e
: Error) => void
21 * Callback invoked when the worker has started successfully.
23 export type OnlineHandler
<Worker
> = (this: Worker
) => void
26 * Callback invoked when the worker exits successfully.
28 export type ExitHandler
<Worker
> = (this: Worker
, code
: number) => void
31 * Interface that describes the minimum required implementation of listener events for a pool worker.
33 export interface IPoolWorker
{
35 * Register a listener to the message event.
37 * @param event `'message'`.
38 * @param handler The message handler.
40 on(event
: 'message', handler
: MessageHandler
<this>): void
42 * Register a listener to the error event.
44 * @param event `'error'`.
45 * @param handler The error handler.
47 on(event
: 'error', handler
: ErrorHandler
<this>): void
49 * Register a listener to the online event.
51 * @param event `'online'`.
52 * @param handler The online handler.
54 on(event
: 'online', handler
: OnlineHandler
<this>): void
56 * Register a listener to the exit event.
58 * @param event `'exit'`.
59 * @param handler The exit handler.
61 on(event
: 'exit', handler
: ExitHandler
<this>): void
63 * Register a listener to the exit event that will only performed once.
65 * @param event `'exit'`.
66 * @param handler The exit handler.
68 once(event
: 'exit', handler
: ExitHandler
<this>): void