X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIServiceWorkerBroadcastChannel.ts;h=cdbe108df333e9f6b6388f91e5327be60a8a3821;hb=4d20f040ab91f06c9399dbaf084358183cf75314;hp=a2b209220ae084ba84e1fa7accda1e782443929b;hpb=5e3cb7281de2b6fa8b61a453f964c2f213fefa80;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index a2b20922..cdbe108d 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -1,12 +1,12 @@ -import { ResponsePayload, ResponseStatus } from '../types/UIProtocol'; +import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; +import WorkerBroadcastChannel from './WorkerBroadcastChannel'; +import { type ResponsePayload, ResponseStatus } from '../types/UIProtocol'; import type { BroadcastChannelResponse, BroadcastChannelResponsePayload, MessageEvent, } from '../types/WorkerBroadcastChannel'; import logger from '../utils/Logger'; -import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; -import WorkerBroadcastChannel from './WorkerBroadcastChannel'; const moduleName = 'UIServiceWorkerBroadcastChannel'; @@ -29,11 +29,14 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan } private responseHandler(messageEvent: MessageEvent): void { - if (this.isRequest(messageEvent.data)) { + const validatedMessageEvent = this.validateMessageEvent(messageEvent); + if (validatedMessageEvent === false) { return; } - const [uuid, responsePayload] = this.validateMessageEvent(messageEvent) - .data as BroadcastChannelResponse; + if (this.isRequest(validatedMessageEvent.data) === true) { + return; + } + const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse; if (this.responses.has(uuid) === false) { this.responses.set(uuid, { responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid), @@ -41,16 +44,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); @@ -58,11 +59,12 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan } private buildResponsePayload(uuid: string): ResponsePayload { - const responsesStatus = this.responses - .get(uuid) - ?.responses.every((response) => response.status === ResponseStatus.SUCCESS) - ? ResponseStatus.SUCCESS - : ResponseStatus.FAILURE; + const responsesStatus = + this.responses + .get(uuid) + ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true + ? ResponseStatus.SUCCESS + : ResponseStatus.FAILURE; return { status: responsesStatus, hashIdsSucceeded: this.responses @@ -83,13 +85,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 } + messageEvent ); } }