Align 'started' attribute usage to all classes
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIServiceWorkerBroadcastChannel.ts
CommitLineData
b3b45e6c 1import type { ResponsePayload } from '../types/UIProtocol';
6c1761d4 2import type { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel';
6c8f5d90 3import logger from '../utils/Logger';
db2336d9 4import type AbstractUIService from './ui-server/ui-services/AbstractUIService';
6c8f5d90
JB
5import WorkerBroadcastChannel from './WorkerBroadcastChannel';
6
7const moduleName = 'UIServiceWorkerBroadcastChannel';
8
9export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
10 private uiService: AbstractUIService;
11
12 constructor(uiService: AbstractUIService) {
13 super();
14 this.uiService = uiService;
15 this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void;
16 this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void;
17 }
18
19 private responseHandler(messageEvent: MessageEvent): void {
20 if (this.isRequest(messageEvent.data)) {
21 return;
22 }
852a4c5f 23 this.validateMessageEvent(messageEvent);
6c8f5d90 24 const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse;
10d244c0
JB
25 // TODO: handle multiple responses for the same uuid
26 delete responsePayload.hashId;
6c8f5d90 27
b3b45e6c 28 this.uiService.sendResponse(uuid, responsePayload as ResponsePayload);
6c8f5d90
JB
29 }
30
31 private messageErrorHandler(messageEvent: MessageEvent): void {
32 logger.error(
33 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
34 { messageEvent, messageEventData: messageEvent.data }
35 );
36 }
37}