1 import { BroadcastChannel
} from
'node:worker_threads';
4 BroadcastChannelRequest
,
5 BroadcastChannelResponse
,
9 import { Utils
, logger
} from
'../utils';
11 const moduleName
= 'WorkerBroadcastChannel';
13 export abstract class WorkerBroadcastChannel
extends BroadcastChannel
{
14 protected constructor() {
18 public sendRequest(request
: BroadcastChannelRequest
): void {
19 this.postMessage(request
);
22 protected sendResponse(response
: BroadcastChannelResponse
): void {
23 this.postMessage(response
);
26 protected isRequest(message
: JsonType
[]): boolean {
27 return Array.isArray(message
) === true && message
.length
=== 3;
30 protected isResponse(message
: JsonType
[]): boolean {
31 return Array.isArray(message
) === true && message
.length
=== 2;
34 protected validateMessageEvent(messageEvent
: MessageEvent
): MessageEvent
| false {
35 if (Array.isArray(messageEvent
.data
) === false) {
39 'validateMessageEvent'
40 )} Worker broadcast channel protocol message event data is not an array`
44 if (Utils
.validateUUID(messageEvent
.data
[0]) === false) {
48 'validateMessageEvent'
49 )} Worker broadcast channel protocol message event data UUID field is invalid`
56 private logPrefix
= (modName
: string, methodName
: string): string => {
57 return Utils
.logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`);