Strict boolean check
[poolifier.git] / src / pools / abstract-pool.ts
index 0921f45644446c18512f876a0a13b3379f8026fb..369e5b90617bab507d97e36331c00a93d6a47c67 100644 (file)
@@ -135,7 +135,7 @@ export abstract class AbstractPool<
       throw new Error(
         'Cannot instantiate a pool without specifying the number of workers'
       )
-    } else if (!Number.isSafeInteger(numberOfWorkers)) {
+    } else if (Number.isSafeInteger(numberOfWorkers) === false) {
       throw new Error(
         'Cannot instantiate a pool with a non integer number of workers'
       )
@@ -226,7 +226,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Shut down given worker.
+   * Shutdowns given worker.
    *
    * @param worker A worker within `workers`.
    */
@@ -279,11 +279,11 @@ export abstract class AbstractPool<
   protected removeWorker (worker: Worker): void {
     // Clean worker from data structure
     this.workers.splice(this.getWorkerIndex(worker), 1)
-    this.resetWorkerTasksUsage(worker)
+    this.removeWorkerTasksUsage(worker)
   }
 
   /**
-   * Choose a worker for the next task.
+   * Chooses a worker for the next task.
    *
    * The default implementation uses a round robin algorithm to distribute the load.
    *
@@ -294,7 +294,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Send a message to the given worker.
+   * Sends a message to the given worker.
    *
    * @param worker The worker which should receive the message.
    * @param message The message.
@@ -305,7 +305,7 @@ export abstract class AbstractPool<
   ): void
 
   /**
-   * Register a listener callback on a given worker.
+   * Registers a listener callback on a given worker.
    *
    * @param worker A worker.
    * @param listener A message listener callback.
@@ -393,7 +393,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Increase the number of tasks that the given worker has applied.
+   * Increases the number of tasks that the given worker has applied.
    *
    * @param worker Worker which running tasks is increased.
    */
@@ -402,7 +402,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Decrease the number of tasks that the given worker has applied.
+   * Decreases the number of tasks that the given worker has applied.
    *
    * @param worker Worker which running tasks is decreased.
    */
@@ -411,7 +411,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Step the number of tasks that the given worker has applied.
+   * Steps the number of tasks that the given worker has applied.
    *
    * @param worker Worker which running tasks are stepped.
    * @param step Number of running tasks step.
@@ -427,12 +427,12 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Step the number of tasks that the given worker has run.
+   * Steps the number of tasks that the given worker has run.
    *
    * @param worker Worker which has run tasks.
    * @param step Number of run tasks step.
    */
-  private stepWorkerRunTasks (worker: Worker, step: number) {
+  private stepWorkerRunTasks (worker: Worker, step: number): void {
     const tasksUsage = this.workersTasksUsage.get(worker)
     if (tasksUsage !== undefined) {
       tasksUsage.run = tasksUsage.run + step
@@ -443,31 +443,38 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Update tasks run time for the given worker.
+   * Updates tasks runtime for the given worker.
    *
    * @param worker Worker which run the task.
-   * @param taskRunTime Worker task run time.
+   * @param taskRunTime Worker task runtime.
    */
   private updateWorkerTasksRunTime (
     worker: Worker,
     taskRunTime: number | undefined
-  ) {
-    const tasksUsage = this.workersTasksUsage.get(worker)
-    if (tasksUsage !== undefined && tasksUsage.run !== 0) {
-      tasksUsage.runTime += taskRunTime ?? 0
-      tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run
-      this.workersTasksUsage.set(worker, tasksUsage)
-    } else {
-      throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+  ): void {
+    if (
+      this.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime === true
+    ) {
+      const tasksUsage = this.workersTasksUsage.get(worker)
+      if (tasksUsage !== undefined) {
+        tasksUsage.runTime += taskRunTime ?? 0
+        if (tasksUsage.run !== 0) {
+          tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run
+        }
+        this.workersTasksUsage.set(worker, tasksUsage)
+      } else {
+        throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+      }
     }
   }
 
   /**
-   * Reset worker tasks usage statistics.
+   * Removes worker tasks usage statistics.
    *
    * @param worker The worker.
    */
-  private resetWorkerTasksUsage (worker: Worker): void {
+  private removeWorkerTasksUsage (worker: Worker): void {
     this.workersTasksUsage.delete(worker)
   }
 }