.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`;
);
})
.finally(() => {
- asyncLock.release().catch(Constants.EMPTY_FUNCTION);
+ AsyncLock.release(AsyncLockType.configuration).catch(Constants.EMPTY_FUNCTION);
});
} else {
logger.debug(
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
);
})
.finally(() => {
- asyncLock.release().catch(Constants.EMPTY_FUNCTION);
+ AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION);
});
}
}
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)[];
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);
+ }
}