refactor: cleanup async lock API
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 2 May 2023 21:22:51 +0000 (23:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 2 May 2023 21:22:51 +0000 (23:22 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/performance/storage/JsonFileStorage.ts
src/utils/AsyncLock.ts

index 9855c8cabdab3fca7242f5122a99ca99831b2cc9..f18951635f72614fe0b284670668d655a3af5d63 100644 (file)
@@ -1594,9 +1594,7 @@ export class ChargingStation {
           .update(JSON.stringify(configurationData))
           .digest('hex');
         if (this.configurationFileHash !== configurationHash) {
-          const asyncLock = AsyncLock.getInstance(AsyncLockType.configuration);
-          asyncLock
-            .acquire()
+          AsyncLock.acquire(AsyncLockType.configuration)
             .then(() => {
               configurationData.configurationHash = configurationHash;
               const measureId = `${FileType.ChargingStationConfiguration} write`;
@@ -1618,7 +1616,7 @@ export class ChargingStation {
               );
             })
             .finally(() => {
-              asyncLock.release().catch(Constants.EMPTY_FUNCTION);
+              AsyncLock.release(AsyncLockType.configuration).catch(Constants.EMPTY_FUNCTION);
             });
         } else {
           logger.debug(
index 5effae906dc6965d3d60893c801d5e6b465e8d4d..fb7062674bb2ea14787a2d5eb0ff6646bab75d11 100644 (file)
@@ -16,9 +16,7 @@ export class JsonFileStorage extends Storage {
 
   public storePerformanceStatistics(performanceStatistics: Statistics): void {
     this.checkPerformanceRecordsFile();
-    const asyncLock = AsyncLock.getInstance(AsyncLockType.performance);
-    asyncLock
-      .acquire()
+    AsyncLock.acquire(AsyncLockType.performance)
       .then(() => {
         const fileData = fs.readFileSync(this.dbName, 'utf8');
         const performanceRecords: Statistics[] = fileData
@@ -40,7 +38,7 @@ export class JsonFileStorage extends Storage {
         );
       })
       .finally(() => {
-        asyncLock.release().catch(Constants.EMPTY_FUNCTION);
+        AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION);
       });
   }
 
index 53907ce46b723f246aa1bf0361f5986b727caf21..7f44bd257251f32cde860ce6a40d6eb82cf1a51c 100644 (file)
@@ -6,7 +6,7 @@ export enum AsyncLockType {
 }
 
 export class AsyncLock {
-  private static readonly instances = new Map<AsyncLockType, AsyncLock>();
+  private static readonly asyncLocks = new Map<AsyncLockType, AsyncLock>();
   private acquired: boolean;
   private readonly resolveQueue: ((value: void | PromiseLike<void>) => void)[];
 
@@ -15,32 +15,34 @@ export class AsyncLock {
     this.resolveQueue = [];
   }
 
-  public static getInstance(type: AsyncLockType): AsyncLock {
-    if (!AsyncLock.instances.has(type)) {
-      AsyncLock.instances.set(type, new AsyncLock(type));
-    }
-    return AsyncLock.instances.get(type);
-  }
-
-  public async acquire(): Promise<void> {
-    if (!this.acquired) {
-      this.acquired = true;
+  public static async acquire(type: AsyncLockType): Promise<void> {
+    const asyncLock = AsyncLock.getAsyncLock(type);
+    if (!asyncLock.acquired) {
+      asyncLock.acquired = true;
     } else {
       return new Promise((resolve) => {
-        this.resolveQueue.push(resolve);
+        asyncLock.resolveQueue.push(resolve);
       });
     }
   }
 
-  public async release(): Promise<void> {
-    if (this.resolveQueue.length === 0 && this.acquired) {
-      this.acquired = false;
+  public static async release(type: AsyncLockType): Promise<void> {
+    const asyncLock = AsyncLock.getAsyncLock(type);
+    if (asyncLock.resolveQueue.length === 0 && asyncLock.acquired) {
+      asyncLock.acquired = false;
       return;
     }
-    const queuedResolve = this.resolveQueue.shift();
+    const queuedResolve = asyncLock.resolveQueue.shift();
     return new Promise((resolve) => {
       queuedResolve();
       resolve();
     });
   }
+
+  private static getAsyncLock(type: AsyncLockType): AsyncLock {
+    if (!AsyncLock.asyncLocks.has(type)) {
+      AsyncLock.asyncLocks.set(type, new AsyncLock(type));
+    }
+    return AsyncLock.asyncLocks.get(type);
+  }
 }