1 import type { BroadcastChannelResponse
, MessageEvent
} from
'../types/WorkerBroadcastChannel';
2 import logger from
'../utils/Logger';
3 import type AbstractUIService from
'./ui-server/ui-services/AbstractUIService';
4 import WorkerBroadcastChannel from
'./WorkerBroadcastChannel';
6 const moduleName
= 'UIServiceWorkerBroadcastChannel';
8 export default class UIServiceWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
9 private uiService
: AbstractUIService
;
11 constructor(uiService
: AbstractUIService
) {
13 this.uiService
= uiService
;
14 this.onmessage
= this.responseHandler
.bind(this) as (message
: MessageEvent
) => void;
15 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: MessageEvent
) => void;
18 private responseHandler(messageEvent
: MessageEvent
): void {
19 if (this.isRequest(messageEvent
.data
)) {
22 this.validateMessageEvent(messageEvent
);
23 const [uuid
, responsePayload
] = messageEvent
.data
as BroadcastChannelResponse
;
24 // TODO: handle multiple responses for the same uuid
25 delete responsePayload
.hashId
;
27 this.uiService
.sendResponse(uuid
, responsePayload
);
30 private messageErrorHandler(messageEvent
: MessageEvent
): void {
32 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,
33 { messageEvent
, messageEventData
: messageEvent
.data
}