1 import EventEmitter from
'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:
61 readonly emitter
?: PoolEmitter
63 * Performs the task specified in the constructor with the data parameter.
65 * @param data - The input for the specified task. This can only be serializable data.
66 * @returns Promise that will be resolved when the task is successfully completed.
68 execute
: (data
: Data
) => Promise
<Response
>
70 * Shutdowns every current worker in this pool.
72 destroy
: () => Promise
<void>
74 * Sets the worker choice strategy in this pool.
76 * @param workerChoiceStrategy - The worker choice strategy.
78 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void