Skip to content

Commit

Permalink
Merge pull request #28 from jsr-core/fix-mutex
Browse files Browse the repository at this point in the history
fix: properly clear internal waiters on `Mutex`
  • Loading branch information
lambdalisue authored Aug 12, 2024
2 parents e8a2fce + e324508 commit b000238
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
31 changes: 31 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 b000238

Please sign in to comment.