type ResponsePayload,
ResponseStatus,
} from '../../types';
-import { Utils, logger } from '../../utils';
+import { isNullOrUndefined, logger } from '../../utils';
import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService';
const moduleName = 'UIServiceWorkerBroadcastChannel';
-type Responses = {
+interface Responses {
responsesExpected: number;
responsesReceived: number;
responses: BroadcastChannelResponsePayload[];
-};
+}
export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
private readonly uiService: AbstractUIService;
constructor(uiService: AbstractUIService) {
super();
this.uiService = uiService;
- this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void;
- this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void;
+ this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void;
+ this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void;
this.responses = new Map<string, Responses>();
}
responses: [responsePayload],
});
} else if (
- this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected
+ this.responses.get(uuid)!.responsesReceived <= this.responses.get(uuid)!.responsesExpected
) {
- this.responses.get(uuid).responsesReceived++;
+ ++this.responses.get(uuid)!.responsesReceived;
this.responses.get(uuid)?.responses.push(responsePayload);
}
if (
status: responsesStatus,
hashIdsSucceeded: this.responses
.get(uuid)
- ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId))
+ ?.responses.filter(({ hashId }) => !isNullOrUndefined(hashId))
.map(({ status, hashId }) => {
if (status === ResponseStatus.SUCCESS) {
return hashId;
}
- }),
+ }) as string[],
...(responsesStatus === ResponseStatus.FAILURE && {
hashIdsFailed: this.responses
.get(uuid)
- ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId))
+ ?.responses.filter(({ hashId }) => !isNullOrUndefined(hashId))
.map(({ status, hashId }) => {
if (status === ResponseStatus.FAILURE) {
return hashId;
}
- }),
+ }) as string[],
}),
...(responsesStatus === ResponseStatus.FAILURE && {
responsesFailed: this.responses
.get(uuid)
- ?.responses.filter((response) => !Utils.isNullOrUndefined(response))
+ ?.responses.filter((response) => !isNullOrUndefined(response))
.map((response) => {
if (response.status === ResponseStatus.FAILURE) {
return response;
}
- }),
+ }) as BroadcastChannelResponsePayload[],
}),
};
}
private messageErrorHandler(messageEvent: MessageEvent): void {
logger.error(
`${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
- messageEvent
+ messageEvent,
);
}
}