@da440dil/js-locker
Version:
Distributed locking using Redis
33 lines (32 loc) • 1.01 kB
TypeScript
export declare class LockResult implements ILockResult {
private _lock;
private result;
constructor(lock: ILock, result: Result);
lock(ttl: number): Promise<Result>;
unlock(): Promise<boolean>;
get ok(): boolean;
get ttl(): number;
}
/** Implements distributed locking. */
export interface ILock {
/**
* Applies the lock if it is not already applied, otherwise extends the lock TTL.
* @param ttl TTL of the lock key. Must be positive integer.
*/
lock(ttl: number): Promise<Result>;
/** Releases the lock. */
unlock(): Promise<boolean>;
}
/** Result of applying a lock. */
export declare type Result = {
/** Success flag of applying a lock. */
ok: boolean;
/**
* TTL of a lock in milliseconds.
* Makes sense if operation failed, otherwise ttl is less than 0.
*/
ttl: number;
};
/** Contains new lock and result of applying a lock. */
export interface ILockResult extends ILock, Result {
}