Commit | Line | Data |
---|---|---|
6c1761d4 | 1 | import type { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; |
6c8f5d90 | 2 | import logger from '../utils/Logger'; |
db2336d9 | 3 | import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; |
6c8f5d90 JB |
4 | import WorkerBroadcastChannel from './WorkerBroadcastChannel'; |
5 | ||
6 | const moduleName = 'UIServiceWorkerBroadcastChannel'; | |
7 | ||
8 | export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { | |
9 | private uiService: AbstractUIService; | |
10 | ||
11 | constructor(uiService: AbstractUIService) { | |
12 | super(); | |
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; | |
16 | } | |
17 | ||
18 | private responseHandler(messageEvent: MessageEvent): void { | |
19 | if (this.isRequest(messageEvent.data)) { | |
20 | return; | |
21 | } | |
852a4c5f | 22 | this.validateMessageEvent(messageEvent); |
6c8f5d90 | 23 | const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; |
10d244c0 JB |
24 | // TODO: handle multiple responses for the same uuid |
25 | delete responsePayload.hashId; | |
6c8f5d90 JB |
26 | |
27 | this.uiService.sendResponse(uuid, responsePayload); | |
28 | } | |
29 | ||
30 | private messageErrorHandler(messageEvent: MessageEvent): void { | |
31 | logger.error( | |
32 | `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, | |
33 | { messageEvent, messageEventData: messageEvent.data } | |
34 | ); | |
35 | } | |
36 | } |