repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
perf: lookup connector status once at OCPP responses handling
[e-mobility-charging-stations-simulator.git]
/
src
/
utils
/
AsyncLock.ts
diff --git
a/src/utils/AsyncLock.ts
b/src/utils/AsyncLock.ts
index 8bb8f519222dd1f738fdfd64cd02317c91e4a62a..9c0584565e3ecb80897b2ad32418a1afdb0cc888 100644
(file)
--- a/
src/utils/AsyncLock.ts
+++ b/
src/utils/AsyncLock.ts
@@
-1,44
+1,52
@@
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
+
+import Queue from 'mnemonist/queue.js';
+
export enum AsyncLockType {
configuration = 'configuration',
performance = 'performance',
}
export enum AsyncLockType {
configuration = 'configuration',
performance = 'performance',
}
+type ResolveType = (value: void | PromiseLike<void>) => void;
+
export class AsyncLock {
export class AsyncLock {
- private static readonly
instance
s = new Map<AsyncLockType, AsyncLock>();
+ private static readonly
asyncLock
s = new Map<AsyncLockType, AsyncLock>();
private acquired: boolean;
private acquired: boolean;
- private readonly resolveQueue:
((value: void | PromiseLike<void>) => void)[]
;
+ private readonly resolveQueue:
Queue<ResolveType>
;
- private constructor(
private readonly type: AsyncLockType
) {
+ private constructor() {
this.acquired = false;
this.acquired = false;
- 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);
+ this.resolveQueue = new Queue<ResolveType>();
}
}
- public async acquire(): Promise<void> {
- if (!this.acquired) {
- this.acquired = true;
- } else {
- return new Promise((resolve) => {
- this.resolveQueue.push(resolve);
- });
+ public static async acquire(type: AsyncLockType): Promise<void> {
+ const asyncLock = AsyncLock.getAsyncLock(type);
+ if (!asyncLock.acquired) {
+ asyncLock.acquired = true;
+ return;
}
}
+ return new Promise<void>((resolve) => {
+ asyncLock.resolveQueue.enqueue(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.size === 0 && asyncLock.acquired) {
+ asyncLock.acquired = false;
return;
}
return;
}
- const queuedResolve =
this.resolveQueue.shift()
;
- return new Promise((resolve) => {
+ const queuedResolve =
asyncLock.resolveQueue.dequeue()!
;
+ return new Promise
<void>
((resolve) => {
queuedResolve();
resolve();
});
}
queuedResolve();
resolve();
});
}
+
+ private static getAsyncLock(type: AsyncLockType): AsyncLock {
+ if (!AsyncLock.asyncLocks.has(type)) {
+ AsyncLock.asyncLocks.set(type, new AsyncLock());
+ }
+ return AsyncLock.asyncLocks.get(type)!;
+ }
}
}