X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIServiceWorkerBroadcastChannel.ts;h=5f77b0e3450768bc79a87cacbfa744e77c3f268b;hb=34eeb1fb097b9eda12531ff9024d2f9c0e627a28;hp=7e22860aa1c28e2b1aadfb3547bd610cf2bcc9fc;hpb=5dea4c94d00775ef2ad383c4045a2181a5deb709;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index 7e22860a..5f77b0e3 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 { - BroadcastChannelResponse, - BroadcastChannelResponsePayload, - MessageEvent, -} from '../types/WorkerBroadcastChannel'; -import logger from '../utils/Logger'; -import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; -import WorkerBroadcastChannel from './WorkerBroadcastChannel'; +import { type AbstractUIService, WorkerBroadcastChannel } from './internal'; +import { + type BroadcastChannelResponse, + type BroadcastChannelResponsePayload, + type MessageEvent, + type ResponsePayload, + ResponseStatus, +} from '../types'; +import { Utils, logger } from '../utils'; const moduleName = 'UIServiceWorkerBroadcastChannel'; @@ -16,7 +16,7 @@ type Responses = { responses: BroadcastChannelResponsePayload[]; }; -export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { +export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly uiService: AbstractUIService; private readonly responses: Map; @@ -47,7 +47,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected ) { this.responses.get(uuid).responsesReceived++; - this.responses.get(uuid).responses.push(responsePayload); + this.responses.get(uuid)?.responses.push(responsePayload); } if ( this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected @@ -59,40 +59,41 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan } private buildResponsePayload(uuid: string): ResponsePayload { - const responsesStatus = this.responses - .get(uuid) - ?.responses.every(({ status }) => 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 .get(uuid) - ?.responses.map(({ status, hashId }) => { + ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId)) + .map(({ status, hashId }) => { if (status === ResponseStatus.SUCCESS) { return hashId; } - }) - .filter((hashId) => hashId !== undefined), + }), ...(responsesStatus === ResponseStatus.FAILURE && { hashIdsFailed: this.responses .get(uuid) - ?.responses.map(({ status, hashId }) => { + ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId)) + .map(({ status, hashId }) => { if (status === ResponseStatus.FAILURE) { return hashId; } - }) - .filter((hashId) => hashId !== undefined), + }), }), ...(responsesStatus === ResponseStatus.FAILURE && { responsesFailed: this.responses .get(uuid) - ?.responses.map((response) => { + ?.responses.filter((response) => !Utils.isNullOrUndefined(response)) + .map((response) => { if (response.status === ResponseStatus.FAILURE) { return response; } - }) - .filter((response) => response !== undefined), + }), }), }; } @@ -100,7 +101,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan private messageErrorHandler(messageEvent: MessageEvent): void { logger.error( `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, - { messageEvent } + messageEvent ); } }