From: Jérôme Benoit Date: Mon, 11 Dec 2023 12:49:21 +0000 (+0100) Subject: perf: a charging station worker is not an AsyncResource X-Git-Tag: v1.2.30~27 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ca0da0da5f4dc739572c835d6e53e40de3a42382;p=e-mobility-charging-stations-simulator.git perf: a charging station worker is not an AsyncResource Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 955ce933..d9e462d0 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -1,6 +1,5 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { AsyncResource } from 'node:async_hooks'; import { parentPort } from 'node:worker_threads'; import { ThreadWorker } from 'poolifier'; @@ -9,9 +8,7 @@ import { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; import type { ChargingStationWorkerData } from '../types'; import { Configuration } from '../utils'; -import { type WorkerData, type WorkerMessage, WorkerMessageEvents } from '../worker'; - -const moduleName = 'ChargingStationWorker'; +import { type WorkerMessage, WorkerMessageEvents } from '../worker'; /** * Creates and starts a charging station instance @@ -22,19 +19,14 @@ const startChargingStation = (data?: ChargingStationWorkerData): void => { new ChargingStation(data!.index, data!.templateFile).start(); }; -class ChargingStationWorker extends AsyncResource { +class ChargingStationWorker { constructor() { - super(moduleName); // Add message listener to create and start charging station from the main thread parentPort?.on('message', (message: WorkerMessage) => { switch (message.event) { case WorkerMessageEvents.startWorkerElement: try { - this.runInAsyncScope( - startChargingStation.bind(this) as (data?: Data) => void, - this, - message.data, - ); + startChargingStation(message.data); parentPort?.postMessage({ event: WorkerMessageEvents.startedWorkerElement, });