1 import { BroadcastChannel
} from
'worker_threads';
3 import BaseError from
'../exception/BaseError';
5 BroadcastChannelRequest
,
6 BroadcastChannelResponse
,
8 } from
'../types/WorkerBroadcastChannel';
10 export default abstract class WorkerBroadcastChannel
extends BroadcastChannel
{
11 protected constructor() {
15 public sendRequest(request
: BroadcastChannelRequest
): void {
16 this.postMessage(request
);
19 protected sendResponse(response
: BroadcastChannelResponse
): void {
20 this.postMessage(response
);
23 protected isRequest(message
: any): boolean {
24 return Array.isArray(message
) && message
.length
=== 3;
27 protected isResponse(message
: any): boolean {
28 return Array.isArray(message
) && message
.length
=== 2;
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');