From: Jérôme Benoit Date: Thu, 1 Jun 2023 02:43:07 +0000 (+0200) Subject: refactor: encapsulate worker in an object X-Git-Tag: v1.2.16~23 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6b57eb9a3fccc8e29028b00a3a1f33544852760f;p=e-mobility-charging-stations-simulator.git refactor: encapsulate worker in an object Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 19dd6f8d..3703de9c 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -1,7 +1,7 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. import { AsyncResource } from 'node:async_hooks'; -import { parentPort, workerData } from 'node:worker_threads'; +import { parentPort } from 'node:worker_threads'; import { ThreadWorker } from 'poolifier'; @@ -19,6 +19,27 @@ const startChargingStation = (data: ChargingStationWorkerData): void => { new ChargingStation(data.index, data.templateFile).start(); }; +class ChargingStationWorker extends AsyncResource { + constructor() { + super('ChargingStationWorker'); + // Add message listener to create and start charging station from the main thread + parentPort?.on('message', (message: WorkerMessage) => { + if (message.id === WorkerMessageEvents.startWorkerElement) { + this.run(message.data); + } + }); + } + + private run(data: ChargingStationWorkerData): void { + this.runInAsyncScope( + startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void, + this, + data + ); + } +} + +export let chargingStationWorker: ChargingStationWorker; // Conditionally export ThreadWorker instance for pool usage export let threadWorker: ThreadWorker; if (Configuration.workerPoolInUse()) { @@ -26,26 +47,5 @@ if (Configuration.workerPoolInUse()) { maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME, }); } else { - class ChargingStationWorker extends AsyncResource { - constructor() { - super('ChargingStationWorker'); - } - - public run(data: ChargingStationWorkerData): void { - this.runInAsyncScope( - startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void, - this, - data - ); - } - } - // Add message listener to create and start charging station from the main thread - parentPort?.on('message', (message: WorkerMessage) => { - if (message.id === WorkerMessageEvents.startWorkerElement) { - new ChargingStationWorker().run(message.data); - } - }); - if (workerData !== undefined) { - new ChargingStationWorker().run(workerData as ChargingStationWorkerData); - } + chargingStationWorker = new ChargingStationWorker(); }