refactor: cleanup worker.addTaskFunction()
[poolifier.git] / src / worker / abstract-worker.ts
index 3adc56d13ee3d5aa4be402cd38dd179bc05ec594..7d5494deeb3bbe80d2a725bd6ef0d685ca288775 100644 (file)
@@ -142,11 +142,11 @@ export abstract class AbstractWorker<
           )
         }
         const boundFn = fn.bind(this)
-        this.taskFunctions.set(name, boundFn)
         if (firstEntry) {
           this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
           firstEntry = false
         }
+        this.taskFunctions.set(name, boundFn)
       }
       if (firstEntry) {
         throw new Error('taskFunctions parameter object is empty')
@@ -198,8 +198,8 @@ export abstract class AbstractWorker<
     if (typeof fn !== 'function') {
       throw new TypeError('fn parameter is not a function')
     }
-    const boundFn = fn.bind(this)
     try {
+      const boundFn = fn.bind(this)
       if (
         this.taskFunctions.get(name) ===
         this.taskFunctions.get(DEFAULT_TASK_NAME)
@@ -242,7 +242,16 @@ export abstract class AbstractWorker<
   }
 
   /**
-   * Sets the default task function to use when no task function name is specified.
+   * Lists the names of the worker's task functions.
+   *
+   * @returns The names of the worker's task functions.
+   */
+  public listTaskFunctions (): string[] {
+    return Array.from(this.taskFunctions.keys())
+  }
+
+  /**
+   * Sets the default task function to use in the worker.
    *
    * @param name - The name of the task function to use as default task function.
    * @returns Whether the default task function was set or not.