refactor: enforce more worker type safety
[poolifier.git] / src / pools / pool.ts
CommitLineData
fc3e6586 1import EventEmitter from 'node:events'
bdaf31cd
JB
2import type {
3 ErrorHandler,
4 ExitHandler,
50e66724 5 IWorker,
bdaf31cd
JB
6 MessageHandler,
7 OnlineHandler
f06e48d8 8} from './worker'
da309861
JB
9import type {
10 WorkerChoiceStrategy,
11 WorkerChoiceStrategyOptions
12} from './selection-strategies/selection-strategies-types'
bdaf31cd 13
b4904890
JB
14/**
15 * Pool events emitter.
16 */
17export class PoolEmitter extends EventEmitter {}
18
aee46736
JB
19/**
20 * Enumeration of pool events.
21 */
22export const PoolEvents = Object.freeze({
23 full: 'full',
24 busy: 'busy'
25} as const)
26
27/**
28 * Pool event.
29 */
30export type PoolEvent = keyof typeof PoolEvents
31
bdaf31cd
JB
32/**
33 * Options for a poolifier pool.
34 */
50e66724 35export interface PoolOptions<Worker extends IWorker> {
bdaf31cd
JB
36 /**
37 * A function that will listen for message event on each worker.
38 */
39 messageHandler?: MessageHandler<Worker>
40 /**
41 * A function that will listen for error event on each worker.
42 */
43 errorHandler?: ErrorHandler<Worker>
44 /**
45 * A function that will listen for online event on each worker.
46 */
47 onlineHandler?: OnlineHandler<Worker>
48 /**
49 * A function that will listen for exit event on each worker.
50 */
51 exitHandler?: ExitHandler<Worker>
52 /**
46e857ca 53 * The worker choice strategy to use in this pool.
bdaf31cd
JB
54 */
55 workerChoiceStrategy?: WorkerChoiceStrategy
da309861
JB
56 /**
57 * The worker choice strategy options.
58 */
59 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
bdaf31cd
JB
60 /**
61 * Pool events emission.
62 *
38e795c1 63 * @defaultValue true
bdaf31cd
JB
64 */
65 enableEvents?: boolean
ff733df7
JB
66 /**
67 * Pool worker tasks queue.
68 *
69 * @experimental
70 * @defaultValue false
71 */
72 enableTasksQueue?: boolean
bdaf31cd 73}
a35560ba 74
729c563d
S
75/**
76 * Contract definition for a poolifier pool.
77 *
38e795c1
JB
78 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
79 * @typeParam Response - Type of response of execution. This can only be serializable data.
729c563d 80 */
d3c8a1a8 81export interface IPool<Data = unknown, Response = unknown> {
b4904890
JB
82 /**
83 * Emitter on which events can be listened to.
84 *
85 * Events that can currently be listened to:
86 *
164d950a
JB
87 * - `'full'`: Emitted when the pool is dynamic and full.
88 * - `'busy'`: Emitted when the pool is busy.
b4904890
JB
89 */
90 readonly emitter?: PoolEmitter
729c563d 91 /**
bdede008 92 * Performs the task specified in the constructor with the data parameter.
729c563d 93 *
38e795c1 94 * @param data - The input for the specified task. This can only be serializable data.
729c563d
S
95 * @returns Promise that will be resolved when the task is successfully completed.
96 */
78cea37e 97 execute: (data: Data) => Promise<Response>
280c2a77 98 /**
675bb809 99 * Shutdowns every current worker in this pool.
280c2a77 100 */
78cea37e 101 destroy: () => Promise<void>
a35560ba 102 /**
bdede008 103 * Sets the worker choice strategy in this pool.
a35560ba 104 *
38e795c1 105 * @param workerChoiceStrategy - The worker choice strategy.
a35560ba 106 */
78cea37e 107 setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void
c97c7edb 108}