import type { MutexInterface } from './interface'; import Semaphore from './semaphore'; class Mutex implements MutexInterface { constructor(cancelError?: Error) { this._semaphore = new Semaphore(1, cancelError); } async acquire(priority = 0): Promise { const [, releaser] = await this._semaphore.acquire(1, priority); return releaser; } runExclusive( callback: MutexInterface.Worker, priority = 0 ): Promise { return this._semaphore.runExclusive(() => callback(), 1, priority); } isLocked(): boolean { return this._semaphore.isLocked(); } waitForUnlock(priority = 0): Promise { return this._semaphore.waitForUnlock(1, priority); } release(): void { if (this._semaphore.isLocked()) this._semaphore.release(); } cancel(): void { return this._semaphore.cancel(); } private _semaphore: Semaphore; } export default Mutex;