refactor: remove deprecated getter in worker selection strategies code
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
index a88a8eed1eee1b2f627fcd4b1fe89489e44a8394..bd616a54d666ffc3372d09e040686e3e9aa1aca2 100644 (file)
@@ -1,38 +1,39 @@
-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,
+  RequiredStatistics,
   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.
  *
- * @template Worker Type of worker.
- * @template Data Type of data sent to the worker. This can only be serializable data.
- * @template Response Type of response of execution. This can only be serializable data.
+ * @typeParam Worker - Type of worker.
+ * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
+ * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class WorkerChoiceStrategyContext<
-  Worker extends AbstractPoolWorker,
+  Worker extends IPoolWorker,
   Data,
   Response
 > {
-  private workerChoiceStrategy!: IWorkerChoiceStrategy<Worker>
+  private workerChoiceStrategy!: IWorkerChoiceStrategy
 
   /**
    * Worker choice strategy context constructor.
    *
-   * @param pool The pool instance.
-   * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
-   * @param workerChoiceStrategy The worker choice strategy.
+   * @param pool The pool instance.
+   * @param createWorkerCallback - The worker creation callback for dynamic pool.
+   * @param workerChoiceStrategy The worker choice strategy.
    */
   public constructor (
     private readonly pool: IPoolInternal<Worker, Data, Response>,
-    private createDynamicallyWorkerCallback: () => Worker,
+    private readonly createWorkerCallback: () => number,
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.setWorkerChoiceStrategy(workerChoiceStrategy)
@@ -41,53 +42,60 @@ export class WorkerChoiceStrategyContext<
   /**
    * Gets the worker choice strategy instance specific to the pool type.
    *
-   * @param workerChoiceStrategy The worker choice strategy.
+   * @param workerChoiceStrategy The worker choice strategy.
    * @returns The worker choice strategy instance for the pool type.
    */
   private getPoolWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
-  ): IWorkerChoiceStrategy<Worker> {
+  ): IWorkerChoiceStrategy {
     if (this.pool.type === PoolType.DYNAMIC) {
       return new DynamicPoolWorkerChoiceStrategy(
         this.pool,
-        this.createDynamicallyWorkerCallback,
+        this.createWorkerCallback,
         workerChoiceStrategy
       )
     }
-    return SelectionStrategiesUtils.getWorkerChoiceStrategy(
-      this.pool,
-      workerChoiceStrategy
-    )
+    return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy)
   }
 
   /**
-   * Gets the worker choice strategy used in the context.
+   * Gets the worker choice strategy required statistics.
    *
-   * @returns The worker choice strategy.
+   * @returns The required statistics.
    */
-  public getWorkerChoiceStrategy (): IWorkerChoiceStrategy<Worker> {
-    return this.workerChoiceStrategy
+  public getRequiredStatistics (): RequiredStatistics {
+    return this.workerChoiceStrategy.requiredStatistics
   }
 
   /**
    * Sets the worker choice strategy to use in the context.
    *
-   * @param workerChoiceStrategy The worker choice strategy to set.
+   * @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)
   }
 
   /**
    * Chooses a worker with the underlying selection strategy.
    *
-   * @returns The chosen one.
+   * @returns The key of the chosen one.
    */
-  public execute (): Worker {
+  public execute (): number {
     return this.workerChoiceStrategy.choose()
   }
+
+  /**
+   * Removes a worker in the underlying selection strategy internals.
+   *
+   * @param workerKey - The key of the worker to remove.
+   * @returns `true` if the removal is successful, `false` otherwise.
+   */
+  public remove (workerKey: number): boolean {
+    return this.workerChoiceStrategy.remove(workerKey)
+  }
 }