]> Piment Noir Git Repositories - poolifier.git/commitdiff
perf: remove unneeded worker class event emitter inheritance
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 17:09:32 +0000 (19:09 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 17:09:32 +0000 (19:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/worker/abstract-worker.ts
src/worker/worker-types.ts [deleted file]

index 3b5e6af07c69322298d01cc875e520fd7852f097..caa95a9b0f64bbf46b6d7f4a1573234a714c9702 100644 (file)
@@ -1,7 +1,6 @@
 import type { Worker } from 'node:cluster'
 import type { MessagePort } from 'node:worker_threads'
 
-import { EventEmitter } from 'node:events'
 import { performance } from 'node:perf_hooks'
 
 import type {
@@ -19,7 +18,6 @@ import type {
   TaskFunctions,
   TaskSyncFunction,
 } from './task-functions.js'
-import type { AbortTaskEventDetail } from './worker-types.js'
 
 import {
   buildTaskFunctionProperties,
@@ -63,7 +61,7 @@ export abstract class AbstractWorker<
   MainWorker extends MessagePort | Worker,
   Data = unknown,
   Response = unknown
-> extends EventEmitter {
+> {
   /**
    * Handler id of the `activeInterval` worker activity check.
    */
@@ -108,7 +106,6 @@ export abstract class AbstractWorker<
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
   ) {
-    super()
     if (this.isMain == null) {
       throw new Error('isMain parameter is mandatory')
     }
@@ -117,12 +114,6 @@ export abstract class AbstractWorker<
       `${string}-${string}-${string}-${string}-${string}`,
       () => void
     >()
-    this.on('abortTask', (eventDetail: AbortTaskEventDetail) => {
-      const { taskId } = eventDetail
-      if (this.taskAbortFunctions.has(taskId)) {
-        this.taskAbortFunctions.get(taskId)?.()
-      }
-    })
     this.checkWorkerOptions(this.opts)
     if (!this.isMain) {
       // Should be once() but Node.js on windows has a bug that prevents it from working
@@ -319,7 +310,6 @@ export abstract class AbstractWorker<
         this.sendToMainWorker({ kill: 'failure' })
       }
     }
-    this.removeAllListeners()
   }
 
   /**
@@ -416,7 +406,9 @@ export abstract class AbstractWorker<
       this.run(message)
     } else if (taskOperation === 'abort' && taskId != null) {
       // Abort task operation message received
-      this.emit('abortTask', { taskId })
+      if (this.taskAbortFunctions.has(taskId)) {
+        this.taskAbortFunctions.get(taskId)?.()
+      }
     } else if (kill === true) {
       // Kill message received
       this.handleKillMessage(message)
diff --git a/src/worker/worker-types.ts b/src/worker/worker-types.ts
deleted file mode 100644 (file)
index 706b155..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-export interface AbortTaskEventDetail {
-  taskId: `${string}-${string}-${string}-${string}-${string}`
-}