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(