Locks.d.ts 1.0 KB

12345678910111213141516171819202122232425
  1. /**
  2. * Creates a lock manager, which can create exclusive locks across browser tabs.
  3. * Uses `window.localStorage` by default to lock across tabs.
  4. *
  5. * Below example, will wait for 5 seconds to acquire a lock, and then execute
  6. * the function once lock is acquired and release the lock after function
  7. * execution. It will fail with `LOCK_TIMEOUT` error if lock is not acquired
  8. * within the 5 seconds. The lock will acquired for 2 seconds (default 1000ms).
  9. *
  10. * ```ts
  11. * Locks.get().lock('my-lock', 2000, 5000)(async () => {
  12. * console.log('Lock acquired');
  13. * });
  14. * ```
  15. */
  16. export declare class Locks {
  17. protected readonly store: Record<string, string>;
  18. protected readonly now: () => number;
  19. protected readonly pfx: string;
  20. static get: () => Locks;
  21. constructor(store?: Record<string, string>, now?: () => number, pfx?: string);
  22. acquire(id: string, ms?: number): (() => void) | undefined;
  23. isLocked(id: string): boolean;
  24. lock(id: string, ms?: number, timeoutMs?: number, checkMs?: number): <T>(fn: () => Promise<T>) => Promise<T>;
  25. }