happy-rusty
    Preparing search index...

    Interface RwLock<T>

    An async read-write lock for protecting shared data.

    This lock allows multiple concurrent readers or a single exclusive writer. Writers are given priority to prevent writer starvation.

    1.8.0

    const config = RwLock({ apiUrl: 'https://api.example.com', timeout: 5000 });

    // Multiple readers can access simultaneously
    async function getConfig() {
    const guard = await config.read();
    try {
    return { ...guard.value };
    } finally {
    guard.unlock();
    }
    }

    // Writers get exclusive access
    async function updateConfig(newConfig: Partial<Config>) {
    const guard = await config.write();
    try {
    Object.assign(guard.value, newConfig);
    } finally {
    guard.unlock();
    }
    }
    interface RwLock<T> {
        "[toStringTag]": "RwLock";
        get(): Promise<Awaited<T>>;
        isWriteLocked(): boolean;
        read(): Promise<RwLockReadGuard<T>>;
        readerCount(): number;
        replace(value: T): Promise<Awaited<T>>;
        set(value: T): Promise<void>;
        toString(): string;
        tryRead(): Option<RwLockReadGuard<T>>;
        tryWrite(): Option<RwLockWriteGuard<T>>;
        withRead<U>(fn: (value: T) => U | PromiseLike<U>): Promise<Awaited<U>>;
        withWrite<U>(fn: (value: T) => U | PromiseLike<U>): Promise<Awaited<U>>;
        write(): Promise<RwLockWriteGuard<T>>;
    }

    Type Parameters

    • T

      The type of the protected value.

    Index

    Properties

    "[toStringTag]": "RwLock"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString().

    Methods

    • Acquires a read lock and returns a copy of the protected value.

      Returns Promise<Awaited<T>>

      A promise that resolves to a copy of the value.

      const value = await rwlock.get();
      
    • Returns true if a writer currently holds the lock.

      Returns boolean

      console.log(rwlock.isWriteLocked()); // false
      const guard = await rwlock.write();
      console.log(rwlock.isWriteLocked()); // true
    • Acquires a read lock and returns a guard for manual control.

      Returns Promise<RwLockReadGuard<T>>

      A promise that resolves to a read guard.

      const guard = await rwlock.read();
      try {
      console.log(guard.value);
      } finally {
      guard.unlock();
      }
    • Returns the number of active readers.

      Returns number

      console.log(rwlock.readerCount()); // 0
      const guard = await rwlock.read();
      console.log(rwlock.readerCount()); // 1
    • Acquires a write lock, sets a new value, and returns the old value.

      Parameters

      • value: T

        The new value to set.

      Returns Promise<Awaited<T>>

      A promise that resolves to the old value.

      const rwlock = RwLock(42);
      const old = await rwlock.replace(100);
      console.log(old); // 42
      console.log(await rwlock.get()); // 100
    • Acquires a write lock and sets a new value.

      Parameters

      • value: T

        The new value to set.

      Returns Promise<void>

      A promise that resolves when the value has been set.

      await rwlock.set(newValue);
      
    • Custom toString implementation.

      Returns string

      const rwlock = RwLock(42);
      console.log(rwlock.toString()); // 'RwLock(<unlocked>)'
    • Attempts to acquire a read lock without waiting.

      Returns None if a write lock is currently held.

      Returns Option<RwLockReadGuard<T>>

      Some(guard) if acquired, None if a writer holds the lock.

      const maybeGuard = rwlock.tryRead();
      if (maybeGuard.isSome()) {
      const guard = maybeGuard.unwrap();
      try {
      console.log(guard.value);
      } finally {
      guard.unlock();
      }
      }
    • Attempts to acquire a write lock without waiting.

      Returns None if any read or write lock is currently held.

      Returns Option<RwLockWriteGuard<T>>

      Some(guard) if acquired, None if any lock is held.

      const maybeGuard = rwlock.tryWrite();
      if (maybeGuard.isSome()) {
      const guard = maybeGuard.unwrap();
      try {
      guard.value = newValue;
      } finally {
      guard.unlock();
      }
      }
    • Acquires a read lock and executes the callback with the protected value.

      Multiple read operations can execute concurrently.

      Type Parameters

      • U

        The return type of the callback.

      Parameters

      • fn: (value: T) => U | PromiseLike<U>

        The callback that receives the protected value (read-only).

      Returns Promise<Awaited<U>>

      A promise that resolves to the callback's return value.

      const data = await rwlock.withRead(async (value) => {
      return value.items.filter(item => item.active);
      });
    • Acquires a write lock and executes the callback with the protected value.

      Write operations are exclusive - no other reads or writes can proceed.

      Type Parameters

      • U

        The return type of the callback.

      Parameters

      • fn: (value: T) => U | PromiseLike<U>

        The callback that receives the protected value (read-write).

      Returns Promise<Awaited<U>>

      A promise that resolves to the callback's return value.

      await rwlock.withWrite(async (value) => {
      value.items.push(await fetchNewItem());
      });
    • Acquires a write lock and returns a guard for manual control.

      Returns Promise<RwLockWriteGuard<T>>

      A promise that resolves to a write guard.

      const guard = await rwlock.write();
      try {
      guard.value = newValue;
      } finally {
      guard.unlock();
      }