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 * Worker tasks queue options.
35 export interface TasksQueueOptions
{
37 * Maximum number of tasks that can be executed concurrently on a worker.
45 * Options for a poolifier pool.
47 export interface PoolOptions
<Worker
extends IWorker
> {
49 * A function that will listen for message event on each worker.
51 messageHandler
?: MessageHandler
<Worker
>
53 * A function that will listen for error event on each worker.
55 errorHandler
?: ErrorHandler
<Worker
>
57 * A function that will listen for online event on each worker.
59 onlineHandler
?: OnlineHandler
<Worker
>
61 * A function that will listen for exit event on each worker.
63 exitHandler
?: ExitHandler
<Worker
>
65 * The worker choice strategy to use in this pool.
67 workerChoiceStrategy
?: WorkerChoiceStrategy
69 * The worker choice strategy options.
71 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
73 * Pool events emission.
77 enableEvents
?: boolean
79 * Pool worker tasks queue.
84 enableTasksQueue
?: boolean
86 * Pool worker tasks queue options.
89 * @defaultValue \{ concurrency: 1 \}
91 tasksQueueOptions
?: TasksQueueOptions
95 * Contract definition for a poolifier pool.
97 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
98 * @typeParam Response - Type of response of execution. This can only be serializable data.
100 export interface IPool
<Data
= unknown
, Response
= unknown
> {
102 * Emitter on which events can be listened to.
104 * Events that can currently be listened to:
106 * - `'full'`: Emitted when the pool is dynamic and full.
107 * - `'busy'`: Emitted when the pool is busy.
109 readonly emitter
?: PoolEmitter
111 * Performs the task specified in the constructor with the data parameter.
113 * @param data - The input for the specified task. This can only be serializable data.
114 * @returns Promise that will be resolved when the task is successfully completed.
116 execute
: (data
: Data
) => Promise
<Response
>
118 * Shutdowns every current worker in this pool.
120 destroy
: () => Promise
<void>
122 * Sets the worker choice strategy in this pool.
124 * @param workerChoiceStrategy - The worker choice strategy.
126 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void