refactor: cleanup workerId usage in messaging
[poolifier.git] / src / worker / abstract-worker.ts
index 1b64dc4f4cfff7c769ade8ef2bbd100efa1885ab..11a2263a5b5238ac4f728daa8ed97f6ca013c86f 100644 (file)
@@ -18,15 +18,11 @@ import { KillBehaviors, type WorkerOptions } from './worker-options'
 import type {
   TaskAsyncFunction,
   TaskFunction,
+  TaskFunctionOperationReturnType,
   TaskFunctions,
   TaskSyncFunction
 } from './task-functions'
 
-interface TaskFunctionOperationReturnType {
-  status: boolean
-  error?: Error
-}
-
 const DEFAULT_MAX_INACTIVE_TIME = 60000
 const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
   /**
@@ -217,7 +213,7 @@ export abstract class AbstractWorker<
         this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
       }
       this.taskFunctions.set(name, boundFn)
-      this.sendTaskFunctionsListToMainWorker()
+      this.sendTaskFunctionNamesToMainWorker()
       return { status: true }
     } catch (error) {
       return { status: false, error: error as Error }
@@ -247,7 +243,7 @@ export abstract class AbstractWorker<
         )
       }
       const deleteStatus = this.taskFunctions.delete(name)
-      this.sendTaskFunctionsListToMainWorker()
+      this.sendTaskFunctionNamesToMainWorker()
       return { status: deleteStatus }
     } catch (error) {
       return { status: false, error: error as Error }
@@ -355,9 +351,7 @@ export abstract class AbstractWorker<
   ): void {
     const { taskFunctionOperation, taskFunction, taskFunctionName } = message
     let response!: TaskFunctionOperationReturnType
-    if (taskFunctionOperation === 'has') {
-      response = this.hasTaskFunction(taskFunctionName as string)
-    } else if (taskFunctionOperation === 'add') {
+    if (taskFunctionOperation === 'add') {
       response = this.addTaskFunction(
         taskFunctionName as string,
         // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
@@ -377,8 +371,7 @@ export abstract class AbstractWorker<
       workerError: {
         name: taskFunctionName as string,
         message: this.handleError(response.error as Error | string)
-      },
-      workerId: this.id
+      }
     })
   }
 
@@ -392,11 +385,11 @@ export abstract class AbstractWorker<
     if (isAsyncFunction(this.opts.killHandler)) {
       (this.opts.killHandler?.() as Promise<void>)
         .then(() => {
-          this.sendToMainWorker({ kill: 'success', workerId: this.id })
+          this.sendToMainWorker({ kill: 'success' })
           return null
         })
         .catch(() => {
-          this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+          this.sendToMainWorker({ kill: 'failure' })
         })
         .finally(() => {
           this.emitDestroy()
@@ -406,9 +399,9 @@ export abstract class AbstractWorker<
       try {
         // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
         this.opts.killHandler?.() as void
-        this.sendToMainWorker({ kill: 'success', workerId: this.id })
+        this.sendToMainWorker({ kill: 'success' })
       } catch {
-        this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+        this.sendToMainWorker({ kill: 'failure' })
       } finally {
         this.emitDestroy()
       }
@@ -460,7 +453,7 @@ export abstract class AbstractWorker<
       performance.now() - this.lastTaskTimestamp >
       (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
     ) {
-      this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id })
+      this.sendToMainWorker({ kill: this.opts.killBehavior })
     }
   }
 
@@ -487,12 +480,11 @@ export abstract class AbstractWorker<
   ): void
 
   /**
-   * Sends the list of task function names to the main worker.
+   * Sends task function names to the main worker.
    */
-  protected sendTaskFunctionsListToMainWorker (): void {
+  protected sendTaskFunctionNamesToMainWorker (): void {
     this.sendToMainWorker({
-      taskFunctionNames: this.listTaskFunctionNames(),
-      workerId: this.id
+      taskFunctionNames: this.listTaskFunctionNames()
     })
   }
 
@@ -522,7 +514,6 @@ export abstract class AbstractWorker<
           message: `Task function '${name as string}' not found`,
           data
         },
-        workerId: this.id,
         taskId
       })
       return
@@ -552,7 +543,6 @@ export abstract class AbstractWorker<
       this.sendToMainWorker({
         data: res,
         taskPerformance,
-        workerId: this.id,
         taskId
       })
     } catch (error) {
@@ -562,7 +552,6 @@ export abstract class AbstractWorker<
           message: this.handleError(error as Error | string),
           data
         },
-        workerId: this.id,
         taskId
       })
     } finally {
@@ -588,7 +577,6 @@ export abstract class AbstractWorker<
         this.sendToMainWorker({
           data: res,
           taskPerformance,
-          workerId: this.id,
           taskId
         })
         return null
@@ -600,7 +588,6 @@ export abstract class AbstractWorker<
             message: this.handleError(error as Error | string),
             data
           },
-          workerId: this.id,
           taskId
         })
       })