X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fbroadcast-channel%2FUIServiceWorkerBroadcastChannel.ts;h=3a0c75b7d7e7ac19bce1cb255e320b7b4d35c5b0;hb=45a9839313c9bd8e81d61349bde0ea3bb69adf6a;hp=62588e0aa4f11925cf9d084eca1fbae108d72c6b;hpb=a6ef1ece74c0d08e86a905571f4f6045c28131cb;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts index 62588e0a..3a0c75b7 100644 --- a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts @@ -1,108 +1,113 @@ -import { WorkerBroadcastChannel } from './WorkerBroadcastChannel.js'; +import { WorkerBroadcastChannel } from './WorkerBroadcastChannel.js' import { type BroadcastChannelResponse, type BroadcastChannelResponsePayload, type MessageEvent, type ResponsePayload, - ResponseStatus, -} from '../../types/index.js'; -import { isNullOrUndefined, logger } from '../../utils/index.js'; -import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService.js'; + ResponseStatus +} from '../../types/index.js' +import { logger } from '../../utils/index.js' +import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService.js' -const moduleName = 'UIServiceWorkerBroadcastChannel'; +const moduleName = 'UIServiceWorkerBroadcastChannel' interface Responses { - responsesExpected: number; - responsesReceived: number; - responses: BroadcastChannelResponsePayload[]; + responsesExpected: number + responsesReceived: number + responses: BroadcastChannelResponsePayload[] } export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { - private readonly uiService: AbstractUIService; - private readonly responses: Map; + private readonly uiService: AbstractUIService + private readonly responses: Map - constructor(uiService: AbstractUIService) { - super(); - this.uiService = uiService; - this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void; - this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void; - this.responses = new Map(); + constructor (uiService: AbstractUIService) { + super() + this.uiService = uiService + this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void + this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void + this.responses = new Map() } - private responseHandler(messageEvent: MessageEvent): void { - const validatedMessageEvent = this.validateMessageEvent(messageEvent); + private responseHandler (messageEvent: MessageEvent): void { + const validatedMessageEvent = this.validateMessageEvent(messageEvent) if (validatedMessageEvent === false) { - return; + return } - if (this.isRequest(validatedMessageEvent.data) === true) { - return; + if (this.isRequest(validatedMessageEvent.data)) { + return } - const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse; - if (this.responses.has(uuid) === false) { + const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse + if (!this.responses.has(uuid)) { this.responses.set(uuid, { responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid), responsesReceived: 1, - responses: [responsePayload], - }); + responses: [responsePayload] + }) } else if ( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.responses.get(uuid)!.responsesReceived <= this.responses.get(uuid)!.responsesExpected ) { - ++this.responses.get(uuid)!.responsesReceived; - this.responses.get(uuid)?.responses.push(responsePayload); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ++this.responses.get(uuid)!.responsesReceived + this.responses.get(uuid)?.responses.push(responsePayload) } if ( this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected ) { - this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)); - this.responses.delete(uuid); - this.uiService.deleteBroadcastChannelRequest(uuid); + this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)) + this.responses.delete(uuid) + this.uiService.deleteBroadcastChannelRequest(uuid) } } - private buildResponsePayload(uuid: string): ResponsePayload { + private buildResponsePayload (uuid: string): ResponsePayload { const responsesStatus = this.responses .get(uuid) ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true ? ResponseStatus.SUCCESS - : ResponseStatus.FAILURE; + : ResponseStatus.FAILURE return { status: responsesStatus, hashIdsSucceeded: this.responses .get(uuid) ?.responses.map(({ status, hashId }) => { - if (hashId !== undefined && status === ResponseStatus.SUCCESS) { - return hashId; + if (hashId != null && status === ResponseStatus.SUCCESS) { + return hashId } + return undefined }) - .filter((hashId) => !isNullOrUndefined(hashId)) as string[], + .filter(hashId => hashId != null) as string[], ...(responsesStatus === ResponseStatus.FAILURE && { hashIdsFailed: this.responses .get(uuid) ?.responses.map(({ status, hashId }) => { - if (hashId !== undefined && status === ResponseStatus.FAILURE) { - return hashId; + if (hashId != null && status === ResponseStatus.FAILURE) { + return hashId } + return undefined }) - .filter((hashId) => !isNullOrUndefined(hashId)) as string[], + .filter(hashId => hashId != null) as string[] }), ...(responsesStatus === ResponseStatus.FAILURE && { responsesFailed: this.responses .get(uuid) - ?.responses.map((response) => { - if (response !== undefined && response.status === ResponseStatus.FAILURE) { - return response; + ?.responses.map(response => { + if (response.status === ResponseStatus.FAILURE) { + return response } + return undefined }) - .filter((response) => !isNullOrUndefined(response)) as BroadcastChannelResponsePayload[], - }), - }; + .filter(response => response != null) as BroadcastChannelResponsePayload[] + }) + } } - private messageErrorHandler(messageEvent: MessageEvent): void { + private messageErrorHandler (messageEvent: MessageEvent): void { logger.error( `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, - messageEvent, - ); + messageEvent + ) } }