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) {
super(moduleName);
// Add message listener to create and start charging station from the main thread
parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
- 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,
- },
- });
- }
}
});
}