From 57b1cf63dbea71542e0a286940241c41ad7b26e6 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 13 Aug 2024 06:04:53 +0900 Subject: [PATCH 1/2] chore: Add `deno.lock` file --- deno.lock | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 deno.lock 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" + ] + } +} From e3245086e2a0c39e32610a3b834b29355dbfd9a8 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 13 Aug 2024 06:05:07 +0900 Subject: [PATCH 2/2] fix: properly clear internal waiters on `Mutex` Close #27 --- mutex.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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(