rwlockfile
Version:
lockfile utility with reader/writers
97 lines (96 loc) • 2.19 kB
TypeScript
export interface LockfileOptions {
debug?: any;
timeout?: number;
retryInterval?: number;
}
export interface LockOptions {
reason?: string;
ifLocked: ({reason}: {
reason?: string;
}) => Promise<void> | void;
timeout: number;
retryInterval: number;
}
export default class Lockfile {
base: string;
timeout: number;
retryInterval: number;
stale: number;
uuid: string;
private fs;
private _debug?;
private _count;
private updater?;
/**
* creates a new simple lockfile without read/write support
*/
constructor(base: string, options?: LockfileOptions);
readonly count: number;
readonly dirPath: string;
/**
* creates a lock
* same as add
*/
lock(): Promise<void>;
/**
* creates a lock
* same as add
*/
lockSync(): void;
/**
* removes all lock counts
*/
unlock(): Promise<void>;
/**
* removes all lock counts
*/
unlockSync(): void;
/**
* adds 1 lock count
*/
add(opts?: Partial<LockOptions>): Promise<void>;
/**
* adds 1 lock count
*/
addSync(opts?: {
reason?: string;
}): void;
/**
* removes 1 lock count
*/
remove(): Promise<void>;
/**
* removes 1 lock count
*/
removeSync(): void;
/**
* check if this instance can get a lock
* returns true if it already has a lock
*/
check(): Promise<boolean>;
/**
* check if this instance can get a lock
* returns true if it already has a lock
*/
checkSync(): boolean;
private readonly _infoPath;
private fetchReason();
private fetchReasonSync();
private _saveReason(reason);
private _saveReasonSync(reason);
private fetchMtime();
private fetchMtimeSync();
private isStale(mtime?);
private debug(msg, ...args);
private _add(opts);
private _lock(opts);
private _lockSync({reason, retries}?);
private _status(mtime);
private startLocking();
private _stopLocking();
private _debugReport(action, reason?);
}
export interface RWLockfileOptions {
debug?: any;
file?: string;
}