| 1 | import type { IPoolInternal } from '../pool-internal' |
| 2 | import { PoolType } from '../pool-internal' |
| 3 | import type { IPoolWorker } from '../pool-worker' |
| 4 | import type { |
| 5 | IWorkerChoiceStrategy, |
| 6 | RequiredStatistics |
| 7 | } from './selection-strategies-types' |
| 8 | |
| 9 | /** |
| 10 | * Worker choice strategy abstract base class. |
| 11 | * |
| 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. |
| 15 | */ |
| 16 | export abstract class AbstractWorkerChoiceStrategy< |
| 17 | Worker extends IPoolWorker, |
| 18 | Data, |
| 19 | Response |
| 20 | > implements IWorkerChoiceStrategy { |
| 21 | /** {@inheritDoc} */ |
| 22 | public readonly isDynamicPool: boolean |
| 23 | /** {@inheritDoc} */ |
| 24 | public requiredStatistics: RequiredStatistics = { |
| 25 | runTime: false, |
| 26 | avgRunTime: false |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Constructs a worker choice strategy attached to the pool. |
| 31 | * |
| 32 | * @param pool - The pool instance. |
| 33 | */ |
| 34 | public constructor ( |
| 35 | protected readonly pool: IPoolInternal<Worker, Data, Response> |
| 36 | ) { |
| 37 | this.isDynamicPool = this.pool.type === PoolType.DYNAMIC |
| 38 | this.choose.bind(this) |
| 39 | } |
| 40 | |
| 41 | /** {@inheritDoc} */ |
| 42 | public abstract reset (): boolean |
| 43 | |
| 44 | /** {@inheritDoc} */ |
| 45 | public abstract choose (): number |
| 46 | |
| 47 | /** {@inheritDoc} */ |
| 48 | public abstract remove (workerKey: number): boolean |
| 49 | } |