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