feat: ensure charging station add op return its station info
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorker.ts
index 4eb6e6a0aa8d8a13ed7091e8255f35029ed62fce..042d3d5bfc2b3bc913484c5a0f5d0982b982148f 100644 (file)
@@ -4,25 +4,26 @@ import { parentPort } from 'node:worker_threads'
 
 import { ThreadWorker } from 'poolifier'
 
-import { ChargingStation } from './ChargingStation.js'
 import { BaseError } from '../exception/index.js'
 import type {
-  ChargingStationData,
+  ChargingStationInfo,
   ChargingStationWorkerData,
   ChargingStationWorkerEventError,
   ChargingStationWorkerMessage
 } from '../types/index.js'
-import { Configuration, buildChargingStationDataPayload } from '../utils/index.js'
+import { Configuration } from '../utils/index.js'
 import { type WorkerMessage, WorkerMessageEvents } from '../worker/index.js'
+import { ChargingStation } from './ChargingStation.js'
 
 export let chargingStationWorker: object
 if (Configuration.workerPoolInUse()) {
-  chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(
-    (data?: ChargingStationWorkerData): void => {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      new ChargingStation(data!.index, data!.templateFile).add()
-    }
-  )
+  chargingStationWorker = new ThreadWorker<
+  ChargingStationWorkerData,
+  ChargingStationInfo | undefined
+  >((data?: ChargingStationWorkerData): ChargingStationInfo | undefined => {
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, no-new
+    return new ChargingStation(data!.index, data!.templateFile, data!.options).stationInfo
+  })
 } else {
   // eslint-disable-next-line @typescript-eslint/no-extraneous-class
   class ChargingStationWorker<Data extends ChargingStationWorkerData> {
@@ -33,18 +34,19 @@ if (Configuration.workerPoolInUse()) {
             try {
               const chargingStation = new ChargingStation(
                 message.data.index,
-                message.data.templateFile
+                message.data.templateFile,
+                message.data.options
               )
-              chargingStation.add()
               parentPort?.postMessage({
                 event: WorkerMessageEvents.addedWorkerElement,
-                data: buildChargingStationDataPayload(chargingStation)
-              } satisfies ChargingStationWorkerMessage<ChargingStationData>)
+                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+                data: chargingStation.stationInfo!
+              } satisfies ChargingStationWorkerMessage<ChargingStationInfo>)
             } catch (error) {
               parentPort?.postMessage({
                 event: WorkerMessageEvents.workerElementError,
                 data: {
-                  event: WorkerMessageEvents.addWorkerElement,
+                  event: message.event,
                   name: (error as Error).name,
                   message: (error as Error).message,
                   stack: (error as Error).stack