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=3d122f0292d77805a3e59fb56a386fbec1b54396;hpb=d59df138a323dd194fcf1a8e2095142df3fc9fb9;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 3d122f02..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, + 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() }