d9e746393d32b03f6062787a8924d819d864bf1d
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIServiceWorkerBroadcastChannel.ts
1 import { type AbstractUIService, WorkerBroadcastChannel } from './internal';
2 import {
3 type BroadcastChannelResponse,
4 type BroadcastChannelResponsePayload,
5 type MessageEvent,
6 type ResponsePayload,
7 ResponseStatus,
8 } from '../types';
9 import { logger } from '../utils/Logger';
10
11 const moduleName = 'UIServiceWorkerBroadcastChannel';
12
13 type Responses = {
14 responsesExpected: number;
15 responsesReceived: number;
16 responses: BroadcastChannelResponsePayload[];
17 };
18
19 export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
20 private readonly uiService: AbstractUIService;
21 private readonly responses: Map<string, Responses>;
22
23 constructor(uiService: AbstractUIService) {
24 super();
25 this.uiService = uiService;
26 this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void;
27 this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void;
28 this.responses = new Map<string, Responses>();
29 }
30
31 private responseHandler(messageEvent: MessageEvent): void {
32 const validatedMessageEvent = this.validateMessageEvent(messageEvent);
33 if (validatedMessageEvent === false) {
34 return;
35 }
36 if (this.isRequest(validatedMessageEvent.data) === true) {
37 return;
38 }
39 const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse;
40 if (this.responses.has(uuid) === false) {
41 this.responses.set(uuid, {
42 responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid),
43 responsesReceived: 1,
44 responses: [responsePayload],
45 });
46 } else if (
47 this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected
48 ) {
49 this.responses.get(uuid).responsesReceived++;
50 this.responses.get(uuid)?.responses.push(responsePayload);
51 }
52 if (
53 this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected
54 ) {
55 this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid));
56 this.responses.delete(uuid);
57 this.uiService.deleteBroadcastChannelRequest(uuid);
58 }
59 }
60
61 private buildResponsePayload(uuid: string): ResponsePayload {
62 const responsesStatus =
63 this.responses
64 .get(uuid)
65 ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true
66 ? ResponseStatus.SUCCESS
67 : ResponseStatus.FAILURE;
68 return {
69 status: responsesStatus,
70 hashIdsSucceeded: this.responses
71 .get(uuid)
72 ?.responses.filter(({ hashId }) => hashId !== undefined)
73 .map(({ status, hashId }) => {
74 if (status === ResponseStatus.SUCCESS) {
75 return hashId;
76 }
77 }),
78 ...(responsesStatus === ResponseStatus.FAILURE && {
79 hashIdsFailed: this.responses
80 .get(uuid)
81 ?.responses.filter(({ hashId }) => hashId !== undefined)
82 .map(({ status, hashId }) => {
83 if (status === ResponseStatus.FAILURE) {
84 return hashId;
85 }
86 }),
87 }),
88 ...(responsesStatus === ResponseStatus.FAILURE && {
89 responsesFailed: this.responses
90 .get(uuid)
91 ?.responses.filter((response) => response !== undefined)
92 .map((response) => {
93 if (response.status === ResponseStatus.FAILURE) {
94 return response;
95 }
96 }),
97 }),
98 };
99 }
100
101 private messageErrorHandler(messageEvent: MessageEvent): void {
102 logger.error(
103 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
104 messageEvent
105 );
106 }
107 }