Skip to content

Commit e324508

Browse files
committed
fix: properly clear internal waiters on Mutex
Close #27
1 parent 57b1cf6 commit e324508

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

mutex.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* ```
2727
*/
2828
export class Mutex {
29-
#waiters: Promise<void>[] = [];
29+
#waiters: Set<Promise<void>> = new Set();
3030

3131
/**
3232
* Returns true if the mutex is locked, false otherwise.
3333
*/
3434
get locked(): boolean {
35-
return this.#waiters.length > 0;
35+
return this.#waiters.size > 0;
3636
}
3737

3838
/**
@@ -43,10 +43,11 @@ export class Mutex {
4343
acquire(): Promise<Disposable> & Disposable {
4444
const waiters = [...this.#waiters];
4545
const { promise, resolve } = Promise.withResolvers<void>();
46-
this.#waiters.push(promise);
46+
this.#waiters.add(promise);
4747
const disposable = {
4848
[Symbol.dispose]: () => {
4949
resolve();
50+
this.#waiters.delete(promise);
5051
},
5152
};
5253
return Object.assign(

0 commit comments

Comments
 (0)