1 import { BroadcastChannel
} from
'worker_threads';
3 import type { JsonType
} from
'../types/JsonType';
5 BroadcastChannelRequest
,
6 BroadcastChannelResponse
,
8 } from
'../types/WorkerBroadcastChannel';
9 import logger from
'../utils/Logger';
10 import Utils from
'../utils/Utils';
12 const moduleName
= 'WorkerBroadcastChannel';
14 export default abstract class WorkerBroadcastChannel
extends BroadcastChannel
{
15 protected constructor() {
19 public sendRequest(request
: BroadcastChannelRequest
): void {
20 this.postMessage(request
);
23 protected sendResponse(response
: BroadcastChannelResponse
): void {
24 this.postMessage(response
);
27 protected isRequest(message
: JsonType
[]): boolean {
28 return Array.isArray(message
) === true && message
.length
=== 3;
31 protected isResponse(message
: JsonType
[]): boolean {
32 return Array.isArray(message
) === true && message
.length
=== 2;
35 protected validateMessageEvent(messageEvent
: MessageEvent
): MessageEvent
| false {
36 if (Array.isArray(messageEvent
.data
) === false) {
40 'validateMessageEvent'
41 )} Worker broadcast channel protocol message event data is not an array`
45 if (Utils
.validateUUID(messageEvent
.data
[0]) === false) {
49 'validateMessageEvent'
50 )} Worker broadcast channel protocol message event data UUID field is invalid`
57 private logPrefix
= (modName
: string, methodName
: string): string => {
58 return Utils
.logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`);