From d769e04a20f93f2b11eacccc48c555236a65bf65 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 20 Nov 2023 11:19:58 +0100 Subject: [PATCH] refactor: strong type data sent to worker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationWorker.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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(); } -- 2.34.1