@vulppi/promise-lock
Version:
A promise-based lock library for Vulppi
62 lines (58 loc) • 2.03 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
/**
* Creates a lock object that can be used to synchronize access to a shared resource.
*
* @param opt.timeout - The timeout in milliseconds for acquiring the lock.
* Defaults to 20 seconds.
* If timeout is 0, it will never timeout.
*
* @return An object with the following properties:
* - `length`: A getter function that returns the current number of active locks.
* - `lock`: An asynchronous function that returns a lock object. When called, it increments the `length` property and waits for any previously acquired locks to be released before resolving. The lock object has the following properties:
* - `length`: A getter function that returns the current number of active locks.
* - `unlock`: A function that releases the lock by decrementing the `length` property and resolving any waiting locks.
*/
export declare function createLocker(opt?: {
timeout?: number;
}): {
/**
* Getter function that returns the current number of active locks.
*
* @return {number} The current number of active locks.
*/
readonly length: number;
/**
* Asynchronously acquires a lock and returns a lock object.
*
* @return A promise that resolves to an object with two properties:
* - `length`: A getter function that returns the current number of active locks.
* - `unlock`: A function that releases the lock for the next lock can run.
*/
lock(): Promise<Lock$1>;
};
type Lock$1 = {
/**
* Getter function that returns the current number of active locks.
*
* @return {number} The current number of active locks.
*/
readonly length: number;
/**
* Getter function that returns if the lock is already free.
*
* @return {boolean} If the lock is already free.
*/
readonly is_free: boolean;
/**
* A function that releases the lock for the next lock can run.
*/
unlock: () => void;
/**
* A function that releases the lock and stops propagation.
*/
stopPropagation: () => void;
};
export {
Lock$1 as Lock,
};
export {};