refactor: remove unneeded encapsulation around tasks usage handling
[poolifier.git] / src / pools / pool-worker.ts
1 /**
2 * Callback invoked if the worker has received a message.
3 */
4 export type MessageHandler<Worker> = (this: Worker, m: unknown) => void
5
6 /**
7 * Callback invoked if the worker raised an error.
8 */
9 export type ErrorHandler<Worker> = (this: Worker, e: Error) => void
10
11 /**
12 * Callback invoked when the worker has started successfully.
13 */
14 export type OnlineHandler<Worker> = (this: Worker) => void
15
16 /**
17 * Callback invoked when the worker exits successfully.
18 */
19 export type ExitHandler<Worker> = (this: Worker, code: number) => void
20
21 /**
22 * Interface that describes the minimum required implementation of listener events for a pool worker.
23 */
24 export interface IPoolWorker {
25 /**
26 * Register an event listener.
27 *
28 * @param event - The event.
29 * @param handler - The event listener.
30 */
31 on: ((event: 'message', handler: MessageHandler<this>) => void) &
32 ((event: 'error', handler: ErrorHandler<this>) => void) &
33 ((event: 'online', handler: OnlineHandler<this>) => void) &
34 ((event: 'exit', handler: ExitHandler<this>) => void)
35 /**
36 * Register a listener to the exit event that will only performed once.
37 *
38 * @param event - `'exit'`.
39 * @param handler - The exit handler.
40 */
41 once: (event: 'exit', handler: ExitHandler<this>) => void
42 }