From: Jérôme Benoit Date: Tue, 30 Jan 2024 16:58:03 +0000 (+0100) Subject: perf: 'await' on UI request handlers only when necessary X-Git-Tag: v1.2.34~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4b9332afad3ebdd1c7b7e3e98dc1a9fcefebc04f;p=e-mobility-charging-stations-simulator.git perf: 'await' on UI request handlers only when necessary Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index d85430f4..c464f9ca 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -37,7 +37,13 @@ import { type StopTransactionRequest, type StopTransactionResponse } from '../../types/index.js' -import { Constants, convertToInt, isEmptyObject, logger } from '../../utils/index.js' +import { + Constants, + convertToInt, + isAsyncFunction, + isEmptyObject, + logger +} from '../../utils/index.js' import type { ChargingStation } from '../ChargingStation.js' import { getConfigurationKey } from '../ConfigurationKeyUtils.js' import { buildMeterValue } from '../ocpp/index.js' @@ -56,7 +62,7 @@ type CommandResponse = type CommandHandler = ( requestPayload?: BroadcastChannelRequestPayload // eslint-disable-next-line @typescript-eslint/no-invalid-void-type -) => Promise | void +) => Promise | CommandResponse | void export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly commandHandlers: Map @@ -334,7 +340,13 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne if (this.commandHandlers.has(command)) { this.cleanRequestPayload(command, requestPayload) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return await this.commandHandlers.get(command)!(requestPayload) + const commandHandler = this.commandHandlers.get(command)! + if (isAsyncFunction(commandHandler)) { + return await commandHandler(requestPayload) + } + return ( + commandHandler as (requestPayload?: BroadcastChannelRequestPayload) => CommandResponse + )(requestPayload) } throw new BaseError(`Unknown worker broadcast channel command: '${command}'`) } diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 44caca54..416d660d 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -12,7 +12,7 @@ import { type ResponsePayload, ResponseStatus } from '../../../types/index.js' -import { isNotEmptyArray, logger } from '../../../utils/index.js' +import { isAsyncFunction, isNotEmptyArray, logger } from '../../../utils/index.js' import { Bootstrap } from '../../Bootstrap.js' import { UIServiceWorkerBroadcastChannel } from '../../broadcast-channel/UIServiceWorkerBroadcastChannel.js' import type { AbstractUIServer } from '../AbstractUIServer.js' @@ -93,7 +93,18 @@ export abstract class AbstractUIService { // Call the request handler to build the response payload // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - responsePayload = await this.requestHandlers.get(command)!(messageId, command, requestPayload) + const requestHandler = this.requestHandlers.get(command)! + if (isAsyncFunction(requestHandler)) { + responsePayload = await requestHandler(messageId, command, requestPayload) + } else { + responsePayload = ( + requestHandler as ( + uuid?: string, + procedureName?: ProcedureName, + payload?: RequestPayload + ) => undefined | ResponsePayload + )(messageId, command, requestPayload) + } } catch (error) { // Log logger.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error)