UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

35 lines (34 loc) 1.71 kB
import type { AnyCaller } from "../../util/function.js"; import type { Endpoint } from "../endpoint/Endpoint.js"; import type { APIProvider } from "../provider/APIProvider.js"; import { EndpointStore } from "../store/EndpointStore.js"; /** * Cache of `EndpointStore` objects for a single endpoint, keyed by serialized payload. * - Use `get(payload)` to retrieve or create the `EndpointStore` for a given payload. */ export declare class EndpointCache<P = unknown, R = unknown> implements AsyncDisposable { private readonly _endpoints; readonly endpoint: Endpoint<P, R>; readonly provider: APIProvider<P, R>; constructor(endpoint: Endpoint<P, R>, provider: APIProvider<P, R>); /** Get (or create) the `EndpointStore` for the given payload. */ get(payload: P, caller?: AnyCaller): EndpointStore<P, R>; /** * Fetch (or return a cached result) for the given payload. * - Returns the cached value immediately if one exists. * - Waits for the in-flight fetch if the store is loading. * - Throws if the fetch fails, matching `APIProvider.call` behaviour. * * @param maxAge The maximum age (defaults to only refreshing if the value is still in a loading state). */ call(payload: P, maxAge?: number, caller?: AnyCaller): Promise<R>; /** Invalidate a specific store. */ invalidate(payload: P, caller?: AnyCaller): void; /** Invalidate all stores. */ invalidateAll(): void; /** Trigger a refetch on a specific store. */ refresh(payload: P, maxAge?: number, caller?: AnyCaller): Promise<void>; /** Trigger a refetch on all stores. */ refreshAll(maxAge?: number): Promise<void>; [Symbol.asyncDispose](): Promise<void>; }