Bump moment-timezone from 0.5.34 to 0.5.37 (#160)
[e-mobility-charging-stations-simulator.git] / src / charging-station / WorkerBroadcastChannel.ts
index 2099d695ab683e75d315151e02c9c5914eb6cedd..dba83cbb01fccdd367143906d53f8fd7a4d280bc 100644 (file)
@@ -1,9 +1,14 @@
 import { BroadcastChannel } from 'worker_threads';
 
-import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel';
+import BaseError from '../exception/BaseError';
+import type {
+  BroadcastChannelRequest,
+  BroadcastChannelResponse,
+  MessageEvent,
+} from '../types/WorkerBroadcastChannel';
 
-export default class WorkerBroadcastChannel extends BroadcastChannel {
-  constructor() {
+export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
+  protected constructor() {
     super('worker');
   }
 
@@ -11,7 +16,21 @@ export default class WorkerBroadcastChannel extends BroadcastChannel {
     this.postMessage(request);
   }
 
-  public sendResponse(response: BroadcastChannelResponse): void {
+  protected sendResponse(response: BroadcastChannelResponse): void {
     this.postMessage(response);
   }
+
+  protected isRequest(message: any): boolean {
+    return Array.isArray(message) && message.length === 3;
+  }
+
+  protected isResponse(message: any): boolean {
+    return Array.isArray(message) && message.length === 2;
+  }
+
+  protected validateMessageEvent(messageEvent: MessageEvent): void {
+    if (Array.isArray(messageEvent.data) === false) {
+      throw new BaseError('Worker broadcast channel protocol message event data is not an array');
+    }
+  }
 }