fix(simulator): ensure configuration file reload will restart the
[e-mobility-charging-stations-simulator.git] / src / utils / AsyncLock.ts
index b396e49b50901fae636f565198f73dddc0b58c09..fe984cebdef97d8043d0f29f6fb9ba99ec502f3c 100644 (file)
@@ -2,6 +2,8 @@
 
 import Queue from 'mnemonist/queue.js';
 
+import { Constants } from './Constants';
+
 export enum AsyncLockType {
   configuration = 'configuration',
   performance = 'performance',
@@ -19,25 +21,33 @@ export class AsyncLock {
     this.resolveQueue = new Queue<ResolveType>();
   }
 
-  public static async acquire(type: AsyncLockType): Promise<void> {
+  public static async runExclusive<T>(type: AsyncLockType, fn: () => T | Promise<T>): Promise<T> {
+    return AsyncLock.acquire(type)
+      .then(fn)
+      .finally(() => {
+        AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION);
+      });
+  }
+
+  private static async acquire(type: AsyncLockType): Promise<void> {
     const asyncLock = AsyncLock.getAsyncLock(type);
     if (!asyncLock.acquired) {
       asyncLock.acquired = true;
       return;
     }
-    return new Promise((resolve) => {
+    return new Promise<void>((resolve) => {
       asyncLock.resolveQueue.enqueue(resolve);
     });
   }
 
-  public static async release(type: AsyncLockType): Promise<void> {
+  private static async release(type: AsyncLockType): Promise<void> {
     const asyncLock = AsyncLock.getAsyncLock(type);
     if (asyncLock.resolveQueue.size === 0 && asyncLock.acquired) {
       asyncLock.acquired = false;
       return;
     }
     const queuedResolve = asyncLock.resolveQueue.dequeue()!;
-    return new Promise((resolve) => {
+    return new Promise<void>((resolve) => {
       queuedResolve();
       resolve();
     });