Skip to main content
zerotal

Documentation


Documentation / zerotal / lock / Lock

Class: Lock

Defined in: packages/core/src/lock/facades/Lock.ts:57

Static facade over the container-bound LockManager.

Resolves the live lock singleton on every call and forwards to the manager, so it exposes the same three usage styles: try (fail fast), block (wait for the lock), and make (a manual ManagedLock handle). Requires LockProvider to be registered.

Example

// Fail-fast — throws immediately if the lock is busy
await Lock.try('invoice:123', 10, async () => {
  await processInvoice(123);
});

// Blocking — waits up to 30s for the lock
await Lock.block('invoice:123', 10, async () => {
  await processInvoice(123);
}, { timeout: 30 });

// Manual handle for complex flows
const handle = Lock.make('payment:456', 15);
if (await handle.acquire()) {
  try {
    await capturePayment(456);
  } finally {
    await handle.release();
  }
}

Constructors

Constructor

new Lock(): Lock

Returns

Lock

Acquiring

make()

static make(key, ttlSeconds): ManagedLock

Defined in: packages/core/src/lock/facades/Lock.ts:67

Create a named lock handle for manual acquire/release flows. Does NOT acquire the lock — call .acquire() or .block() explicitly.

Parameters

key

string

Logical lock name.

ttlSeconds

number

Lock time-to-live in seconds.

Returns

ManagedLock

Throws

(code E_LOCK_FACADE_UNAVAILABLE) if LockProvider is not registered.


try()

static try<T>(key, ttlSeconds, callback): Promise<T>

Defined in: packages/core/src/lock/facades/Lock.ts:82

Acquire the lock exactly once, run callback, then release — always, even if callback throws.

Type Parameters

T

T

Parameters

key

string

Logical lock name.

ttlSeconds

number

Lock time-to-live in seconds.

callback

() => T | Promise<T>

Critical section to run while the lock is held.

Returns

Promise<T>

The value returned by callback.

Throws

Immediately, if the lock is busy.


block()

static block<T>(key, ttlSeconds, callback, options?): Promise<T>

Defined in: packages/core/src/lock/facades/Lock.ts:98

Wait until the lock is free (up to options.timeout seconds), run callback, then release — always, even if callback throws.

Type Parameters

T

T

Parameters

key

string

Logical lock name.

ttlSeconds

number

Lock time-to-live in seconds.

callback

() => T | Promise<T>

Critical section to run while the lock is held.

options?

BlockOptions

Wait timeout and poll interval.

Returns

Promise<T>

The value returned by callback.

Throws

If the timeout elapses before the lock is acquired.


NotAcquired

readonly static NotAcquired: typeof LockNotAcquiredError = LockNotAcquiredError

Defined in: packages/core/src/lock/facades/Lock.ts:113

The LockNotAcquiredError class, re-exported for convenience in catch blocks (err instanceof Lock.NotAcquired).