Class Locks

Creates a lock manager, which can create exclusive locks across browser tabs. Uses window.localStorage by default to lock across tabs.

Below example, will wait for 5 seconds to acquire a lock, and then execute the function once lock is acquired and release the lock after function execution. It will fail with LOCK_TIMEOUT error if lock is not acquired within the 5 seconds. The lock will acquired for 2 seconds (default 1000ms).

Locks.get().lock('my-lock', 2000, 5000)(async () => {
console.log('Lock acquired');
});

Constructors

Properties

Methods

Constructors

  • Parameters

    • store: Record<string, string> = ...
    • now: (() => number) = Date.now
        • (): number
        • Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

          Returns number

    • pfx: string = 'lock-'

    Returns Locks

Properties

now: (() => number) = Date.now

Type declaration

    • (): number
    • Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

      Returns number

pfx: string = 'lock-'
store: Record<string, string> = ...

Methods

  • Parameters

    • id: string
    • Optional ms: number
    • timeoutMs: number = ...
    • checkMs: number = 10

    Returns (<T>(fn) => Promise<T>)

      • <T>(fn): Promise<T>
      • Type Parameters

        • T

        Parameters

        • fn: (() => Promise<T>)
            • (): Promise<T>
            • Returns Promise<T>

        Returns Promise<T>