fix: fix default worker data value
[poolifier.git] / src / pools / pool.ts
CommitLineData
b4904890 1import EventEmitter from 'events'
bdaf31cd
JB
2import type {
3 ErrorHandler,
4 ExitHandler,
5 MessageHandler,
6 OnlineHandler
7} from './pool-worker'
8import type { WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types'
9
b4904890
JB
10/**
11 * Pool events emitter.
12 */
13export class PoolEmitter extends EventEmitter {}
14
bdaf31cd
JB
15/**
16 * Options for a poolifier pool.
17 */
18export interface PoolOptions<Worker> {
19 /**
20 * A function that will listen for message event on each worker.
21 */
22 messageHandler?: MessageHandler<Worker>
23 /**
24 * A function that will listen for error event on each worker.
25 */
26 errorHandler?: ErrorHandler<Worker>
27 /**
28 * A function that will listen for online event on each worker.
29 */
30 onlineHandler?: OnlineHandler<Worker>
31 /**
32 * A function that will listen for exit event on each worker.
33 */
34 exitHandler?: ExitHandler<Worker>
35 /**
46e857ca 36 * The worker choice strategy to use in this pool.
bdaf31cd
JB
37 */
38 workerChoiceStrategy?: WorkerChoiceStrategy
39 /**
40 * Pool events emission.
41 *
38e795c1 42 * @defaultValue true
bdaf31cd
JB
43 */
44 enableEvents?: boolean
45}
a35560ba 46
729c563d
S
47/**
48 * Contract definition for a poolifier pool.
49 *
38e795c1
JB
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.
729c563d 52 */
d3c8a1a8 53export interface IPool<Data = unknown, Response = unknown> {
b4904890
JB
54 /**
55 * Emitter on which events can be listened to.
56 *
57 * Events that can currently be listened to:
58 *
59 * - `'busy'`
60 */
61 readonly emitter?: PoolEmitter
729c563d 62 /**
bdede008 63 * Performs the task specified in the constructor with the data parameter.
729c563d 64 *
38e795c1 65 * @param data - The input for the specified task. This can only be serializable data.
729c563d
S
66 * @returns Promise that will be resolved when the task is successfully completed.
67 */
78cea37e 68 execute: (data: Data) => Promise<Response>
280c2a77 69 /**
675bb809 70 * Shutdowns every current worker in this pool.
280c2a77 71 */
78cea37e 72 destroy: () => Promise<void>
a35560ba 73 /**
bdede008 74 * Sets the worker choice strategy in this pool.
a35560ba 75 *
38e795c1 76 * @param workerChoiceStrategy - The worker choice strategy.
a35560ba 77 */
78cea37e 78 setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void
c97c7edb 79}