X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fbroadcast-channel%2FWorkerBroadcastChannel.ts;h=91c92ac2b41ede0d234b184d127ec6329f1b9427;hb=c0bbb3eaf0c5dc704ea92820a2666a68ffdc27ff;hp=9102b10009a7114597a3091f344357c0a6abea12;hpb=5edd8ba0f8978cfb3ca9d80f299d9748c6c5970e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts index 9102b100..91c92ac2 100644 --- a/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts @@ -1,59 +1,59 @@ -import { BroadcastChannel } from 'node:worker_threads'; +import { BroadcastChannel } from 'node:worker_threads' import type { BroadcastChannelRequest, BroadcastChannelResponse, JsonType, - MessageEvent, -} from '../../types'; -import { logPrefix, logger, validateUUID } from '../../utils'; + MessageEvent +} from '../../types/index.js' +import { logger, logPrefix, validateUUID } from '../../utils/index.js' -const moduleName = 'WorkerBroadcastChannel'; +const moduleName = 'WorkerBroadcastChannel' export abstract class WorkerBroadcastChannel extends BroadcastChannel { - protected constructor() { - super('worker'); + protected constructor () { + super('worker') } - public sendRequest(request: BroadcastChannelRequest): void { - this.postMessage(request); + public sendRequest (request: BroadcastChannelRequest): void { + this.postMessage(request) } - protected sendResponse(response: BroadcastChannelResponse): void { - this.postMessage(response); + protected sendResponse (response: BroadcastChannelResponse): void { + this.postMessage(response) } - protected isRequest(message: JsonType[]): boolean { - return Array.isArray(message) === true && message.length === 3; + protected isRequest (message: JsonType[]): boolean { + return Array.isArray(message) && message.length === 3 } - protected isResponse(message: JsonType[]): boolean { - return Array.isArray(message) === true && message.length === 2; + protected isResponse (message: JsonType[]): boolean { + return Array.isArray(message) && message.length === 2 } - protected validateMessageEvent(messageEvent: MessageEvent): MessageEvent | false { - if (Array.isArray(messageEvent.data) === false) { + protected validateMessageEvent (messageEvent: MessageEvent): MessageEvent | false { + if (!Array.isArray(messageEvent.data)) { logger.error( `${this.logPrefix( moduleName, - 'validateMessageEvent', - )} Worker broadcast channel protocol message event data is not an array`, - ); - return false; + 'validateMessageEvent' + )} Worker broadcast channel protocol message event data is not an array` + ) + return false } - if (validateUUID(messageEvent.data[0]) === false) { + if (!validateUUID(messageEvent.data[0])) { logger.error( `${this.logPrefix( moduleName, - 'validateMessageEvent', - )} Worker broadcast channel protocol message event data UUID field is invalid`, - ); - return false; + 'validateMessageEvent' + )} Worker broadcast channel protocol message event data UUID field is invalid` + ) + return false } - return messageEvent; + return messageEvent } - private logPrefix = (modName: string, methodName: string): string => { - return logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`); - }; + private readonly logPrefix = (modName: string, methodName: string): string => { + return logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`) + } }