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