From: Jérôme Benoit Date: Thu, 20 Jul 2023 19:30:02 +0000 (+0200) Subject: feat: strict message event checking in worker set mode X-Git-Tag: v1.2.20~178 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f93dda6ae88071623a20bd4171026222cf66c631;p=e-mobility-charging-stations-simulator.git feat: strict message event checking in worker set mode Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 4bfea321..79d9a008 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -292,7 +292,9 @@ export class Bootstrap extends EventEmitter { break; default: throw new BaseError( - `Unknown event type: '${msg.event}' for data: ${JSON.stringify(msg.data, null, 2)}`, + `Unknown charging station worker event: '${ + msg.event + }' received with data: ${JSON.stringify(msg.data, null, 2)}`, ); } } catch (error) { diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index b20bf3e8..db7ae2cb 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -26,25 +26,35 @@ class ChargingStationWorker extends AsyncResource { super(moduleName); // Add message listener to create and start charging station from the main thread parentPort?.on('message', (message: WorkerMessage) => { - if (message.event === WorkerMessageEvents.startWorkerElement) { - try { - this.runInAsyncScope( - startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, - this, - message.data, + switch (message.event) { + case WorkerMessageEvents.startWorkerElement: + try { + this.runInAsyncScope( + startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, + this, + message.data, + ); + parentPort?.postMessage({ + event: WorkerMessageEvents.startedWorkerElement, + }); + } catch (error) { + parentPort?.postMessage({ + event: WorkerMessageEvents.startWorkerElementError, + data: { + message: (error as Error).message, + stack: (error as Error).stack, + }, + }); + } + break; + default: + throw new Error( + `Unknown worker event: '${message.event}' received with data: '${JSON.stringify( + message.data, + null, + 2, + )}'`, ); - parentPort?.postMessage({ - event: WorkerMessageEvents.startedWorkerElement, - }); - } catch (error) { - parentPort?.postMessage({ - event: WorkerMessageEvents.startWorkerElementError, - data: { - message: (error as Error).message, - stack: (error as Error).stack, - }, - }); - } } }); }