From e8a92d57004dcddba34fa4a510226b56e11a8cdb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 10 Apr 2023 23:37:47 +0200 Subject: [PATCH] perf: use arrow functions in hot code paths MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationWorker.ts | 42 +++---- .../ocpp/OCPPRequestService.ts | 112 +++++++++--------- 2 files changed, 77 insertions(+), 77 deletions(-) diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index c720b219..4361ab9b 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -9,6 +9,27 @@ import type { ChargingStationWorkerData } from '../types'; import { Utils } from '../utils'; import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker'; +/** + * Create and start a charging station instance + * + * @param data - workerData + */ +const startChargingStation = (data: ChargingStationWorkerData): void => { + const station = new ChargingStation(data.index, data.templateFile); + station.start(); +}; + +/** + * Listen messages send by the main thread + */ +const addMessageListener = (): void => { + parentPort?.on('message', (message: WorkerMessage) => { + if (message.id === WorkerMessageEvents.startWorkerElement) { + startChargingStation(message.data); + } + }); +}; + // Conditionally export ThreadWorker instance for pool usage export let threadWorker: ThreadWorker; if (ChargingStationUtils.workerPoolInUse()) { @@ -23,24 +44,3 @@ if (ChargingStationUtils.workerPoolInUse()) { startChargingStation(workerData as ChargingStationWorkerData); } } - -/** - * Listen messages send by the main thread - */ -function addMessageListener(): void { - parentPort?.on('message', (message: WorkerMessage) => { - if (message.id === WorkerMessageEvents.startWorkerElement) { - startChargingStation(message.data); - } - }); -} - -/** - * Create and start a charging station instance - * - * @param data - workerData - */ -function startChargingStation(data: ChargingStationWorkerData): void { - const station = new ChargingStation(data.index, data.templateFile); - station.start(); -} diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 4115eff1..e00e954c 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -230,6 +230,62 @@ export abstract class OCPPRequestService { // Send a message through wsConnection return Utils.promiseWithTimeout( new Promise((resolve, reject) => { + /** + * Function that will receive the request's response + * + * @param payload - + * @param requestPayload - + */ + const responseCallback = (payload: JsonType, requestPayload: JsonType): void => { + if (chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics?.addRequestStatistic( + commandName, + MessageType.CALL_RESULT_MESSAGE + ); + } + // Handle the request's response + self.ocppResponseService + .responseHandler( + chargingStation, + commandName as RequestCommand, + payload, + requestPayload + ) + .then(() => { + resolve(payload); + }) + .catch((error) => { + reject(error); + }) + .finally(() => { + chargingStation.requests.delete(messageId); + }); + }; + + /** + * Function that will receive the request's error response + * + * @param error - + * @param requestStatistic - + */ + const errorCallback = (error: OCPPError, requestStatistic = true): void => { + if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics?.addRequestStatistic( + commandName, + MessageType.CALL_ERROR_MESSAGE + ); + } + logger.error( + `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString( + messageType + )} command ${commandName} with PDU %j:`, + messagePayload, + error + ); + chargingStation.requests.delete(messageId); + reject(error); + }; + if (chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType); } @@ -296,62 +352,6 @@ export abstract class OCPPRequestService { if (messageType !== MessageType.CALL_MESSAGE) { return resolve(messagePayload); } - - /** - * Function that will receive the request's response - * - * @param payload - - * @param requestPayload - - */ - function responseCallback(payload: JsonType, requestPayload: JsonType): void { - if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics?.addRequestStatistic( - commandName, - MessageType.CALL_RESULT_MESSAGE - ); - } - // Handle the request's response - self.ocppResponseService - .responseHandler( - chargingStation, - commandName as RequestCommand, - payload, - requestPayload - ) - .then(() => { - resolve(payload); - }) - .catch((error) => { - reject(error); - }) - .finally(() => { - chargingStation.requests.delete(messageId); - }); - } - - /** - * Function that will receive the request's error response - * - * @param error - - * @param requestStatistic - - */ - function errorCallback(error: OCPPError, requestStatistic = true): void { - if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics?.addRequestStatistic( - commandName, - MessageType.CALL_ERROR_MESSAGE - ); - } - logger.error( - `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString( - messageType - )} command ${commandName} with PDU %j:`, - messagePayload, - error - ); - chargingStation.requests.delete(messageId); - reject(error); - } }), Constants.OCPP_WEBSOCKET_TIMEOUT, new OCPPError( -- 2.34.1