perf: remove unneeded class indirection for dynamic pool in worker
[poolifier.git] / src / pools / pool-internal.ts
1 import type { IPool } from './pool'
2 import type { IPoolWorker } from './pool-worker'
3
4 /**
5 * Internal pool types.
6 */
7 export enum PoolType {
8 FIXED = 'fixed',
9 DYNAMIC = 'dynamic'
10 }
11
12 /**
13 * Internal tasks usage statistics.
14 */
15 export interface TasksUsage {
16 run: number
17 running: number
18 runTime: number
19 avgRunTime: number
20 error: number
21 }
22
23 /**
24 * Internal worker type.
25 *
26 * @typeParam Worker - Type of worker which manages this pool.
27 */
28 export interface WorkerType<Worker extends IPoolWorker> {
29 worker: Worker
30 tasksUsage: TasksUsage
31 }
32
33 /**
34 * Internal contract definition for a poolifier pool.
35 *
36 * @typeParam Worker - Type of worker which manages this pool.
37 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
38 * @typeParam Response - Type of response of execution. This can only be serializable data.
39 */
40 export interface IPoolInternal<
41 Worker extends IPoolWorker,
42 Data = unknown,
43 Response = unknown
44 > extends IPool<Data, Response> {
45 /**
46 * Pool worker type items array.
47 */
48 readonly workers: Array<WorkerType<Worker>>
49
50 /**
51 * Pool type.
52 *
53 * If it is `'dynamic'`, it provides the `max` property.
54 */
55 readonly type: PoolType
56
57 /**
58 * Whether the pool is full or not.
59 *
60 * The pool filling boolean status.
61 */
62 readonly full: boolean
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 * Finds a free worker key based on the number of tasks the worker has applied.
73 *
74 * If a worker is found with `0` running tasks, it is detected as free and its key is returned.
75 *
76 * If no free worker is found, `false` is returned.
77 *
78 * @returns A worker key if there is one, `-1` otherwise.
79 */
80 findFreeWorkerKey: () => number
81 }