chore: switch coding style to JS standard
[e-mobility-charging-stations-simulator.git] / src / utils / AsyncLock.ts
index 3f89759d33b6a191c1c2ee8d9384ba452de20b43..c6b35b84457fa9f090b002417e4cbdfeb32cfb5e 100644 (file)
@@ -1,62 +1,64 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { Queue } from 'mnemonist';
+import { Queue } from 'mnemonist'
 
-import { Constants } from './Constants.js';
+import { Constants } from './Constants.js'
 
 export enum AsyncLockType {
   configuration = 'configuration',
-  performance = 'performance',
+  performance = 'performance'
 }
 
-type ResolveType = (value: void | PromiseLike<void>) => void;
+type ResolveType = (value: void | PromiseLike<void>) => void
 
 export class AsyncLock {
-  private static readonly asyncLocks = new Map<AsyncLockType, AsyncLock>();
-  private acquired: boolean;
-  private readonly resolveQueue: Queue<ResolveType>;
+  private static readonly asyncLocks = new Map<AsyncLockType, AsyncLock>()
+  private acquired: boolean
+  private readonly resolveQueue: Queue<ResolveType>
 
-  private constructor() {
-    this.acquired = false;
-    this.resolveQueue = new Queue<ResolveType>();
+  private constructor () {
+    this.acquired = false
+    this.resolveQueue = new Queue<ResolveType>()
   }
 
   public static async runExclusive<T>(type: AsyncLockType, fn: () => T | Promise<T>): Promise<T> {
-    return AsyncLock.acquire(type)
+    return await AsyncLock.acquire(type)
       .then(fn)
       .finally(() => {
-        AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION);
-      });
+        AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION)
+      })
   }
 
-  private static async acquire(type: AsyncLockType): Promise<void> {
-    const asyncLock = AsyncLock.getAsyncLock(type);
+  private static async acquire (type: AsyncLockType): Promise<void> {
+    const asyncLock = AsyncLock.getAsyncLock(type)
     if (!asyncLock.acquired) {
-      asyncLock.acquired = true;
-      return;
+      asyncLock.acquired = true
+      return
     }
-    return new Promise<void>((resolve) => {
-      asyncLock.resolveQueue.enqueue(resolve);
-    });
+    await new Promise<void>((resolve) => {
+      asyncLock.resolveQueue.enqueue(resolve)
+    })
   }
 
-  private static async release(type: AsyncLockType): Promise<void> {
-    const asyncLock = AsyncLock.getAsyncLock(type);
+  private static async release (type: AsyncLockType): Promise<void> {
+    const asyncLock = AsyncLock.getAsyncLock(type)
     if (asyncLock.resolveQueue.size === 0 && asyncLock.acquired) {
-      asyncLock.acquired = false;
-      return;
+      asyncLock.acquired = false
+      return
     }
-    const queuedResolve = asyncLock.resolveQueue.dequeue()!;
-    return new Promise<void>((resolve) => {
-      queuedResolve();
-      resolve();
-    });
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    const queuedResolve = asyncLock.resolveQueue.dequeue()!
+    await new Promise<void>((resolve) => {
+      queuedResolve()
+      resolve()
+    })
   }
 
-  private static getAsyncLock(type: AsyncLockType): AsyncLock {
+  private static getAsyncLock (type: AsyncLockType): AsyncLock {
     if (!AsyncLock.asyncLocks.has(type)) {
-      AsyncLock.asyncLocks.set(type, new AsyncLock());
+      AsyncLock.asyncLocks.set(type, new AsyncLock())
     }
-    return AsyncLock.asyncLocks.get(type)!;
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    return AsyncLock.asyncLocks.get(type)!
   }
 }