Commit | Line | Data |
---|---|---|
53e5fd67 | 1 | import BaseError from '../exception/BaseError'; |
6c8f5d90 JB |
2 | import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; |
3 | import logger from '../utils/Logger'; | |
db2336d9 | 4 | import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; |
6c8f5d90 JB |
5 | import WorkerBroadcastChannel from './WorkerBroadcastChannel'; |
6 | ||
7 | const moduleName = 'UIServiceWorkerBroadcastChannel'; | |
8 | ||
9 | export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { | |
10 | private uiService: AbstractUIService; | |
11 | ||
12 | constructor(uiService: AbstractUIService) { | |
13 | super(); | |
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; | |
17 | } | |
18 | ||
19 | private responseHandler(messageEvent: MessageEvent): void { | |
20 | if (this.isRequest(messageEvent.data)) { | |
21 | return; | |
22 | } | |
53e5fd67 JB |
23 | if (Array.isArray(messageEvent.data) === false) { |
24 | throw new BaseError('Worker broadcast channel protocol response is not an array'); | |
25 | } | |
6c8f5d90 JB |
26 | const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; |
27 | ||
28 | this.uiService.sendResponse(uuid, responsePayload); | |
29 | } | |
30 | ||
31 | private messageErrorHandler(messageEvent: MessageEvent): void { | |
32 | logger.error( | |
33 | `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, | |
34 | { messageEvent, messageEventData: messageEvent.data } | |
35 | ); | |
36 | } | |
37 | } |