+10
lib/mod.ts
+10
lib/mod.ts
···
1
+
/**
2
+
* represents an acquired lock that can be released
3
+
*/
1
4
export interface Lock extends Disposable {
2
5
dispose(): void;
3
6
}
4
7
8
+
/**
9
+
* provides mutual exclusion for async operations
10
+
*/
5
11
class Mutex {
6
12
#promise = Promise.resolve();
7
13
14
+
/**
15
+
* acquires the mutex, blocking until available
16
+
* @returns a lock that must be disposed to release the mutex
17
+
*/
8
18
async acquire(): Promise<Lock> {
9
19
const { promise, resolve } = Promise.withResolvers<void>();
10
20