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'
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>
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}'`)
}
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'
// 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)