fix: fix default worker data value
[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
a35560ba
S
22/**
23 * Internal contract definition for a poolifier pool.
24 *
38e795c1
JB
25 * @typeParam Worker - Type of worker which manages this pool.
26 * @typeParam Data - Type of data sent to the worker.
27 * @typeParam Response - Type of response of execution.
a35560ba
S
28 */
29export interface IPoolInternal<
ea7a90d3 30 Worker extends IPoolWorker,
a35560ba
S
31 Data = unknown,
32 Response = unknown
33> extends IPool<Data, Response> {
34 /**
35 * List of currently available workers.
36 */
37 readonly workers: Worker[]
38
ea7a90d3
JB
39 /**
40 * The workers tasks usage map.
41 *
42 * `key`: The `Worker`
43 * `value`: Worker tasks usage statistics.
44 */
45 readonly workersTasksUsage: Map<Worker, TasksUsage>
46
a35560ba 47 /**
7c0ba920 48 * Pool type.
a35560ba 49 *
7c0ba920 50 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 51 */
7c0ba920 52 readonly type: PoolType
ff5e76e1 53
7c0ba920
JB
54 /**
55 * Whether the pool is busy or not.
56 *
57 * The pool busyness boolean status.
58 */
59 readonly busy: boolean
60
61 /**
62 * Number of tasks currently concurrently running.
63 */
64 readonly numberOfRunningTasks: number
65
66 /**
8accb8d5 67 * Finds a free worker based on the number of tasks the worker has applied.
7c0ba920 68 *
bdaf31cd 69 * If a worker is found with `0` running tasks, it is detected as free and returned.
7c0ba920 70 *
bdaf31cd 71 * If no free worker is found, `false` is returned.
7c0ba920 72 *
bdaf31cd 73 * @returns A free worker if there is one, otherwise `false`.
7c0ba920 74 */
78cea37e 75 findFreeWorker: () => Worker | false
bdaf31cd
JB
76
77 /**
8accb8d5 78 * Gets worker index.
bdaf31cd 79 *
38e795c1 80 * @param worker - The worker.
bdaf31cd
JB
81 * @returns The worker index.
82 */
78cea37e 83 getWorkerIndex: (worker: Worker) => number
bdaf31cd
JB
84
85 /**
8accb8d5 86 * Gets worker running tasks.
bdaf31cd 87 *
38e795c1 88 * @param worker - The worker.
bdaf31cd
JB
89 * @returns The number of tasks currently running on the worker.
90 */
78cea37e 91 getWorkerRunningTasks: (worker: Worker) => number | undefined
bf9549ae
JB
92
93 /**
8accb8d5 94 * Gets worker average tasks runtime.
bf9549ae 95 *
38e795c1 96 * @param worker - The worker.
23135a89 97 * @returns The average tasks runtime on the worker.
bf9549ae 98 */
78cea37e 99 getWorkerAverageTasksRunTime: (worker: Worker) => number | undefined
a35560ba 100}