1 import EventEmitter from
'node:events'
10 WorkerChoiceStrategyOptions
11 } from
'./selection-strategies/selection-strategies-types'
14 * Pool events emitter.
16 export class PoolEmitter
extends EventEmitter
{}
19 * Enumeration of pool events.
21 export const PoolEvents
= Object.freeze({
29 export type PoolEvent
= keyof
typeof PoolEvents
32 * Options for a poolifier pool.
34 export interface PoolOptions
<Worker
> {
36 * A function that will listen for message event on each worker.
38 messageHandler
?: MessageHandler
<Worker
>
40 * A function that will listen for error event on each worker.
42 errorHandler
?: ErrorHandler
<Worker
>
44 * A function that will listen for online event on each worker.
46 onlineHandler
?: OnlineHandler
<Worker
>
48 * A function that will listen for exit event on each worker.
50 exitHandler
?: ExitHandler
<Worker
>
52 * The worker choice strategy to use in this pool.
54 workerChoiceStrategy
?: WorkerChoiceStrategy
56 * The worker choice strategy options.
58 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
60 * Pool events emission.
64 enableEvents
?: boolean
66 * Pool worker tasks queue.
71 enableTasksQueue
?: boolean
75 * Contract definition for a poolifier pool.
77 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
78 * @typeParam Response - Type of response of execution. This can only be serializable data.
80 export interface IPool
<Data
= unknown
, Response
= unknown
> {
82 * Emitter on which events can be listened to.
84 * Events that can currently be listened to:
86 * - `'full'`: Emitted when the pool is dynamic and full.
87 * - `'busy'`: Emitted when the pool is busy.
89 readonly emitter
?: PoolEmitter
91 * Performs the task specified in the constructor with the data parameter.
93 * @param data - The input for the specified task. This can only be serializable data.
94 * @returns Promise that will be resolved when the task is successfully completed.
96 execute
: (data
: Data
) => Promise
<Response
>
98 * Shutdowns every current worker in this pool.
100 destroy
: () => Promise
<void>
102 * Sets the worker choice strategy in this pool.
104 * @param workerChoiceStrategy - The worker choice strategy.
106 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void