fynjs API
    Preparing search index...

    Class Inflight<T>

    Inflight manages deduplication of asynchronous operations by key. It ensures only one promise per key is active at a time, returning the same promise for concurrent requests. Useful for avoiding duplicate network or resource-intensive calls.

    Adds promise semantics to InflightStore; the keyed storage and time tracking are inherited, so there is one implementation of them (FJM-26).

    const inflight = new Inflight<Response>();

    // Multiple concurrent calls with same key return the same promise
    const [a, b] = await Promise.all([
    inflight.promise("user:123", () => fetch("/api/user/123")),
    inflight.promise("user:123", () => fetch("/api/user/123")),
    ]);
    // Only one fetch was made, a === b

    Type Parameters

    • T = unknown

      The type of the resolved value of the managed promises.

    Hierarchy (View Summary)

    Index
    • Create a new Inflight instance.

      Uses native Promise by default. Pass a custom Promise implementation if needed:

      import AveAzul from "aveazul";
      const inflight = new Inflight(AveAzul);

      Type Parameters

      • T = unknown

        The type of the resolved value of the managed promises.

      Parameters

      • OptionalPromiseImpl: PromiseConstructor

        Optional custom Promise implementation to use.

      Returns Inflight<T>

    _inflights: Map<RecordKey, InflightItem<Promise<T>>> = ...

    Map of inflight items by key

    Promise: PromiseConstructor

    The Promise implementation used by this instance

    • get count(): number

      The number of inflight items.

      Returns number

    • get isEmpty(): boolean

      Whether there are no inflight items.

      Returns boolean

    • Track a value under a key.

      Parameters

      • key: RecordKey
      • value: Promise
      • Optionalnow: number

      Returns Promise

      If key already exists.

    • Iterate the tracked items as [key, record] pairs.

      Prefer this over reaching for the internal map - it is what lets a caller sweep every in-progress entry, which is what item-queue's overdue-work watcher does.

      Returns IterableIterator<[RecordKey, InflightItem<Promise<T>>]>

    • Get the tracked value for a key, or undefined.

      Parameters

      Returns Promise<T>

    • Elapsed ms since the last check for a key, or -1 if not found.

      Parameters

      Returns number

    • Get or create a promise for a given key. If a promise for the key is already inflight, returns it. Otherwise, calls the provided factory to create a new promise, tracks it, and returns it.

      Parameters

      Returns Promise<T>

    • Remove the inflight item for a key.

      Parameters

      Returns void

      If key does not exist.

    • Reset the last check time for a key, or for all inflight items if no key is given.

      Parameters

      Returns this

      This instance for chaining.

    • Elapsed ms since the start for a key, or -1 if not found.

      Parameters

      Returns number