60aceb786549ac0daabee8253f6c1fad890918fe
1 import EventEmitter from
'node:events'
8 import type { WorkerChoiceStrategy
} from
'./selection-strategies/selection-strategies-types'
11 * Pool events emitter.
13 export class PoolEmitter
extends EventEmitter
{}
16 * Enumeration of pool events.
18 export const PoolEvents
= Object.freeze({
26 export type PoolEvent
= keyof
typeof PoolEvents
29 * Options for a poolifier pool.
31 export interface PoolOptions
<Worker
> {
33 * A function that will listen for message event on each worker.
35 messageHandler
?: MessageHandler
<Worker
>
37 * A function that will listen for error event on each worker.
39 errorHandler
?: ErrorHandler
<Worker
>
41 * A function that will listen for online event on each worker.
43 onlineHandler
?: OnlineHandler
<Worker
>
45 * A function that will listen for exit event on each worker.
47 exitHandler
?: ExitHandler
<Worker
>
49 * The worker choice strategy to use in this pool.
51 workerChoiceStrategy
?: WorkerChoiceStrategy
53 * Pool events emission.
57 enableEvents
?: boolean
61 * Contract definition for a poolifier pool.
63 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
64 * @typeParam Response - Type of response of execution. This can only be serializable data.
66 export interface IPool
<Data
= unknown
, Response
= unknown
> {
68 * Emitter on which events can be listened to.
70 * Events that can currently be listened to:
72 * - `'full'`: Emitted when the pool is dynamic and full.
73 * - `'busy'`: Emitted when the pool is busy.
75 readonly emitter
?: PoolEmitter
77 * Performs the task specified in the constructor with the data parameter.
79 * @param data - The input for the specified task. This can only be serializable data.
80 * @returns Promise that will be resolved when the task is successfully completed.
82 execute
: (data
: Data
) => Promise
<Response
>
84 * Shutdowns every current worker in this pool.
86 destroy
: () => Promise
<void>
88 * Sets the worker choice strategy in this pool.
90 * @param workerChoiceStrategy - The worker choice strategy.
92 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void