useLockOnMount

useLockOnMount acquires a record lock when the component mounts and releases it automatically on unmount. It falls back to the current ResourceContext, RecordContext, and authProvider identity when explicit options are not provided.

Usage

import { useLockOnMount } from "@/components/realtime";
import { LockConflictError } from "@/components/realtime";
 
function EditGuard({ children }: { children: React.ReactNode }) {
  const { lock, isLocking, lockError } = useLockOnMount();
 
  if (isLocking) return <p>Acquiring lock…</p>;
  if (lockError instanceof LockConflictError) {
    return <p>Locked by {String(lockError.existingLock.identity)}</p>;
  }
  if (lock) return <>{children}</>;
  return null;
}

Params (options object)

FieldTypeDefaultDescription
resourcestringResourceContextResource to lock. Falls back to context.
idIdentifierRecordContext.idRecord ID. Falls back to context.
identityIdentifieruseGetIdentity()User identity. Falls back to auth provider.
metaRecord<string, unknown>Forwarded to lock()
onLockError(error: Error) => voidCalled if the lock call fails

Returns

FieldTypeDescription
lockLock | nullThe held lock, or null while locking or on error
isLockingbooleantrue while the lock request is in flight
lockErrorError | nullThe lock error (often LockConflictError)

Notes

  • The lock is released by calling unlock() in the useEffect cleanup. If the component unmounts before the lock call completes, the unlock is cancelled — no dangling lock.
  • If resource, id, or identity are all unavailable at mount time, the effect is skipped until they become available.
  • LockConflictError on lockError means another user holds the lock — check .existingLock.identity to display who.
  • For a declarative version, see <LockOnMount>.