Chok’s React Utilities
    Preparing search index...

    Class SharedQuery<TArgs, TData>

    Please use sharedQuery() instead.

    Type Parameters

    • TArgs extends unknown[]
    • TData
    Index

    Constructors

    Properties

    expiryMs: number
    keepLastNonEmptyData: boolean
    log: (...args: unknown[]) => void
    maxBytes: number
    maxSize: number
    queryName: string
    refetchOnStale: boolean
    staleMs: number

    Methods

    • Delete all currently cached data & all inflight promises.

      Returns void

    • Delete the cached data for one set of arguments.

      Parameters

      • queryKey: string

      Returns void

    • Discard the oldest entries if the size exceeds the limit. The last remaining record cannot be deleted no matter what the limit is.

      Returns void

    • Get the AbortController to abort the inflight query (if any).

      Note that once you get the controller, you can also get the signal easily using controller.signal. So if you need both the controller and the signal, just use this function.

      Parameters

      Returns AbortController | null

    • Parameters

      • queryKey: string

      Returns AbortController | null

    • Get the AbortSignal to check for query abortions.

      Example:

        const getUserQuery = sharedQuery({
      queryName: "getUser",
      queryFn: (userId: string) => {
      // Get the signal for this current execution.
      const signal = getUserQuery.getAbortSignal([userId]);

      // Pass the signal to fetch so that it can be aborted.
      const response = await fetch(`/users/${userId}`, { signal });

      // Check for errors.
      if (!response.ok) {
      throw new Error(response.statusText);
      }

      // Return the data.
      return await response.json();
      },
      });

      Parameters

      Returns AbortSignal | null

    • Parameters

      • queryKey: string

      Returns AbortSignal | null

    • Get the current stored data for a query key.

      Parameters

      • queryKey: string

      Returns TData | undefined

    • Returns a key to identify requests based on the given args.

      Parameters

      Returns string

    • Get the entire stored entry for a query key.

      Parameters

      • queryKey: string

      Returns
          | Readonly<
              {
                  data?: TData;
                  dataUpdatedMs?: number;
                  error?: unknown;
                  errorUpdatedMs?: number;
                  lastUpdatedMs: number;
                  loading: boolean;
              },
          >
          | undefined

    • Keep track of mounted keys.

      Parameters

      • queryKey: string

      Returns void

    • Set the data directly if the user obtained it from somewhere else.

      Parameters

      • queryKey: string
      • data: TData
      • dataUpdatedMs: number = ...

      Returns void

    • Clean up mounted keys.

      Parameters

      • queryKey: string

      Returns void

    • Do a new fetch even when the data is already cached, but don't fetch if another fetch is already inflight.

      Parameters

      Returns Promise<TData>