chore: generate documentation
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 1499db945f8297181724db968f21b938d8f3618b..29b05fa507f0b3ca8a18a300ad1c6927d9b76a65 100644 (file)
@@ -11,8 +11,8 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types'
  */
 export class RoundRobinWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
@@ -21,13 +21,13 @@ export class RoundRobinWorkerChoiceStrategy<
    */
   private nextWorkerId: number = 0
 
-  /** {@inheritDoc} */
+  /** @inheritDoc */
   public reset (): boolean {
     this.nextWorkerId = 0
     return true
   }
 
-  /** {@inheritDoc} */
+  /** @inheritDoc */
   public choose (): number {
     const chosenWorkerKey = this.nextWorkerId
     this.nextWorkerId =
@@ -37,13 +37,17 @@ export class RoundRobinWorkerChoiceStrategy<
     return chosenWorkerKey
   }
 
-  /** {@inheritDoc} */
+  /** @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
+      if (this.pool.workers.length === 0) {
+        this.nextWorkerId = 0
+      } else {
+        this.nextWorkerId =
+          this.nextWorkerId > this.pool.workers.length - 1
+            ? this.pool.workers.length - 1
+            : this.nextWorkerId
+      }
     }
     return true
   }