/** * A callback function to execute during a lock operation. * @template T - The return type of the task. * @param lockId - The unique identifier for the lock. * @returns A promise resolving to the task's result. */ type TaskCallback = (lockId: string) => Promise; /** * Represents a range with a start and end. */ type RyoikiRange = [number, number]; /** * A map containing task units by their lock ID. */ type TaskUnits = Map; /** * Represents a unit of a task with a range and lifecycle methods. */ interface TaskUnit { id: string; range: RyoikiRange; condition: () => boolean; alloc: () => Promise; free: () => void; } /** * Ryoiki is a locking library for handling overlapping read/write operations * in a range-based manner. */ export declare class Ryoiki { protected readonly readings: TaskUnits; protected readonly writings: TaskUnits; protected readonly readQueue: TaskUnits; protected readonly writeQueue: TaskUnits; protected static CatchError(promise: Promise): Promise<[undefined, T] | [Error]>; protected static IsRangeOverlap(a: RyoikiRange, b: RyoikiRange): boolean; protected static ERR_ALREADY_EXISTS(lockId: string): Error; protected static ERR_NOT_EXISTS(lockId: string): Error; protected static ERR_TIMEOUT(lockId: string, timeout: number): Error; /** * Constructs a new instance of the Ryoiki class. */ constructor(); /** * Creates a range based on a start value and length. * @param start - The starting value of the range. * @param length - The length of the range. * @returns A range tuple [start, start + length]. */ range(start: number, length: number): RyoikiRange; protected rangeOverlapping(tasks: TaskUnits, range: RyoikiRange): boolean; protected isSameRange(a: RyoikiRange, b: RyoikiRange): boolean; protected fetchUnitAndRun(queue: TaskUnits, workspaces: TaskUnits): void; private _handleOverload; private _matchArgs; private _createRandomId; private _alloc; private _free; private _lock; private _checkWorking; /** * Checks if there is any active read lock within the specified range. * @param range The range to check for active read locks. * @returns `true` if there is an active read lock within the range, `false` otherwise. */ isReading(range: RyoikiRange): boolean; /** * Checks if there is any active write lock within the specified range. * @param range The range to check for active write locks. * @returns `true` if there is an active write lock within the range, `false` otherwise. */ isWriting(range: RyoikiRange): boolean; /** * Checks if a read lock can be acquired within the specified range. * @param range The range to check for read lock availability. * @returns `true` if a read lock can be acquired, `false` otherwise. */ canRead(range: RyoikiRange): boolean; /** * Checks if a write lock can be acquired within the specified range. * @param range The range to check for write lock availability. * @returns `true` if a write lock can be acquired, `false` otherwise. */ canWrite(range: RyoikiRange): boolean; /** * Acquires a read lock for the entire range. * @template T - The return type of the task. * @param task - The task to execute within the lock. * @param timeout - The timeout for acquiring the lock. * If the lock cannot be acquired within this period, an error will be thrown. * If this value is not provided, no timeout will be set. * The task receives the lock ID as an argument. * @returns A promise resolving to the result of the task execution. */ readLock(task: TaskCallback, timeout?: number | undefined): Promise; /** * Acquires a read lock for a specific range. * @template T - The return type of the task. * @param range - The range to lock, specified as a tuple [start, end]. * @param task - The task to execute within the lock. * @param timeout - The timeout for acquiring the lock. * If the lock cannot be acquired within this period, an error will be thrown. * If this value is not provided, no timeout will be set. * The task receives the lock ID as an argument. * @returns A promise resolving to the result of the task execution. */ readLock(range: RyoikiRange, task: TaskCallback, timeout?: number | undefined): Promise; /** * Acquires a write lock for the entire range. * @template T - The return type of the task. * @param task - The task to execute within the lock. * @param timeout - The timeout for acquiring the lock. * If the lock cannot be acquired within this period, an error will be thrown. * If this value is not provided, no timeout will be set. * The task receives the lock ID as an argument. * @returns A promise resolving to the result of the task execution. */ writeLock(task: TaskCallback, timeout?: number | undefined): Promise; /** * Acquires a write lock for a specific range. * @template T - The return type of the task. * @param range - The range to lock, specified as a tuple [start, end]. * @param task - The task to execute within the lock. * @param timeout - The timeout for acquiring the lock. * If the lock cannot be acquired within this period, an error will be thrown. * If this value is not provided, no timeout will be set. * The task receives the lock ID as an argument. * @returns A promise resolving to the result of the task execution. */ writeLock(range: RyoikiRange, task: TaskCallback, timeout?: number | undefined): Promise; /** * Releases a read lock by its lock ID. * @param lockId - The unique identifier for the lock to release. */ readUnlock(lockId: string): void; /** * Releases a write lock by its lock ID. * @param lockId - The unique identifier for the lock to release. */ writeUnlock(lockId: string): void; } export {};