diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..5b0da6c --- /dev/null +++ b/deno.lock @@ -0,0 +1,31 @@ +{ + "version": "3", + "packages": { + "specifiers": { + "jsr:@std/assert@^1.0.2": "jsr:@std/assert@1.0.2", + "jsr:@std/async@^1.0.2": "jsr:@std/async@1.0.2", + "jsr:@std/internal@^1.0.1": "jsr:@std/internal@1.0.1" + }, + "jsr": { + "@std/assert@1.0.2": { + "integrity": "ccacec332958126deaceb5c63ff8b4eaf9f5ed0eac9feccf124110435e59e49c", + "dependencies": [ + "jsr:@std/internal@^1.0.1" + ] + }, + "@std/async@1.0.2": { + "integrity": "36e7f0f922c843b45df546857d269f01ed4d0406aced2a6639eac325b2435e43" + }, + "@std/internal@1.0.1": { + "integrity": "6f8c7544d06a11dd256c8d6ba54b11ed870aac6c5aeafff499892662c57673e6" + } + } + }, + "remote": {}, + "workspace": { + "dependencies": [ + "jsr:@std/assert@^1.0.2", + "jsr:@std/async@^1.0.2" + ] + } +} diff --git a/mutex.ts b/mutex.ts index 4851dbb..92c8133 100644 --- a/mutex.ts +++ b/mutex.ts @@ -26,13 +26,13 @@ * ``` */ export class Mutex { - #waiters: Promise[] = []; + #waiters: Set> = new Set(); /** * Returns true if the mutex is locked, false otherwise. */ get locked(): boolean { - return this.#waiters.length > 0; + return this.#waiters.size > 0; } /** @@ -43,10 +43,11 @@ export class Mutex { acquire(): Promise & Disposable { const waiters = [...this.#waiters]; const { promise, resolve } = Promise.withResolvers(); - this.#waiters.push(promise); + this.#waiters.add(promise); const disposable = { [Symbol.dispose]: () => { resolve(); + this.#waiters.delete(promise); }, }; return Object.assign(