Optimize WRR implementation
[poolifier.git] / src / pools / pool-worker.ts
CommitLineData
bdaf31cd
JB
1import type { Worker as ClusterWorker } from 'cluster'
2import type { Worker as WorkerThread } from 'worker_threads'
3import type { Draft } from '../utility-types'
4
5/**
6 * Poolifier supported worker type.
7 */
8export type WorkerType = WorkerThread & ClusterWorker & Draft<MessageChannel>
9
10/**
11 * Callback invoked if the worker has received a message.
12 */
13export type MessageHandler<Worker> = (this: Worker, m: unknown) => void
14
15/**
16 * Callback invoked if the worker raised an error.
17 */
18export type ErrorHandler<Worker> = (this: Worker, e: Error) => void
19
20/**
21 * Callback invoked when the worker has started successfully.
22 */
23export type OnlineHandler<Worker> = (this: Worker) => void
24
25/**
26 * Callback invoked when the worker exits successfully.
27 */
28export type ExitHandler<Worker> = (this: Worker, code: number) => void
29
30/**
ea7a90d3 31 * Interface that describes the minimum required implementation of listener events for a pool worker.
bdaf31cd
JB
32 */
33export interface IPoolWorker {
bdaf31cd
JB
34 /**
35 * Register a listener to the message event.
36 *
37 * @param event `'message'`.
38 * @param handler The message handler.
39 */
40 on(event: 'message', handler: MessageHandler<this>): void
41 /**
42 * Register a listener to the error event.
43 *
44 * @param event `'error'`.
45 * @param handler The error handler.
46 */
47 on(event: 'error', handler: ErrorHandler<this>): void
48 /**
49 * Register a listener to the online event.
50 *
51 * @param event `'online'`.
52 * @param handler The online handler.
53 */
54 on(event: 'online', handler: OnlineHandler<this>): void
55 /**
56 * Register a listener to the exit event.
57 *
58 * @param event `'exit'`.
59 * @param handler The exit handler.
60 */
61 on(event: 'exit', handler: ExitHandler<this>): void
62 /**
63 * Register a listener to the exit event that will only performed once.
64 *
65 * @param event `'exit'`.
66 * @param handler The exit handler.
67 */
68 once(event: 'exit', handler: ExitHandler<this>): void
69}