X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=42e6735e658ac76a9a338e389404f784a6265560;hb=78cea37e264d5ca527bc42eb056f3b9579a2b2c4;hp=3de735321e9a116c0f189a601dda4ae1be1eefbb;hpb=bdaf31cd0e637aa466c78d54a49f157899a2cb3f;p=poolifier.git diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 3de73532..42e6735e 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,13 +1,13 @@ -import type { AbstractPoolWorker } from '../abstract-pool-worker' import type { IPoolInternal } from '../pool-internal' import { PoolType } from '../pool-internal' +import type { IPoolWorker } from '../pool-worker' import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy' import type { IWorkerChoiceStrategy, WorkerChoiceStrategy } from './selection-strategies-types' import { WorkerChoiceStrategies } from './selection-strategies-types' -import { SelectionStrategiesUtils } from './selection-strategies-utils' +import { getWorkerChoiceStrategy } from './selection-strategies-utils' /** * The worker choice strategy context. @@ -17,11 +17,10 @@ import { SelectionStrategiesUtils } from './selection-strategies-utils' * @template Response Type of response of execution. This can only be serializable data. */ export class WorkerChoiceStrategyContext< - Worker extends AbstractPoolWorker, + Worker extends IPoolWorker, Data, Response > { - // Will be set by setter in constructor private workerChoiceStrategy!: IWorkerChoiceStrategy /** @@ -33,14 +32,14 @@ export class WorkerChoiceStrategyContext< */ public constructor ( private readonly pool: IPoolInternal, - private createDynamicallyWorkerCallback: () => Worker, + private readonly createDynamicallyWorkerCallback: () => Worker, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.setWorkerChoiceStrategy(workerChoiceStrategy) } /** - * Get the worker choice strategy instance specific to the pool type. + * Gets the worker choice strategy instance specific to the pool type. * * @param workerChoiceStrategy The worker choice strategy. * @returns The worker choice strategy instance for the pool type. @@ -55,27 +54,33 @@ export class WorkerChoiceStrategyContext< workerChoiceStrategy ) } - return SelectionStrategiesUtils.getWorkerChoiceStrategy( - this.pool, - workerChoiceStrategy - ) + return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy) } /** - * Set the worker choice strategy to use in the context. + * Gets the worker choice strategy used in the context. + * + * @returns The worker choice strategy. + */ + public getWorkerChoiceStrategy (): IWorkerChoiceStrategy { + return this.workerChoiceStrategy + } + + /** + * Sets the worker choice strategy to use in the context. * * @param workerChoiceStrategy The worker choice strategy to set. */ public setWorkerChoiceStrategy ( workerChoiceStrategy: WorkerChoiceStrategy ): void { - this.workerChoiceStrategy = this.getPoolWorkerChoiceStrategy( - workerChoiceStrategy - ) + this.workerChoiceStrategy?.reset() + this.workerChoiceStrategy = + this.getPoolWorkerChoiceStrategy(workerChoiceStrategy) } /** - * Choose a worker with the underlying selection strategy. + * Chooses a worker with the underlying selection strategy. * * @returns The chosen one. */