/** * a value created by `mutex.lock()` * * calling the `guard()` or `guard.release()` will release the mutex and revoke `MutexGuard.value` * so that any subsequent access to the value will throw a TypeError * * @public */ export type MutexGuard = V extends object ? { (): void; release: () => void; value: V; } : () => void; /** * Asynchronos style mutex lock * * @typeParam V - type of the object wrapped by the mutex, and a immutable T does not make sense * @public */ export declare class Mutex { protected _value: V; private semaphore; /** * {@link Semaphore} with capacity of 1 * * @param value - you may optionally wrap an object with mutex */ constructor(); constructor(value: V); /** * acquire lock * * @returns `MutexGuard` - a function to release the lock, you can access wrapped value using `MutexGuard.value` before release * * @example * ```typescript * const mutex = new Mutex({ a: 1 }) * const { release, value } = await mutex.lock() * const ref = value * ref.a // => 1 * release() * ref.a // => TypeError, temporary reference destroyed * ``` */ lock(timeout?: number): Promise>; /** * synchronosly acquire lock * * @throws Error if failed to acquire lock * @returns `MutexGuard` - a function to release the lock, you can access wrapped value using `MutexGuard.value` before release * * @example * ```typescript * const mutex = new Mutex({ a: 1 }) * const { release, value } = mutex.tryLock() * const ref = value // value is a temporary reference which does not equal the value stores in mutex * ref.a // => 1 * release() * ref.a // => TypeError, temporary reference destroyed * ``` */ tryLock(): MutexGuard; /** * check if mutex is available, returns true if it is not locked and frozen */ get canLock(): boolean; /** * Schedule a task to run when mutex is not locked. */ schedule(fn: (v: V) => T): Promise>; /** * freeze the mutex lock, see {@link Semaphore.freeze} */ freeze(): void; /** * unfreeze the mutex lock, see {@link Semaphore.unfreeze} */ unfreeze(): Promise; /** * unfreeze the mutex lock, see {@link Semaphore.unfreeze} */ get frozen(): boolean; private createLockGuard; } //# sourceMappingURL=mutex.d.ts.map