fix: fix 'Cannot read properties of undefined' with revervation enabled
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorker.ts
index 4361ab9badd0a6f0e6d80b82f16e62a8fed0a804..3a723eca25dd5dd86902c510354c9069332643d6 100644 (file)
@@ -1,46 +1,47 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { parentPort, workerData } from 'node:worker_threads';
+import { AsyncResource } from 'node:async_hooks';
+import { parentPort } from 'node:worker_threads';
 
 import { ThreadWorker } from 'poolifier';
 
-import { ChargingStation, ChargingStationUtils } from './internal';
+import { ChargingStation } from './ChargingStation';
 import type { ChargingStationWorkerData } from '../types';
-import { Utils } from '../utils';
+import { Configuration } from '../utils';
 import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker';
 
+const moduleName = 'ChargingStationWorker';
+
 /**
- * Create and start a charging station instance
+ * Creates and starts a charging station instance
  *
  * @param data - workerData
  */
-const startChargingStation = (data: ChargingStationWorkerData): void => {
-  const station = new ChargingStation(data.index, data.templateFile);
-  station.start();
+const startChargingStation = (data?: ChargingStationWorkerData): void => {
+  new ChargingStation(data!.index, data!.templateFile).start();
 };
 
-/**
- * Listen messages send by the main thread
- */
-const addMessageListener = (): void => {
-  parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
-    if (message.id === WorkerMessageEvents.startWorkerElement) {
-      startChargingStation(message.data);
-    }
-  });
-};
+class ChargingStationWorker extends AsyncResource {
+  constructor() {
+    super(moduleName);
+    // Add message listener to create and start charging station from the main thread
+    parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
+      if (message.event === WorkerMessageEvents.startWorkerElement) {
+        this.runInAsyncScope(
+          startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void,
+          this,
+          message.data,
+        );
+      }
+    });
+  }
+}
 
-// Conditionally export ThreadWorker instance for pool usage
-export let threadWorker: ThreadWorker;
-if (ChargingStationUtils.workerPoolInUse()) {
-  threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
+export let chargingStationWorker: ChargingStationWorker | ThreadWorker<ChargingStationWorkerData>;
+if (Configuration.workerPoolInUse()) {
+  chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
     maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
-    async: false,
   });
 } else {
-  // Add message listener to start charging station from main thread
-  addMessageListener();
-  if (Utils.isUndefined(workerData) === false) {
-    startChargingStation(workerData as ChargingStationWorkerData);
-  }
+  chargingStationWorker = new ChargingStationWorker();
 }