From 8e77a4e6c2aab9717609ce5f687d4c0103de7c4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 1 Jul 2025 19:09:32 +0200 Subject: [PATCH] perf: remove unneeded worker class event emitter inheritance MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 16 ++++------------ src/worker/worker-types.ts | 3 --- 2 files changed, 4 insertions(+), 15 deletions(-) delete mode 100644 src/worker/worker-types.ts diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 3b5e6af07..caa95a9b0 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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 | TaskFunctions, 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 index 706b155e1..000000000 --- a/src/worker/worker-types.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface AbortTaskEventDetail { - taskId: `${string}-${string}-${string}-${string}-${string}` -} -- 2.43.0