X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=41b515b816d00dc991c27d8143efe80ec7c6116a;hb=3300e7bcb155358c2cc1eed6e4ac11581457616f;hp=f6e0feab2e8cb425fec1da9f420deba8b75135e1;hpb=9cd39dd47830f0923cd3ebf53b709bf7fb07e788;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 f6e0feab..41b515b8 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,5 +1,4 @@ import type { IPoolInternal } from '../pool-internal' -import { PoolType } from '../pool-internal' import type { IPoolWorker } from '../pool-worker' import type { IWorkerChoiceStrategy, @@ -21,7 +20,7 @@ export class WorkerChoiceStrategyContext< Data, Response > { - private workerChoiceStrategy!: IWorkerChoiceStrategy + private workerChoiceStrategy: IWorkerChoiceStrategy /** * Worker choice strategy context constructor. @@ -31,12 +30,15 @@ export class WorkerChoiceStrategyContext< * @param workerChoiceStrategy - The worker choice strategy. */ public constructor ( - private readonly pool: IPoolInternal, + pool: IPoolInternal, private readonly createWorkerCallback: () => number, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.execute.bind(this) - this.setWorkerChoiceStrategy(workerChoiceStrategy) + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, + workerChoiceStrategy + ) } /** @@ -54,11 +56,12 @@ export class WorkerChoiceStrategyContext< * @param workerChoiceStrategy - The worker choice strategy to set. */ public setWorkerChoiceStrategy ( + pool: IPoolInternal, workerChoiceStrategy: WorkerChoiceStrategy ): void { this.workerChoiceStrategy?.reset() - this.workerChoiceStrategy = getWorkerChoiceStrategy( - this.pool, + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, workerChoiceStrategy ) } @@ -70,9 +73,9 @@ export class WorkerChoiceStrategyContext< */ public execute (): number { if ( - this.pool.type === PoolType.DYNAMIC && - !this.pool.full && - this.pool.findFreeWorkerKey() === -1 + this.workerChoiceStrategy.isDynamicPool && + !this.workerChoiceStrategy.pool.full && + this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1 ) { return this.createWorkerCallback() }