rooks
Version:
Collection of awesome react hooks
79 lines (78 loc) • 1.92 kB
TypeScript
/**
* Web Locks API lock request options
*/
type LockOptions = {
/**
* The lock mode - "exclusive" (default) or "shared"
*/
mode?: "exclusive" | "shared";
/**
* If true, the lock request will only succeed if the lock is available immediately
*/
ifAvailable?: boolean;
/**
* If true, any held locks with the same name will be released first
*/
steal?: boolean;
/**
* AbortSignal to cancel the lock request
*/
signal?: AbortSignal;
};
/**
* Hook configuration options
*/
type UseWebLocksApiOptions = {
/**
* Enable periodic checking of lock state (disabled by default)
*/
periodicCheck?: boolean;
/**
* Interval in milliseconds for periodic checks (default: 1000ms)
*/
checkInterval?: number;
};
/**
* useWebLocksApi hook return type
*/
type UseWebLocksApiReturn = {
/**
* Whether Web Locks API is supported
*/
isSupported: boolean;
/**
* Whether the resource is currently locked
*/
isLocked: boolean;
/**
* Number of pending lock requests for this resource
*/
waitingCount: number;
/**
* Current error state
*/
error: Error | null;
/**
* The resource name being managed
*/
resourceName: string;
/**
* Acquire a lock on the resource
*/
acquire: <T>(callback: () => Promise<T> | T, options?: LockOptions) => Promise<T>;
/**
* Release the current lock
*/
release: () => void;
/**
* Query the current lock state
*/
query: () => Promise<any>;
};
/**
* useWebLocksApi
* @description Hook for coordinating operations across tabs/workers with Web Locks API
* @see {@link https://rooks.vercel.app/docs/hooks/useWebLocksApi}
*/
declare function useWebLocksApi(resourceName: string, options?: UseWebLocksApiOptions): UseWebLocksApiReturn;
export { useWebLocksApi };