From: Jérôme Benoit Date: Tue, 12 Aug 2025 21:41:26 +0000 (+0200) Subject: refactor: cleanup async lock implementation X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2e719408cec263020d66d056d63579b154524eb4;p=e-mobility-charging-stations-simulator.git refactor: cleanup async lock implementation Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index 49565046..8fad063b 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -30,7 +30,7 @@ export class AsyncLock { return fn() as T } } finally { - await AsyncLock.release(type) + AsyncLock.release(type) } } @@ -53,16 +53,13 @@ export class AsyncLock { return AsyncLock.asyncLocks.get(type)! } - private static async release (type: AsyncLockType): Promise { + private static release (type: AsyncLockType): void { const asyncLock = AsyncLock.getAsyncLock(type) - if (asyncLock.resolveQueue.size === 0 && asyncLock.acquired) { - asyncLock.acquired = false + const nextResolve = asyncLock.resolveQueue.dequeue() + if (nextResolve != null) { + nextResolve() return } - await new Promise(resolve => { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - asyncLock.resolveQueue.dequeue()!() - resolve() - }) + asyncLock.acquired = false } }