msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
): void {
// logger.debug(
- // `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
+ // `${this.logPrefix()} ${moduleName}.messageHandler: Charging station worker message received: ${JSON.stringify(
// msg,
// undefined,
// 2
break
default:
throw new BaseError(
- `Unknown charging station worker event: '${event}' received with data: ${JSON.stringify(data, undefined, 2)}`
+ `Unknown charging station worker message event: '${event}' received with data: ${JSON.stringify(data, undefined, 2)}`
)
}
} catch (error) {
logger.error(
- `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${event}' event:`,
+ `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling charging station worker message event '${event}':`,
error
)
}
constructor () {
parentPort?.on('message', (message: WorkerMessage<Data>) => {
const { uuid, event, data } = message
- switch (event) {
- case WorkerMessageEvents.addWorkerElement:
- try {
- const chargingStation = new ChargingStation(
- data.index,
- data.templateFile,
- data.options
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ if (uuid != null) {
+ switch (event) {
+ case WorkerMessageEvents.addWorkerElement:
+ try {
+ const chargingStation = new ChargingStation(
+ data.index,
+ data.templateFile,
+ data.options
+ )
+ parentPort?.postMessage({
+ uuid,
+ event: WorkerMessageEvents.addedWorkerElement,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ data: chargingStation.stationInfo!
+ } satisfies WorkerMessage<ChargingStationInfo>)
+ } catch (error) {
+ parentPort?.postMessage({
+ uuid,
+ event: WorkerMessageEvents.workerElementError,
+ data: {
+ event,
+ name: (error as Error).name,
+ message: (error as Error).message,
+ stack: (error as Error).stack
+ }
+ } satisfies WorkerMessage<WorkerDataError>)
+ }
+ break
+ default:
+ throw new BaseError(
+ `Unknown worker message event: '${event}' received with data: '${JSON.stringify(
+ data,
+ undefined,
+ 2
+ )}'`
)
- parentPort?.postMessage({
- uuid,
- event: WorkerMessageEvents.addedWorkerElement,
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- data: chargingStation.stationInfo!
- } satisfies WorkerMessage<ChargingStationInfo>)
- } catch (error) {
- parentPort?.postMessage({
- uuid,
- event: WorkerMessageEvents.workerElementError,
- data: {
- event,
- name: (error as Error).name,
- message: (error as Error).message,
- stack: (error as Error).stack
- }
- } satisfies WorkerMessage<WorkerDataError>)
- }
- break
- default:
- throw new BaseError(
- `Unknown worker event: '${event}' received with data: '${JSON.stringify(
- data,
- undefined,
- 2
- )}'`
- )
+ }
}
})
}
if (this.promiseResponseMap.has(uuid)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { resolve, reject, workerSetElement } = this.promiseResponseMap.get(uuid)!
- if (event === WorkerMessageEvents.addedWorkerElement) {
- this.emitter?.emit(WorkerSetEvents.elementAdded, this.info)
- ++workerSetElement.numberOfWorkerElements
- resolve(data)
- } else if (event === WorkerMessageEvents.workerElementError) {
- this.emitter?.emit(WorkerSetEvents.elementError, data)
- reject(data)
+ switch (event) {
+ case WorkerMessageEvents.addedWorkerElement:
+ this.emitter?.emit(WorkerSetEvents.elementAdded, this.info)
+ ++workerSetElement.numberOfWorkerElements
+ resolve(data)
+ break
+ case WorkerMessageEvents.workerElementError:
+ this.emitter?.emit(WorkerSetEvents.elementError, data)
+ reject(data)
+ break
+ default:
+ reject(
+ new Error(
+ `Unknown worker message event: '${event}' received with data: '${JSON.stringify(
+ data,
+ undefined,
+ 2
+ )}'`
+ )
+ )
}
this.promiseResponseMap.delete(uuid)
}