refactor: improve task function operation handling on the worker side
[poolifier.git] / src / worker / abstract-worker.ts
index 8bbd07a713c2a30c7f4614012350de54ed69bf55..fde55a588e85413d1f6bad59d0abffee25d39c73 100644 (file)
@@ -18,7 +18,7 @@ import { KillBehaviors, type WorkerOptions } from './worker-options'
 import type {
   TaskAsyncFunction,
   TaskFunction,
-  TaskFunctionOperationReturnType,
+  TaskFunctionOperationResult,
   TaskFunctions,
   TaskSyncFunction
 } from './task-functions'
@@ -201,7 +201,7 @@ export abstract class AbstractWorker<
    * @param name - The name of the task function to check.
    * @returns Whether the worker has a task function with the given name or not.
    */
-  public hasTaskFunction (name: string): TaskFunctionOperationReturnType {
+  public hasTaskFunction (name: string): TaskFunctionOperationResult {
     try {
       this.checkTaskFunctionName(name)
     } catch (error) {
@@ -221,7 +221,7 @@ export abstract class AbstractWorker<
   public addTaskFunction (
     name: string,
     fn: TaskFunction<Data, Response>
-  ): TaskFunctionOperationReturnType {
+  ): TaskFunctionOperationResult {
     try {
       this.checkTaskFunctionName(name)
       if (name === DEFAULT_TASK_NAME) {
@@ -253,7 +253,7 @@ export abstract class AbstractWorker<
    * @param name - The name of the task function to remove.
    * @returns Whether the task function existed and was removed or not.
    */
-  public removeTaskFunction (name: string): TaskFunctionOperationReturnType {
+  public removeTaskFunction (name: string): TaskFunctionOperationResult {
     try {
       this.checkTaskFunctionName(name)
       if (name === DEFAULT_TASK_NAME) {
@@ -309,7 +309,7 @@ export abstract class AbstractWorker<
    * @param name - The name of the task function to use as default task function.
    * @returns Whether the default task function was set or not.
    */
-  public setDefaultTaskFunction (name: string): TaskFunctionOperationReturnType {
+  public setDefaultTaskFunction (name: string): TaskFunctionOperationResult {
     try {
       this.checkTaskFunctionName(name)
       if (name === DEFAULT_TASK_NAME) {
@@ -378,28 +378,39 @@ export abstract class AbstractWorker<
     message: MessageValue<Data>
   ): void {
     const { taskFunctionOperation, taskFunctionName, taskFunction } = message
-    let response!: TaskFunctionOperationReturnType
-    if (taskFunctionOperation === 'add') {
-      response = this.addTaskFunction(
-        taskFunctionName as string,
-        // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
-        new Function(`return ${taskFunction as string}`)() as TaskFunction<
-        Data,
-        Response
-        >
-      )
-    } else if (taskFunctionOperation === 'remove') {
-      response = this.removeTaskFunction(taskFunctionName as string)
-    } else if (taskFunctionOperation === 'default') {
-      response = this.setDefaultTaskFunction(taskFunctionName as string)
+    let response!: TaskFunctionOperationResult
+    switch (taskFunctionOperation) {
+      case 'add':
+        response = this.addTaskFunction(
+          taskFunctionName as string,
+          // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
+          new Function(`return ${taskFunction as string}`)() as TaskFunction<
+          Data,
+          Response
+          >
+        )
+        break
+      case 'remove':
+        response = this.removeTaskFunction(taskFunctionName as string)
+        break
+      case 'default':
+        response = this.setDefaultTaskFunction(taskFunctionName as string)
+        break
+      default:
+        response = { status: false, error: new Error('Unknown task operation') }
+        break
     }
     this.sendToMainWorker({
       taskFunctionOperation,
       taskFunctionOperationStatus: response.status,
-      workerError: {
-        name: taskFunctionName as string,
-        message: this.handleError(response.error as Error | string)
-      }
+      taskFunctionName,
+      ...(!response.status &&
+        response?.error != null && {
+        workerError: {
+          name: taskFunctionName as string,
+          message: this.handleError(response.error as Error | string)
+        }
+      })
     })
   }
 
@@ -607,7 +618,7 @@ export abstract class AbstractWorker<
           taskPerformance,
           taskId
         })
-        return null
+        return undefined
       })
       .catch(error => {
         this.sendToMainWorker({