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