perf: bind to this some methods in the tasks execution code path
[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
2740a743 20 error: number
bf9549ae
JB
21}
22
ffcbbad8
JB
23/**
24 * Internal worker type.
25 *
26 * @typeParam Worker - Type of worker which manages this pool.
27 */
28export interface WorkerType<Worker extends IPoolWorker> {
29 worker: Worker
30 tasksUsage: TasksUsage
31}
32
a35560ba
S
33/**
34 * Internal contract definition for a poolifier pool.
35 *
38e795c1
JB
36 * @typeParam Worker - Type of worker which manages this pool.
37 * @typeParam Data - Type of data sent to the worker.
38 * @typeParam Response - Type of response of execution.
a35560ba
S
39 */
40export interface IPoolInternal<
ea7a90d3 41 Worker extends IPoolWorker,
a35560ba
S
42 Data = unknown,
43 Response = unknown
44> extends IPool<Data, Response> {
45 /**
e65c6cd9 46 * Pool workers item array.
a35560ba 47 */
e65c6cd9 48 readonly workers: Array<WorkerType<Worker>>
ea7a90d3 49
a35560ba 50 /**
7c0ba920 51 * Pool type.
a35560ba 52 *
7c0ba920 53 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 54 */
7c0ba920 55 readonly type: PoolType
ff5e76e1 56
7c0ba920
JB
57 /**
58 * Whether the pool is busy or not.
59 *
60 * The pool busyness boolean status.
61 */
62 readonly busy: boolean
63
64 /**
65 * Number of tasks currently concurrently running.
66 */
67 readonly numberOfRunningTasks: number
68
69 /**
c923ce56 70 * Finds a free worker key based on the number of tasks the worker has applied.
7c0ba920 71 *
c923ce56 72 * If a worker is found with `0` running tasks, it is detected as free and its key is returned.
7c0ba920 73 *
bdaf31cd 74 * If no free worker is found, `false` is returned.
7c0ba920 75 *
bf90656c 76 * @returns A worker key if there is one, `-1` otherwise.
7c0ba920 77 */
bf90656c 78 findFreeWorkerKey: () => number
a35560ba 79}