| 1 | import EventEmitter from 'events' |
| 2 | import type { IWorker } from './abstract-pool' |
| 3 | import type { IPool } from './pool' |
| 4 | |
| 5 | /** |
| 6 | * Pool types. |
| 7 | */ |
| 8 | export enum PoolType { |
| 9 | FIXED = 'fixed', |
| 10 | DYNAMIC = 'dynamic' |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Internal poolifier pool emitter. |
| 15 | */ |
| 16 | export class PoolEmitter extends EventEmitter {} |
| 17 | |
| 18 | /** |
| 19 | * Internal contract definition for a poolifier pool. |
| 20 | * |
| 21 | * @template Worker Type of worker which manages this pool. |
| 22 | * @template Data Type of data sent to the worker. |
| 23 | * @template Response Type of response of execution. |
| 24 | */ |
| 25 | export interface IPoolInternal< |
| 26 | Worker extends IWorker, |
| 27 | Data = unknown, |
| 28 | Response = unknown |
| 29 | > extends IPool<Data, Response> { |
| 30 | /** |
| 31 | * List of currently available workers. |
| 32 | */ |
| 33 | readonly workers: Worker[] |
| 34 | |
| 35 | /** |
| 36 | * The tasks map. |
| 37 | * |
| 38 | * - `key`: The `Worker` |
| 39 | * - `value`: Number of tasks currently in progress on the worker. |
| 40 | */ |
| 41 | readonly tasks: Map<Worker, number> |
| 42 | |
| 43 | /** |
| 44 | * Emitter on which events can be listened to. |
| 45 | * |
| 46 | * Events that can currently be listened to: |
| 47 | * |
| 48 | * - `'busy'` |
| 49 | */ |
| 50 | readonly emitter?: PoolEmitter |
| 51 | |
| 52 | /** |
| 53 | * Pool type. |
| 54 | * |
| 55 | * If it is `'dynamic'`, it provides the `max` property. |
| 56 | */ |
| 57 | readonly type: PoolType |
| 58 | |
| 59 | /** |
| 60 | * Maximum number of workers that can be created by this pool. |
| 61 | */ |
| 62 | readonly max?: number |
| 63 | |
| 64 | /** |
| 65 | * Whether the pool is busy or not. |
| 66 | * |
| 67 | * The pool busyness boolean status. |
| 68 | */ |
| 69 | readonly busy: boolean |
| 70 | |
| 71 | /** |
| 72 | * Number of tasks currently concurrently running. |
| 73 | */ |
| 74 | readonly numberOfRunningTasks: number |
| 75 | |
| 76 | /** |
| 77 | * Find a tasks map entry with a free worker based on the number of tasks the worker has applied. |
| 78 | * |
| 79 | * If an entry is found with a worker that has `0` tasks, it is detected as free. |
| 80 | * |
| 81 | * If no tasks map entry with a free worker was found, `false` will be returned. |
| 82 | * |
| 83 | * @returns A tasks map entry with a free worker if there was one, otherwise `false`. |
| 84 | */ |
| 85 | findFreeTasksMapEntry(): [Worker, number] | false |
| 86 | } |