@universal-lock/types
v1.0.0
Published
Type definitions for universal-lock
Maintainers
Readme
@universal-lock/types
Shared TypeScript type definitions for the universal-lock ecosystem.
Installation
npm install @universal-lock/typesNote: You typically don't need to install this package directly. It is included as a dependency of
universal-lockand all backend packages.
Types
Backend
type BackendSetupFunction = () => Promise<void>;
type BackendAcquireFunction = (lockName: string, stale: number, lockId: string) => Promise<void>;
type BackendRenewFunction = (lockName: string, lockId: string) => Promise<void>;
type BackendReleaseFunction = (lockName: string, lockId: string) => Promise<void>;
type Backend = {
setup: BackendSetupFunction;
acquire: BackendAcquireFunction;
renew: BackendRenewFunction;
release: BackendReleaseFunction;
};
type BackendFactory = (...args: any[]) => Backend;Utility
type AsyncFunction<R = unknown, P extends unknown[] = unknown[]> = (...args: P) => Promise<R>;Lock
type Lock = { acquire: LockAcquireFunction };
type LockAcquireFunction = (lockName: string) => Promise<LockReleaseFunction>;
type LockReleaseFunction = (() => Promise<void>) & { signal: AbortSignal };Configuration
type LockConfiguration = {
acquireInterval: number;
acquireFailTimeout: number;
stale: number;
renewInterval: number;
maxHoldTime: number;
onLockLost: (lockName: string, reason: "renewFailed" | "timeout") => void;
onEvent: (event: LockEvent) => void;
};Events
type LockEvent = { type: "acquired"; lockName: string } | { type: "released"; lockName: string } | { type: "renewed"; lockName: string } | { type: "renewFailed"; lockName: string; error: unknown } | { type: "lockLost"; lockName: string; reason: "renewFailed" | "timeout" } | { type: "acquireTimeout"; lockName: string };Lock Entry Types
Used by backend implementations:
type TimestampLockEntry = { lockId: string; timestamp: number };
type CallbackLockEntry = { lockId: string; release: () => void };