Something went wrong. Try again.
archived: a really simple mutex jsr.io/@mary/mutex
typescript jsr
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031/** * represents an acquired lock that can be released */export interface Lock extends Disposable { dispose(): void;}
/** * provides mutual exclusion for async operations */class Mutex { #promise = Promise.resolve();
/** * acquires the mutex, blocking until available * @returns a lock that must be disposed to release the mutex */ async acquire(): Promise<Lock> { const { promise, resolve } = Promise.withResolvers<void>();
const previous = this.#promise;
this.#promise = promise; await previous;
return { [Symbol.dispose]: resolve, dispose: resolve }; }}
export default Mutex;