Commit | Line | Data |
---|---|---|
6c8f5d90 JB |
1 | import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; |
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 | } | |
22 | const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; | |
23 | ||
24 | this.uiService.sendResponse(uuid, responsePayload); | |
25 | } | |
26 | ||
27 | private messageErrorHandler(messageEvent: MessageEvent): void { | |
28 | logger.error( | |
29 | `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, | |
30 | { messageEvent, messageEventData: messageEvent.data } | |
31 | ); | |
32 | } | |
33 | } |