1 import EventEmitter from
'events'
2 import type { IPool
} from
'./pool'
3 import type { IPoolWorker
} from
'./pool-worker'
14 * Tasks usage statistics.
16 export interface TasksUsage
{
24 * Internal poolifier pool emitter.
26 export class PoolEmitter
extends EventEmitter
{}
29 * Internal contract definition for a poolifier pool.
31 * @template Worker Type of worker which manages this pool.
32 * @template Data Type of data sent to the worker.
33 * @template Response Type of response of execution.
35 export interface IPoolInternal
<
36 Worker
extends IPoolWorker
,
39 > extends IPool
<Data
, Response
> {
41 * List of currently available workers.
43 readonly workers
: Worker
[]
46 * The workers tasks usage map.
49 * `value`: Worker tasks usage statistics.
51 readonly workersTasksUsage
: Map
<Worker
, TasksUsage
>
54 * Emitter on which events can be listened to.
56 * Events that can currently be listened to:
60 readonly emitter
?: PoolEmitter
65 * If it is `'dynamic'`, it provides the `max` property.
67 readonly type: PoolType
70 * Maximum number of workers that can be created by this pool.
75 * Whether the pool is busy or not.
77 * The pool busyness boolean status.
79 readonly busy
: boolean
82 * Number of tasks currently concurrently running.
84 readonly numberOfRunningTasks
: number
87 * Finds a free worker based on the number of tasks the worker has applied.
89 * If a worker is found with `0` running tasks, it is detected as free and returned.
91 * If no free worker is found, `false` is returned.
93 * @returns A free worker if there is one, otherwise `false`.
95 findFreeWorker(): Worker
| false
100 * @param worker The worker.
101 * @returns The worker index.
103 getWorkerIndex(worker
: Worker
): number
106 * Gets worker running tasks.
108 * @param worker The worker.
109 * @returns The number of tasks currently running on the worker.
111 getWorkerRunningTasks(worker
: Worker
): number | undefined
114 * Gets worker average tasks runtime.
116 * @param worker The worker.
117 * @returns The average tasks runtime on the worker.
119 getWorkerAverageTasksRunTime(worker
: Worker
): number | undefined