refactor: factor out code to get worker function
[poolifier.git] / src / worker / abstract-worker.ts
index 788c5dfa4aabcc2c5343567dabc8be14fba5e4a2..2aca9e914b2f4f296624f842ccbee9e159790815 100644 (file)
@@ -12,6 +12,7 @@ import { EMPTY_FUNCTION } from '../utils'
 import type { KillBehavior, WorkerOptions } from './worker-options'
 import { KillBehaviors } from './worker-options'
 
+const DEFAULT_FUNCTION_NAME = 'default'
 const DEFAULT_MAX_INACTIVE_TIME = 60000
 const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
 
@@ -104,7 +105,9 @@ export abstract class AbstractWorker<
     | WorkerFunction<Data, Response>
     | TaskFunctions<Data, Response>
   ): void {
-    if (taskFunctions == null) { throw new Error('taskFunctions parameter is mandatory') }
+    if (taskFunctions == null) {
+      throw new Error('taskFunctions parameter is mandatory')
+    }
     if (
       typeof taskFunctions !== 'function' &&
       typeof taskFunctions !== 'object'
@@ -129,7 +132,7 @@ export abstract class AbstractWorker<
         this.taskFunctions.set(name, fn.bind(this))
       }
     } else {
-      this.taskFunctions.set('default', taskFunctions.bind(this))
+      this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
     }
   }
 
@@ -140,13 +143,8 @@ export abstract class AbstractWorker<
    */
   protected messageListener (message: MessageValue<Data, MainWorker>): void {
     if (message.id != null && message.data != null) {
-      let fn: WorkerFunction<Data, Response> | undefined
-      if (message.name == null) {
-        fn = this.taskFunctions.get('default')
-      } else {
-        fn = this.taskFunctions.get(message.name)
-      }
       // Task message received
+      const fn = this.getTaskFunction(message.name)
       if (fn?.constructor.name === 'AsyncFunction') {
         this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
       } else {
@@ -260,4 +258,13 @@ export abstract class AbstractWorker<
       })
       .catch(EMPTY_FUNCTION)
   }
+
+  private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
+    name = name ?? DEFAULT_FUNCTION_NAME
+    const fn = this.taskFunctions.get(name)
+    if (fn == null) {
+      throw new Error(`Task function "${name}" not found`)
+    }
+    return fn
+  }
 }