docs: updates changelog entries
[poolifier.git] / src / pools / pool.ts
CommitLineData
fc3e6586 1import EventEmitter from 'node:events'
bdaf31cd
JB
2import type {
3 ErrorHandler,
4 ExitHandler,
5 MessageHandler,
6 OnlineHandler
7} from './pool-worker'
8import type { WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types'
9
b4904890
JB
10/**
11 * Pool events emitter.
12 */
13export class PoolEmitter extends EventEmitter {}
14
bdaf31cd
JB
15/**
16 * Options for a poolifier pool.
17 */
18export interface PoolOptions<Worker> {
19 /**
20 * A function that will listen for message event on each worker.
21 */
22 messageHandler?: MessageHandler<Worker>
23 /**
24 * A function that will listen for error event on each worker.
25 */
26 errorHandler?: ErrorHandler<Worker>
27 /**
28 * A function that will listen for online event on each worker.
29 */
30 onlineHandler?: OnlineHandler<Worker>
31 /**
32 * A function that will listen for exit event on each worker.
33 */
34 exitHandler?: ExitHandler<Worker>
35 /**
46e857ca 36 * The worker choice strategy to use in this pool.
bdaf31cd
JB
37 */
38 workerChoiceStrategy?: WorkerChoiceStrategy
39 /**
40 * Pool events emission.
41 *
38e795c1 42 * @defaultValue true
bdaf31cd
JB
43 */
44 enableEvents?: boolean
45}
a35560ba 46
729c563d
S
47/**
48 * Contract definition for a poolifier pool.
49 *
38e795c1
JB
50 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
51 * @typeParam Response - Type of response of execution. This can only be serializable data.
729c563d 52 */
d3c8a1a8 53export interface IPool<Data = unknown, Response = unknown> {
b4904890
JB
54 /**
55 * Emitter on which events can be listened to.
56 *
57 * Events that can currently be listened to:
58 *
164d950a
JB
59 * - `'full'`: Emitted when the pool is dynamic and full.
60 * - `'busy'`: Emitted when the pool is busy.
b4904890
JB
61 */
62 readonly emitter?: PoolEmitter
729c563d 63 /**
bdede008 64 * Performs the task specified in the constructor with the data parameter.
729c563d 65 *
38e795c1 66 * @param data - The input for the specified task. This can only be serializable data.
729c563d
S
67 * @returns Promise that will be resolved when the task is successfully completed.
68 */
78cea37e 69 execute: (data: Data) => Promise<Response>
280c2a77 70 /**
675bb809 71 * Shutdowns every current worker in this pool.
280c2a77 72 */
78cea37e 73 destroy: () => Promise<void>
a35560ba 74 /**
bdede008 75 * Sets the worker choice strategy in this pool.
a35560ba 76 *
38e795c1 77 * @param workerChoiceStrategy - The worker choice strategy.
a35560ba 78 */
78cea37e 79 setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void
c97c7edb 80}