Skip to content

Commit

Permalink
fix: properly clear internal waiters on Mutex
Browse files Browse the repository at this point in the history
Close #27
  • Loading branch information
lambdalisue committed Aug 12, 2024
1 parent 57b1cf6 commit e324508
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
* ```
*/
export class Mutex {
#waiters: Promise<void>[] = [];
#waiters: Set<Promise<void>> = new Set();

/**
* Returns true if the mutex is locked, false otherwise.
*/
get locked(): boolean {
return this.#waiters.length > 0;
return this.#waiters.size > 0;
}

/**
Expand All @@ -43,10 +43,11 @@ export class Mutex {
acquire(): Promise<Disposable> & Disposable {
const waiters = [...this.#waiters];
const { promise, resolve } = Promise.withResolvers<void>();
this.#waiters.push(promise);
this.#waiters.add(promise);
const disposable = {
[Symbol.dispose]: () => {
resolve();
this.#waiters.delete(promise);
},
};
return Object.assign(
Expand Down

0 comments on commit e324508

Please sign in to comment.