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 * Options for a poolifier pool.
18 export interface PoolOptions
<Worker
> {
20 * A function that will listen for message event on each worker.
22 messageHandler
?: MessageHandler
<Worker
>
24 * A function that will listen for error event on each worker.
26 errorHandler
?: ErrorHandler
<Worker
>
28 * A function that will listen for online event on each worker.
30 onlineHandler
?: OnlineHandler
<Worker
>
32 * A function that will listen for exit event on each worker.
34 exitHandler
?: ExitHandler
<Worker
>
36 * The worker choice strategy to use in this pool.
38 workerChoiceStrategy
?: WorkerChoiceStrategy
40 * Pool events emission.
44 enableEvents
?: boolean
48 * Contract definition for a poolifier pool.
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.
53 export interface IPool
<Data
= unknown
, Response
= unknown
> {
55 * Emitter on which events can be listened to.
57 * Events that can currently be listened to:
59 * - `'full'`: Emitted when the pool is dynamic and full.
60 * - `'busy'`: Emitted when the pool is busy.
62 readonly emitter
?: PoolEmitter
64 * Performs the task specified in the constructor with the data parameter.
66 * @param data - The input for the specified task. This can only be serializable data.
67 * @returns Promise that will be resolved when the task is successfully completed.
69 execute
: (data
: Data
) => Promise
<Response
>
71 * Shutdowns every current worker in this pool.
73 destroy
: () => Promise
<void>
75 * Sets the worker choice strategy in this pool.
77 * @param workerChoiceStrategy - The worker choice strategy.
79 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void