-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';
responses: BroadcastChannelResponsePayload[];
};
-export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
+export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
private readonly uiService: AbstractUIService;
private readonly responses: Map<string, Responses>;
}
private responseHandler(messageEvent: MessageEvent): void {
- if (this.isRequest(messageEvent.data) === true) {
+ 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),
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
}
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),
+ }),
}),
};
}
private messageErrorHandler(messageEvent: MessageEvent): void {
logger.error(
`${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
- { messageEvent }
+ messageEvent
);
}
}