README.md: fix UI protocol documentation
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIServiceWorkerBroadcastChannel.ts
index 5986a051a1b0480c6ed8d59386095f31ccd13738..7e4cc87720f81e10dacb625c00ed1474a821260d 100644 (file)
@@ -18,7 +18,7 @@ type Responses = {
 
 export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
   private readonly uiService: AbstractUIService;
-  private responses: Map<string, Responses>;
+  private readonly responses: Map<string, Responses>;
 
   constructor(uiService: AbstractUIService) {
     super();
@@ -29,11 +29,11 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
   }
 
   private responseHandler(messageEvent: MessageEvent): void {
-    if (this.isRequest(messageEvent.data)) {
+    if (this.isRequest(messageEvent.data) === true) {
       return;
     }
-    this.validateMessageEvent(messageEvent);
-    const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse;
+    const [uuid, responsePayload] = this.validateMessageEvent(messageEvent)
+      .data as BroadcastChannelResponse;
     if (this.responses.has(uuid) === false) {
       this.responses.set(uuid, {
         responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid),
@@ -41,16 +41,14 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
         responses: [responsePayload],
       });
     } else if (
-      this.responses.get(uuid)?.responsesReceived + 1 <
-      this.responses.get(uuid)?.responsesExpected
+      this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected
     ) {
       this.responses.get(uuid).responsesReceived++;
       this.responses.get(uuid).responses.push(responsePayload);
-    } else if (
-      this.responses.get(uuid)?.responsesReceived + 1 ===
-      this.responses.get(uuid)?.responsesExpected
+    }
+    if (
+      this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected
     ) {
-      this.responses.get(uuid).responses.push(responsePayload);
       this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid));
       this.responses.delete(uuid);
       this.uiService.deleteBroadcastChannelRequest(uuid);
@@ -83,13 +81,23 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
           })
           .filter((hashId) => hashId !== undefined),
       }),
+      ...(responsesStatus === ResponseStatus.FAILURE && {
+        responsesFailed: this.responses
+          .get(uuid)
+          ?.responses.map((response) => {
+            if (response.status === ResponseStatus.FAILURE) {
+              return response;
+            }
+          })
+          .filter((response) => response !== undefined),
+      }),
     };
   }
 
   private messageErrorHandler(messageEvent: MessageEvent): void {
     logger.error(
       `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
-      { messageEvent, messageEventData: messageEvent.data }
+      { messageEvent }
     );
   }
 }