build(deps-dev): bump @types/node
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index c2928930724d34a19687bde75de870d1dc9c4afb..f244374291da55b9173129b6384b00f49d952e65 100644 (file)
@@ -32,7 +32,7 @@ export abstract class AbstractWorkerChoiceStrategy<
   /**
    * The previous worker node key.
    */
-  protected previousWorkerNodeKey: number = 0
+  protected previousWorkerNodeKey = 0
 
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
@@ -57,8 +57,8 @@ export abstract class AbstractWorkerChoiceStrategy<
     protected readonly pool: IPool<Worker, Data, Response>,
     protected opts?: WorkerChoiceStrategyOptions
   ) {
-    this.setOptions(this.opts)
     this.choose = this.choose.bind(this)
+    this.setOptions(this.opts)
   }
 
   protected setTaskStatisticsRequirements (
@@ -132,11 +132,14 @@ export abstract class AbstractWorkerChoiceStrategy<
   }
 
   /**
-   * Check the next worker node readiness.
+   * Check the next worker node key.
    */
-  protected checkNextWorkerNodeReadiness (): void {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) {
+  protected checkNextWorkerNodeKey (): void {
+    if (
+      this.nextWorkerNodeKey != null &&
+      (this.nextWorkerNodeKey < 0 ||
+        !this.isWorkerNodeReady(this.nextWorkerNodeKey))
+    ) {
       delete this.nextWorkerNodeKey
     }
   }
@@ -189,6 +192,9 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @param workerNodeKey - The worker node key.
    */
   protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
-    this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
+    this.previousWorkerNodeKey =
+      workerNodeKey != null && workerNodeKey >= 0
+        ? workerNodeKey
+        : this.previousWorkerNodeKey
   }
 }