From: Jérôme Benoit Date: Sat, 3 Aug 2024 12:53:24 +0000 (+0200) Subject: test: add AsyncLock test X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b842c65bd62d35d7b19f5d45371dfe0c6539e6d3;p=e-mobility-charging-stations-simulator.git test: add AsyncLock test Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index f6b793a6..f69faac8 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -2,7 +2,7 @@ import { Queue } from 'mnemonist' -import { Constants } from './Constants.js' +import { isAsyncFunction } from './Utils.js' export enum AsyncLockType { configuration = 'configuration', @@ -22,11 +22,16 @@ export class AsyncLock { } public static async runExclusive(type: AsyncLockType, fn: () => T | Promise): Promise { - return await AsyncLock.acquire(type) - .then(fn) - .finally(() => { - AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION) - }) + try { + await AsyncLock.acquire(type) + if (isAsyncFunction(fn)) { + return await fn() + } else { + return fn() as T + } + } finally { + await AsyncLock.release(type) + } } private static async acquire (type: AsyncLockType): Promise {