perf: a charging station worker is not an AsyncResource
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 11 Dec 2023 12:49:21 +0000 (13:49 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 11 Dec 2023 12:49:21 +0000 (13:49 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorker.ts

index 955ce933b5ffb27d4e6315bd4206cd7373b101f3..d9e462d064967b75255dc333f3926f28b14b4cf4 100644 (file)
@@ -1,6 +1,5 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { AsyncResource } from 'node:async_hooks';
 import { parentPort } from 'node:worker_threads';
 
 import { ThreadWorker } from 'poolifier';
@@ -9,9 +8,7 @@ import { ChargingStation } from './ChargingStation';
 import { BaseError } from '../exception';
 import type { ChargingStationWorkerData } from '../types';
 import { Configuration } from '../utils';
-import { type WorkerData, type WorkerMessage, WorkerMessageEvents } from '../worker';
-
-const moduleName = 'ChargingStationWorker';
+import { type WorkerMessage, WorkerMessageEvents } from '../worker';
 
 /**
  * Creates and starts a charging station instance
@@ -22,19 +19,14 @@ const startChargingStation = (data?: ChargingStationWorkerData): void => {
   new ChargingStation(data!.index, data!.templateFile).start();
 };
 
-class ChargingStationWorker<Data extends WorkerData> extends AsyncResource {
+class ChargingStationWorker<Data extends ChargingStationWorkerData> {
   constructor() {
-    super(moduleName);
     // Add message listener to create and start charging station from the main thread
     parentPort?.on('message', (message: WorkerMessage<Data>) => {
       switch (message.event) {
         case WorkerMessageEvents.startWorkerElement:
           try {
-            this.runInAsyncScope(
-              startChargingStation.bind(this) as (data?: Data) => void,
-              this,
-              message.data,
-            );
+            startChargingStation(message.data);
             parentPort?.postMessage({
               event: WorkerMessageEvents.startedWorkerElement,
             });