1 import EventEmitter from
'node:events'
11 WorkerChoiceStrategyOptions
12 } from
'./selection-strategies/selection-strategies-types'
15 * Pool events emitter.
17 export class PoolEmitter
extends EventEmitter
{}
20 * Enumeration of pool events.
22 export const PoolEvents
= Object.freeze({
30 export type PoolEvent
= keyof
typeof PoolEvents
33 * Options for a poolifier pool.
35 export interface PoolOptions
<Worker
extends IWorker
> {
37 * A function that will listen for message event on each worker.
39 messageHandler
?: MessageHandler
<Worker
>
41 * A function that will listen for error event on each worker.
43 errorHandler
?: ErrorHandler
<Worker
>
45 * A function that will listen for online event on each worker.
47 onlineHandler
?: OnlineHandler
<Worker
>
49 * A function that will listen for exit event on each worker.
51 exitHandler
?: ExitHandler
<Worker
>
53 * The worker choice strategy to use in this pool.
55 workerChoiceStrategy
?: WorkerChoiceStrategy
57 * The worker choice strategy options.
59 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
61 * Pool events emission.
65 enableEvents
?: boolean
67 * Pool worker tasks queue.
72 enableTasksQueue
?: boolean
76 * Contract definition for a poolifier pool.
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.
81 export interface IPool
<Data
= unknown
, Response
= unknown
> {
83 * Emitter on which events can be listened to.
85 * Events that can currently be listened to:
87 * - `'full'`: Emitted when the pool is dynamic and full.
88 * - `'busy'`: Emitted when the pool is busy.
90 readonly emitter
?: PoolEmitter
92 * Performs the task specified in the constructor with the data parameter.
94 * @param data - The input for the specified task. This can only be serializable data.
95 * @returns Promise that will be resolved when the task is successfully completed.
97 execute
: (data
: Data
) => Promise
<Response
>
99 * Shutdowns every current worker in this pool.
101 destroy
: () => Promise
<void>
103 * Sets the worker choice strategy in this pool.
105 * @param workerChoiceStrategy - The worker choice strategy.
107 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void