From be0a4d4d8bd085fcfb86db9ad025de3f270bc31d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 13 Jan 2024 23:45:22 +0100 Subject: [PATCH] fix: handle properly async performance storage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 14 +++++++++++++- src/utils/Utils.ts | 11 +++++++++++ src/utils/index.ts | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 488cf6a7..6ee60686 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -35,6 +35,7 @@ import { generateUUID, handleUncaughtException, handleUnhandledRejection, + isAsyncFunction, isNotEmptyArray, logPrefix, logger @@ -362,7 +363,18 @@ export class Bootstrap extends EventEmitter { } private readonly workerEventPerformanceStatistics = (data: Statistics): void => { - this.storage?.storePerformanceStatistics(data) as undefined + // eslint-disable-next-line @typescript-eslint/unbound-method + if (isAsyncFunction(this.storage?.storePerformanceStatistics)) { + ( + this.storage.storePerformanceStatistics as ( + performanceStatistics: Statistics + ) => Promise + )(data).catch(Constants.EMPTY_FUNCTION) + } else { + (this.storage?.storePerformanceStatistics as (performanceStatistics: Statistics) => void)( + data + ) + } } private initializeCounters (): void { diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 2472bf61..458fd930 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -261,6 +261,17 @@ export const clone = (object: T): T => { return deepClone(object as CloneableData) as T } +/** + * Detects whether the given value is an asynchronous function or not. + * + * @param fn - Unknown value. + * @returns `true` if `fn` was an asynchronous function, otherwise `false`. + * @internal + */ +export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise => { + return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' +} + export const isObject = (value: unknown): value is object => { return value != null && typeof value === 'object' && !Array.isArray(value) } diff --git a/src/utils/index.ts b/src/utils/index.ts index 0069a875..b2022255 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -24,6 +24,7 @@ export { buildStoppedMessage } from './MessageChannelUtils.js' export { + isAsyncFunction, JSONStringifyWithMapSupport, clone, convertToBoolean, -- 2.34.1