Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
index 8290f89ed9a143a8420a45cb1c5965454bebe35e..1bba1a55eccdc82a02d6c0849c2413c68b70b9dc 100644 (file)
@@ -1,23 +1,23 @@
-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.
  *
- * @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
 > {
@@ -26,22 +26,22 @@ export class WorkerChoiceStrategyContext<
   /**
    * 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 createDynamicallyWorkerCallback 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 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.
+   * @param workerChoiceStrategy The worker choice strategy.
    * @returns The worker choice strategy instance for the pool type.
    */
   private getPoolWorkerChoiceStrategy (
@@ -54,14 +54,11 @@ export class WorkerChoiceStrategyContext<
         workerChoiceStrategy
       )
     }
-    return SelectionStrategiesUtils.getWorkerChoiceStrategy(
-      this.pool,
-      workerChoiceStrategy
-    )
+    return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy)
   }
 
   /**
-   * Get the worker choice strategy used in the context.
+   * Gets the worker choice strategy used in the context.
    *
    * @returns The worker choice strategy.
    */
@@ -70,20 +67,20 @@ export class WorkerChoiceStrategyContext<
   }
 
   /**
-   * Set the worker choice strategy to use in the context.
+   * 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)
   }
 
   /**
-   * Choose a worker with the underlying selection strategy.
+   * Chooses a worker with the underlying selection strategy.
    *
    * @returns The chosen one.
    */