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