refactor: factor out worker helpers
[poolifier.git] / src / pools / worker-node.ts
index 68c8cab2ef97070ccea235dfd391ed5479978120..5a25f8d9bf233c4afdd205bfd504a6e43be23aab 100644 (file)
@@ -20,6 +20,7 @@ import {
   WorkerTypes,
   type WorkerUsage
 } from './worker'
+import { checkWorkerNodeArguments } from './utils'
 
 /**
  * Worker node.
@@ -57,7 +58,7 @@ implements IWorkerNode<Worker, Data> {
    * @param tasksQueueBackPressureSize - The tasks queue back pressure size.
    */
   constructor (worker: Worker, tasksQueueBackPressureSize: number) {
-    this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
+    checkWorkerNodeArguments<Worker>(worker, tasksQueueBackPressureSize)
     this.worker = worker
     this.info = this.initWorkerInfo(worker)
     this.usage = this.initWorkerUsage()
@@ -161,21 +162,21 @@ implements IWorkerNode<Worker, Data> {
 
   /** @inheritdoc */
   public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
-    if (!Array.isArray(this.info.taskFunctions)) {
+    if (!Array.isArray(this.info.taskFunctionNames)) {
       throw new Error(
         `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
       )
     }
     if (
-      Array.isArray(this.info.taskFunctions) &&
-      this.info.taskFunctions.length < 3
+      Array.isArray(this.info.taskFunctionNames) &&
+      this.info.taskFunctionNames.length < 3
     ) {
       throw new Error(
         `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
       )
     }
     if (name === DEFAULT_TASK_NAME) {
-      name = this.info.taskFunctions[1]
+      name = this.info.taskFunctionNames[1]
     }
     if (!this.taskFunctionsUsage.has(name)) {
       this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
@@ -183,6 +184,11 @@ implements IWorkerNode<Worker, Data> {
     return this.taskFunctionsUsage.get(name)
   }
 
+  /** @inheritdoc */
+  public deleteTaskFunctionWorkerUsage (name: string): boolean {
+    return this.taskFunctionsUsage.delete(name)
+  }
+
   private async startOnEmptyQueue (): Promise<void> {
     if (
       this.onEmptyQueueCount > 0 &&
@@ -249,7 +255,7 @@ implements IWorkerNode<Worker, Data> {
       for (const task of this.tasksQueue) {
         if (
           (task.name === DEFAULT_TASK_NAME &&
-            name === (this.info.taskFunctions as string[])[1]) ||
+            name === (this.info.taskFunctionNames as string[])[1]) ||
           (task.name !== DEFAULT_TASK_NAME && name === task.name)
         ) {
           ++taskFunctionQueueSize
@@ -283,28 +289,4 @@ implements IWorkerNode<Worker, Data> {
       }
     }
   }
-
-  private checkWorkerNodeArguments (
-    worker: Worker,
-    tasksQueueBackPressureSize: number
-  ): void {
-    if (worker == null) {
-      throw new TypeError('Cannot construct a worker node without a worker')
-    }
-    if (tasksQueueBackPressureSize == null) {
-      throw new TypeError(
-        'Cannot construct a worker node without a tasks queue back pressure size'
-      )
-    }
-    if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
-      throw new TypeError(
-        'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
-      )
-    }
-    if (tasksQueueBackPressureSize <= 0) {
-      throw new RangeError(
-        'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
-      )
-    }
-  }
 }