@adviser/cement - v0.0.0
    Preparing search index...

    Class KeyedResolvOnce<T, K, CTX, PT>

    Keyed collection of ResolveOnce instances.

    Manages a map of ResolveOnce instances indexed by keys, with optional LRU caching. Each key gets its own ResolveOnce instance that can be accessed and manipulated independently. Values can optionally have a reset() method for cleanup on deletion.

    const cache = new KeyedResolvOnce<number, string>();

    // Each key gets its own ResolveOnce
    const result1 = cache.get('key1').once(() => expensiveCalc1());
    const result2 = cache.get('key2').once(() => expensiveCalc2());

    // Delete specific key
    cache.delete('key1');

    // Iterate over all resolved entries
    cache.forEach((item) => {
    console.log(item.key, item.value.Ok);
    });
    // With custom key type and context
    interface UserKey { org: string; id: string; }
    interface UserContext { apiKey: string; }

    const users = new KeyedResolvOnce<User, UserKey, UserContext>({
    key2string: (key) => `${key.org}:${key.id}`,
    ctx: { apiKey: 'default' },
    lru: { max: 100 }
    });

    const user = users.get({ org: 'acme', id: '123' })
    .once(({ givenKey, ctx }) => fetchUser(givenKey, ctx));

    Type Parameters

    • T extends WithOptionalReset<PT>

      The return type of the ResolveOnce instances (must include optional reset)

    • K = string

      The key type (defaults to string)

    • CTX extends NonNullable<object> = object

      Optional context type (defaults to empty object)

    • PT = T

      Plain type of T without reset (for internal use)

    Implements

    Index

    Constructors

    Properties

    _keyed: KeyedNg<
        K,
        ResolveOnce<WithOptionalReset<T>, KeyedNgItemWithoutValue<K, CTX>>,
        CTX,
    >

    Methods

    • Returns all keys currently in the collection.

      Returns K[]

      Array of all keys

    • Returns all resolved items with their values wrapped in Result.

      Only includes items that have been resolved (ready state). Each item contains the key, Result-wrapped value, and full item metadata.

      Returns KeyedResolveOnceItem<K, T, CTX>[]

      Array of all resolved items

      const items = cache.values();
      items.forEach(({ key, value }) => {
      if (value.Ok) {
      console.log(key, value.unwrap());
      } else {
      console.error(key, value.unwrapErr());
      }
      });
    • Gets or creates a ResolveOnce instance for the given key.

      This is the primary method for accessing ResolveOnce instances. Each unique key gets its own instance that persists across calls.

      Parameters

      • key: K | (() => K)

        The key or function returning the key

      • Optionalctx: CTX

        Optional context override for this operation

      Returns ResolveOnce<T, KeyedNgItemWithoutValue<K, CTX>>

      The ResolveOnce instance for this key

      const result = cache.get('myKey').once(({ refKey, givenKey, ctx }) => {
      return computeValue(givenKey, ctx);
      });
    • Checks if a key exists in the collection.

      Parameters

      • key: K | (() => K)

        The key or function returning the key

      Returns boolean

      True if the key exists

    • Deletes an entry from the collection.

      Triggers onDelete callbacks before removal.

      Parameters

      • key: K

        The key to delete

      Returns void

    • Resets and removes an entry from the collection.

      Calls the optional reset() method on the value before deletion, allowing for cleanup operations.

      Parameters

      • key: K

        The key to reset and delete

      Returns void

    • Resets all entries by calling their optional reset() methods.

      Does not remove entries from the collection, only resets their state. Useful for cleanup without losing the collection structure.

      Returns void

    • Iterates over all completed entries, yielding key-result pairs.

      Only yields entries that have been resolved (ready state). Values are wrapped in Result to distinguish success from error.

      Parameters

      • fn: (ki: KeyItem<K, T>, idx: number) => void

        Callback receiving KeyItem and index

      Returns void

      cache.forEach((item, idx) => {
      console.log(idx, item.key);
      if (item.value.Ok) {
      console.log('Success:', item.value.unwrap());
      } else {
      console.error('Error:', item.value.unwrapErr());
      }
      });
    • Returns an iterable of all completed entries.

      Only yields entries that have been resolved. Values are wrapped in Result.

      Returns Iterable<KeyItem<K, T>>

      Iterable of KeyItem entries

      for (const item of cache.entries()) {
      console.log(item.key, item.value.Ok);
      }