perf: 'await' on UI request handlers only when necessary
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 Jan 2024 16:58:03 +0000 (17:58 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 Jan 2024 16:58:03 +0000 (17:58 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts

index d85430f4c16fa6558e63077635b7635b14f32a09..c464f9ca11d33222925f2ee0bf52c19fa37f8948 100644 (file)
@@ -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<CommandResponse | void> | void
+) => Promise<CommandResponse | void> | CommandResponse | void
 
 export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
   private readonly commandHandlers: Map<BroadcastChannelProcedureName, CommandHandler>
@@ -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}'`)
   }
index 44caca5472daca4084a8e5177a47d225ac304901..416d660d7d5f4c6ead5a5f003c528f13d69b0c9e 100644 (file)
@@ -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)