From 821c6c8229d6dfdc93ce917dfaad74e681e5a35c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 29 Aug 2021 18:57:37 +0200 Subject: [PATCH] Use IIFE (https://developer.mozilla.org/en-US/docs/Glossary/IIFE) for message sent to the main thread MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit It's a fire and forget semantic, so enforce it at the code level. Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 6 +++--- src/charging-station/ocpp/OCPPRequestService.ts | 9 +++++---- src/types/Worker.ts | 2 +- src/worker/WorkerSet.ts | 8 ++++++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 8f86f478..5b94ece8 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -43,9 +43,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } } else { // Throw exception - const errMsg = `${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload, null, 2)}`; - await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg), commandName); - throw new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg); + const error = new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload, null, 2)}`); + await this.chargingStation.ocppRequestService.sendError(messageId, error, commandName); + throw error; } // Send the built response await this.chargingStation.ocppRequestService.sendMessage(messageId, response, MessageType.CALL_RESULT_MESSAGE, commandName); diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index ca047457..5d4883ec 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -23,7 +23,8 @@ export default abstract class OCPPRequestService { this.ocppResponseService = ocppResponseService; } - public async sendMessage(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand): Promise { + public async sendMessage(messageId: string, commandParams: Record, messageType: MessageType, + commandName: RequestCommand | IncomingRequestCommand): Promise { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; // Send a message through wsConnection @@ -34,7 +35,7 @@ export default abstract class OCPPRequestService { // Request case MessageType.CALL_MESSAGE: // Build request - this.chargingStation.requests[messageId] = [responseCallback, rejectCallback, commandParams as Record]; + this.chargingStation.requests[messageId] = [responseCallback, rejectCallback, commandParams]; messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); break; // Response @@ -61,7 +62,7 @@ export default abstract class OCPPRequestService { // Buffer it this.chargingStation.addToMessageQueue(messageToSend); // Reject it - return rejectCallback(new OCPPError(commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams.details ? commandParams.details : {})); + return rejectCallback(new OCPPError(commandParams.code ? commandParams.code as ErrorType : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message as string : `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams.details ? commandParams.details : {})); } // Response? if (messageType === MessageType.CALL_RESULT_MESSAGE) { @@ -69,7 +70,7 @@ export default abstract class OCPPRequestService { resolve(); } else if (messageType === MessageType.CALL_ERROR_MESSAGE) { // Send timeout - setTimeout(() => rejectCallback(new OCPPError(commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams.details ? commandParams.details : {})), Constants.OCPP_ERROR_TIMEOUT); + setTimeout(() => rejectCallback(new OCPPError(commandParams.code ? commandParams.code as ErrorType : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message as string : `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams.details ? commandParams.details : {})), Constants.OCPP_ERROR_TIMEOUT); } /** diff --git a/src/types/Worker.ts b/src/types/Worker.ts index 0c795b2e..66f6d15a 100644 --- a/src/types/Worker.ts +++ b/src/types/Worker.ts @@ -13,7 +13,7 @@ export interface WorkerOptions { poolMinSize?: number; elementsPerWorker?: number; poolOptions?: PoolOptions; - messageHandler?: (message: any) => void | Promise; + messageHandler?: (message: unknown) => void | Promise; } // eslint-disable-next-line @typescript-eslint/no-empty-interface diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 6ab08b92..ae99da5b 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -9,7 +9,7 @@ import { WorkerUtils } from './WorkerUtils'; export default class WorkerSet extends WorkerAbstract { public readonly maxElementsPerWorker: number; - private readonly messageHandler: (message: any) => void | Promise; + private readonly messageHandler: (message: unknown) => void | Promise; private workerSet: Set; /** @@ -79,7 +79,11 @@ export default class WorkerSet extends WorkerAbstract { */ private startWorker(): void { const worker = new Worker(this.workerScript); - worker.on('message', this.messageHandler); + worker.on('message', (msg) => { + (async () => { + await this.messageHandler(msg); + })().catch(() => {}); + }); worker.on('error', () => { /* This is intentional */ }); worker.on('exit', (code) => { WorkerUtils.defaultExitHandler(code); -- 2.34.1