refactor: remove unneeded encapsulation around tasks usage handling
[poolifier.git] / src / pools / pool-internal.ts
CommitLineData
a35560ba 1import type { IPool } from './pool'
ea7a90d3 2import type { IPoolWorker } from './pool-worker'
a35560ba 3
7c0ba920 4/**
b4904890 5 * Internal pool types.
7c0ba920
JB
6 */
7export enum PoolType {
8 FIXED = 'fixed',
9 DYNAMIC = 'dynamic'
10}
11
bf9549ae 12/**
b4904890 13 * Internal tasks usage statistics.
bf9549ae
JB
14 */
15export interface TasksUsage {
16 run: number
17 running: number
18 runTime: number
19 avgRunTime: number
20}
21
ffcbbad8
JB
22/**
23 * Internal worker type.
24 *
25 * @typeParam Worker - Type of worker which manages this pool.
26 */
27export interface WorkerType<Worker extends IPoolWorker> {
28 worker: Worker
29 tasksUsage: TasksUsage
30}
31
a35560ba
S
32/**
33 * Internal contract definition for a poolifier pool.
34 *
38e795c1
JB
35 * @typeParam Worker - Type of worker which manages this pool.
36 * @typeParam Data - Type of data sent to the worker.
37 * @typeParam Response - Type of response of execution.
a35560ba
S
38 */
39export interface IPoolInternal<
ea7a90d3 40 Worker extends IPoolWorker,
a35560ba
S
41 Data = unknown,
42 Response = unknown
43> extends IPool<Data, Response> {
44 /**
f4ff1ce2 45 * Pool workers map.
a35560ba 46 */
ffcbbad8 47 readonly workers: Map<number, WorkerType<Worker>>
ea7a90d3 48
a35560ba 49 /**
7c0ba920 50 * Pool type.
a35560ba 51 *
7c0ba920 52 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 53 */
7c0ba920 54 readonly type: PoolType
ff5e76e1 55
7c0ba920
JB
56 /**
57 * Whether the pool is busy or not.
58 *
59 * The pool busyness boolean status.
60 */
61 readonly busy: boolean
62
63 /**
64 * Number of tasks currently concurrently running.
65 */
66 readonly numberOfRunningTasks: number
67
68 /**
8accb8d5 69 * Finds a free worker based on the number of tasks the worker has applied.
7c0ba920 70 *
bdaf31cd 71 * If a worker is found with `0` running tasks, it is detected as free and returned.
7c0ba920 72 *
bdaf31cd 73 * If no free worker is found, `false` is returned.
7c0ba920 74 *
bdaf31cd 75 * @returns A free worker if there is one, otherwise `false`.
7c0ba920 76 */
78cea37e 77 findFreeWorker: () => Worker | false
bdaf31cd 78
bdaf31cd 79 /**
3032893a 80 * Gets worker tasks usage.
bdaf31cd 81 *
38e795c1 82 * @param worker - The worker.
3032893a 83 * @returns The tasks usage on the worker.
bdaf31cd 84 */
3032893a 85 getWorkerTasksUsage: (worker: Worker) => TasksUsage | undefined
a35560ba 86}