911e761df157a07f54ac1300328c82548cc6d9d6
[poolifier.git] / src / pools / pool-internal.ts
1 import EventEmitter from 'events'
2 import type { IWorker } from './abstract-pool'
3 import type { IPool } from './pool'
4
5 /**
6 * Internal poolifier pool emitter.
7 */
8 export class PoolEmitter extends EventEmitter {}
9
10 /**
11 * Internal contract definition for a poolifier pool.
12 *
13 * @template Worker Type of worker which manages this pool.
14 * @template Data Type of data sent to the worker.
15 * @template Response Type of response of execution.
16 */
17 export interface IPoolInternal<
18 Worker extends IWorker,
19 Data = unknown,
20 Response = unknown
21 > extends IPool<Data, Response> {
22 /**
23 * List of currently available workers.
24 */
25 readonly workers: Worker[]
26
27 /**
28 * The tasks map.
29 *
30 * - `key`: The `Worker`
31 * - `value`: Number of tasks currently in progress on the worker.
32 */
33 readonly tasks: Map<Worker, number>
34
35 /**
36 * Emitter on which events can be listened to.
37 *
38 * Events that can currently be listened to:
39 *
40 * - `'busy'`
41 */
42 readonly emitter: PoolEmitter
43
44 /**
45 * Whether the pool is dynamic or not.
46 *
47 * If it is dynamic, it provides the `max` property.
48 */
49 readonly dynamic: boolean
50
51 /**
52 * Maximum number of workers that can be created by this pool.
53 */
54 readonly max?: number
55 }