refactor: switch Date.now() -> performance.now() where appropriate
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
bdaf31cd
JB
1import type { IPoolInternal } from '../pool-internal'
2import { PoolType } from '../pool-internal'
ea7a90d3 3import type { IPoolWorker } from '../pool-worker'
10fcfaf4
JB
4import type {
5 IWorkerChoiceStrategy,
6 RequiredStatistics
7} from './selection-strategies-types'
bdaf31cd
JB
8
9/**
9cd39dd4 10 * Worker choice strategy abstract base class.
bdaf31cd 11 *
38e795c1
JB
12 * @typeParam Worker - Type of worker which manages the strategy.
13 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
14 * @typeParam Response - Type of response of execution. This can only be serializable data.
bdaf31cd
JB
15 */
16export abstract class AbstractWorkerChoiceStrategy<
ea7a90d3 17 Worker extends IPoolWorker,
b2b1d84e
JB
18 Data = unknown,
19 Response = unknown
17393ac8 20> implements IWorkerChoiceStrategy {
afc003b2 21 /** @inheritDoc */
8b4d4500 22 protected readonly isDynamicPool: boolean
afc003b2 23 /** @inheritDoc */
10fcfaf4 24 public requiredStatistics: RequiredStatistics = {
c6bd2650 25 runTime: false,
78099a15
JB
26 avgRunTime: false,
27 medRunTime: false
10fcfaf4 28 }
bdaf31cd
JB
29
30 /**
6533c3e6 31 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 32 *
38e795c1 33 * @param pool - The pool instance.
bdaf31cd
JB
34 */
35 public constructor (
8b4d4500 36 protected readonly pool: IPoolInternal<Worker, Data, Response>
b8f3418c
JB
37 ) {
38 this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
1086026a 39 this.choose.bind(this)
b8f3418c 40 }
bdaf31cd 41
afc003b2 42 /** @inheritDoc */
a6f7f1b4 43 public abstract reset (): boolean
ea7a90d3 44
afc003b2 45 /** @inheritDoc */
c923ce56 46 public abstract choose (): number
97a2abc3 47
afc003b2 48 /** @inheritDoc */
97a2abc3 49 public abstract remove (workerKey: number): boolean
bdaf31cd 50}