composable-locks
Version:
Composable concurrency locks for Javascript.
25 lines (24 loc) • 951 B
TypeScript
import type { ILock, Releaser } from "./interfaces";
export declare type Resolver<K> = (key: K) => K;
declare type LockRecord<T> = {
count: number;
lock: T;
};
/**
* A keyed lock, for mapping strings to a lock type
*/
export declare class KeyedMutex<TArgs extends unknown[], TKey extends string | number | symbol> implements ILock<[TKey, ...TArgs]> {
protected newLock: () => ILock<TArgs>;
protected locks: Record<TKey, LockRecord<ILock<TArgs>>>;
protected resolver: Resolver<TKey>;
/**
* A keyed lock, for mapping strings to a lock type
* @param newLock A function to create a new lock interface
* @param resolver A function to transform a key into a normalized form.
* Useful for resolving paths.
*/
constructor(newLock: () => ILock<TArgs>, resolver?: Resolver<TKey>);
private getOrCreateLock;
acquire(key: TKey, ...args: TArgs): Promise<Releaser>;
}
export {};