X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FWorkerBroadcastChannel.ts;h=b19bf946dc633db77e5c690e75dd4b921de6868a;hb=853ed24a1ca05b0de603402228dd99e7c41fa397;hp=12a877c12add623365c0c8c6c6068c62841000d2;hpb=6c8f5d901f6dbfd66e921decde63bf73548c156e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/WorkerBroadcastChannel.ts index 12a877c1..b19bf946 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/WorkerBroadcastChannel.ts @@ -1,6 +1,12 @@ import { BroadcastChannel } from 'worker_threads'; -import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel'; +import BaseError from '../exception/BaseError'; +import type { JsonType } from '../types/JsonType'; +import type { + BroadcastChannelRequest, + BroadcastChannelResponse, + MessageEvent, +} from '../types/WorkerBroadcastChannel'; export default abstract class WorkerBroadcastChannel extends BroadcastChannel { protected constructor() { @@ -15,11 +21,17 @@ export default abstract class WorkerBroadcastChannel extends BroadcastChannel { this.postMessage(response); } - protected isRequest(message: any): boolean { + protected isRequest(message: JsonType[]): boolean { return Array.isArray(message) && message.length === 3; } - protected isResponse(message: any): boolean { + protected isResponse(message: JsonType[]): 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'); + } + } }