X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationWorker.ts;h=4361ab9badd0a6f0e6d80b82f16e62a8fed0a804;hb=e8044a69a745aab08dfeea0bd9ec9dd7fe84cdd7;hp=11d7cb0efa238530a321958f9cf7c0deefa3367a;hpb=60a743910478b70e39dcefa5e1b752ec8a93880e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 11d7cb0e..4361ab9b 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -1,6 +1,6 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { parentPort, workerData } from 'worker_threads'; +import { parentPort, workerData } from 'node:worker_threads'; import { ThreadWorker } from 'poolifier'; @@ -9,6 +9,27 @@ import type { ChargingStationWorkerData } from '../types'; import { Utils } from '../utils'; import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker'; +/** + * Create and start a charging station instance + * + * @param data - workerData + */ +const startChargingStation = (data: ChargingStationWorkerData): void => { + const station = new ChargingStation(data.index, data.templateFile); + station.start(); +}; + +/** + * Listen messages send by the main thread + */ +const addMessageListener = (): void => { + parentPort?.on('message', (message: WorkerMessage) => { + if (message.id === WorkerMessageEvents.startWorkerElement) { + startChargingStation(message.data); + } + }); +}; + // Conditionally export ThreadWorker instance for pool usage export let threadWorker: ThreadWorker; if (ChargingStationUtils.workerPoolInUse()) { @@ -23,24 +44,3 @@ if (ChargingStationUtils.workerPoolInUse()) { startChargingStation(workerData as ChargingStationWorkerData); } } - -/** - * Listen messages send by the main thread - */ -function addMessageListener(): void { - parentPort?.on('message', (message: WorkerMessage) => { - if (message.id === WorkerMessageEvents.START_WORKER_ELEMENT) { - startChargingStation(message.data); - } - }); -} - -/** - * Create and start a charging station instance - * - * @param data - workerData - */ -function startChargingStation(data: ChargingStationWorkerData): void { - const station = new ChargingStation(data.index, data.templateFile); - station.start(); -}