refactor: use class property if appropriate
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 44dfa4b76a941fe6caa005fe07bf5dc5ccd34644..9b49d7643581d8a4991bc1b25239b2053e0265d2 100644 (file)
@@ -1,5 +1,6 @@
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { IWorkerChoiceStrategy } from './selection-strategies-types'
 
 /**
  * Selects the next worker in a round robin fashion.
@@ -9,10 +10,12 @@ import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class RoundRobinWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data = unknown,
+    Response = unknown
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /**
    * Id of the next worker.
    */
@@ -25,13 +28,23 @@ export class RoundRobinWorkerChoiceStrategy<
   }
 
   /** {@inheritDoc} */
-  public choose (): Worker {
-    const chosenWorker = this.pool.workers.get(this.nextWorkerId)
-      ?.worker as Worker
+  public choose (): number {
+    const chosenWorkerKey = this.nextWorkerId
     this.nextWorkerId =
-      this.nextWorkerId === this.pool.workers.size - 1
+      this.nextWorkerId === this.pool.workers.length - 1
         ? 0
         : this.nextWorkerId + 1
-    return chosenWorker
+    return chosenWorkerKey
+  }
+
+  /** {@inheritDoc} */
+  public remove (workerKey: number): boolean {
+    if (this.nextWorkerId === workerKey) {
+      this.nextWorkerId =
+        this.nextWorkerId > this.pool.workers.length - 1
+          ? this.pool.workers.length - 1
+          : this.nextWorkerId
+    }
+    return true
   }
 }