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