From: Jérôme Benoit Date: Mon, 20 Nov 2023 10:19:58 +0000 (+0100) Subject: refactor: strong type data sent to worker X-Git-Tag: v1.2.25~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d769e04a20f93f2b11eacccc48c555236a65bf65;p=e-mobility-charging-stations-simulator.git refactor: strong type data sent to worker Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 6201bf07..a0adf805 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -9,29 +9,29 @@ import { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; import type { ChargingStationWorkerData } from '../types'; import { Configuration } from '../utils'; -import { type WorkerMessage, WorkerMessageEvents } from '../worker'; +import { type WorkerData, type WorkerMessage, WorkerMessageEvents } from '../worker'; const moduleName = 'ChargingStationWorker'; /** * Creates and starts a charging station instance * - * @param data - workerData + * @param data - data sent to worker */ const startChargingStation = (data?: ChargingStationWorkerData): void => { new ChargingStation(data!.index, data!.templateFile).start(); }; -class ChargingStationWorker extends AsyncResource { +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) => { + parentPort?.on('message', (message: WorkerMessage) => { switch (message.event) { case WorkerMessageEvents.startWorkerElement: try { this.runInAsyncScope( - startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, + startChargingStation.bind(this) as (data?: Data) => void, this, message.data, ); @@ -61,9 +61,11 @@ class ChargingStationWorker extends AsyncResource { } } -export let chargingStationWorker: ChargingStationWorker | ThreadWorker; +export let chargingStationWorker: + | ChargingStationWorker + | ThreadWorker; if (Configuration.workerPoolInUse()) { chargingStationWorker = new ThreadWorker(startChargingStation); } else { - chargingStationWorker = new ChargingStationWorker(); + chargingStationWorker = new ChargingStationWorker(); }