Factor out worker tasks number in/de-crement. (#182)
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 17 Feb 2021 20:57:30 +0000 (21:57 +0100)
committerGitHub <noreply@github.com>
Wed, 17 Feb 2021 20:57:30 +0000 (21:57 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts

index 4a6ba72f4b7523bd2d8870dd1108c6225814ff16..4f467bf4ebe869ac8215d8c5cc6aa1842edb7aad 100644 (file)
@@ -201,26 +201,31 @@ export abstract class AbstractPool<
   /**
    * Increase the number of tasks that the given workers has done.
    *
-   * @param worker Workers whose tasks are increased.
+   * @param worker Worker whose tasks are increased.
    */
   protected increaseWorkersTask (worker: Worker): void {
-    const numberOfTasksInProgress = this.tasks.get(worker)
-    if (numberOfTasksInProgress !== undefined) {
-      this.tasks.set(worker, numberOfTasksInProgress + 1)
-    } else {
-      throw Error('Worker could not be found in tasks map')
-    }
+    this.stepWorkerNumberOfTasks(worker, 1)
   }
 
   /**
    * Decrease the number of tasks that the given workers has done.
    *
-   * @param worker Workers whose tasks are decreased.
+   * @param worker Worker whose tasks are decreased.
    */
   protected decreaseWorkersTasks (worker: Worker): void {
+    this.stepWorkerNumberOfTasks(worker, -1)
+  }
+
+  /**
+   * Step the number of tasks that the given workers has done.
+   *
+   * @param worker Worker whose tasks are set.
+   * @param step Worker number of tasks step.
+   */
+  private stepWorkerNumberOfTasks (worker: Worker, step: number) {
     const numberOfTasksInProgress = this.tasks.get(worker)
     if (numberOfTasksInProgress !== undefined) {
-      this.tasks.set(worker, numberOfTasksInProgress - 1)
+      this.tasks.set(worker, numberOfTasksInProgress + step)
     } else {
       throw Error('Worker could not be found in tasks map')
     }