refactor: improve task function operation handling on the worker side
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Sep 2023 19:57:27 +0000 (21:57 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Sep 2023 19:57:27 +0000 (21:57 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/worker/abstract-worker.ts

index dc1f6adc02ba8a7b433f08ae8e4d3f4d9abe7b3d..fde55a588e85413d1f6bad59d0abffee25d39c73 100644 (file)
@@ -379,19 +379,26 @@ export abstract class AbstractWorker<
   ): void {
     const { taskFunctionOperation, taskFunctionName, taskFunction } = message
     let response!: TaskFunctionOperationResult
-    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)
+    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,