Refine TS and linter configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / WorkerBroadcastChannel.ts
CommitLineData
32de5a57
LM
1import { BroadcastChannel } from 'worker_threads';
2
852a4c5f 3import BaseError from '../exception/BaseError';
6c1761d4 4import type {
852a4c5f
JB
5 BroadcastChannelRequest,
6 BroadcastChannelResponse,
7 MessageEvent,
8} from '../types/WorkerBroadcastChannel';
4e3ff94d 9
6c8f5d90
JB
10export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
11 protected constructor() {
32de5a57
LM
12 super('worker');
13 }
4e3ff94d
JB
14
15 public sendRequest(request: BroadcastChannelRequest): void {
16 this.postMessage(request);
17 }
02a6943a 18
6c8f5d90 19 protected sendResponse(response: BroadcastChannelResponse): void {
02a6943a
JB
20 this.postMessage(response);
21 }
6c8f5d90
JB
22
23 protected isRequest(message: any): boolean {
24 return Array.isArray(message) && message.length === 3;
25 }
26
27 protected isResponse(message: any): boolean {
28 return Array.isArray(message) && message.length === 2;
29 }
852a4c5f
JB
30
31 protected validateMessageEvent(messageEvent: MessageEvent): void {
32 if (Array.isArray(messageEvent.data) === false) {
33 throw new BaseError('Worker broadcast channel protocol message event data is not an array');
34 }
35 }
32de5a57 36}