import type { PublicInterface } from "type-party"; import type Cache from "../Cache.js"; import type { CacheSpec, SpecForId } from "../types/00_CacheSpec.js"; import type { AnyParams, AnyValidators, Entry, RequestPairedProducerResult } from "../types/index.js"; import { type PartialConsumerRequest } from "./requestPairedProducerUtils.js"; import { type WrapProducerOptions } from "./wrapProducer.js"; /** * A bulk producer function that takes an array of consumer requests and returns * a promise for an array of request-paired producer results. * * The result type is a flat array of (RequestPairedProducerResult | * ErrorType): each element is a discriminated union over the cache's `Spec`, * so each (id, content) pair must internally agree, but the type system does * not require the i'th result element to align with the i'th request's id at * the call site (which would require gnarly mapped-tuple typing). When * `wrapBulkProducer` returns its results to the caller, they ARE narrowed * per-request via the wrapper's own generic. */ export type BulkProducer = (reqs: readonly PartialConsumerRequest[], options?: { signal?: AbortSignal; }) => Promise<(RequestPairedProducerResult | ErrorType)[]>; /** * Fundamentally, this function takes a function that returns values for * multiple requests (likely without the help of a cache), and returns another * function that's a drop-in replacement for the first, except that it tries to * lookup and reuse prior results from a cache using `Cache.getMany`, before * calling the underlying user-provided function only for those requests that * could not be resolved from the cache (or that need revalidation later). * * Note that this can call the underlying producer up to three times: once for * requests that had no immediately-usable cached values, once for requests that * are always uncacheable, and once (in the background) for requests that had * usableWhileRevalidate results and need to be revalidated in the background. * * Note that any supplemental resources returned by the producer will be * cached but not returned to the caller. * * The wrapped function is generic over the specific ids of incoming requests * so that, when `Spec` is a union of cache key shapes, each output slot's * content type is narrowed to the spec variants compatible with the * corresponding input request's id. * * ## AbortSignal support * * The returned function accepts an optional `{ signal }` parameter. Signal * propagation follows the same model as {@link wrapProducer}: * * - **Uncacheable requests**: the signal is forwarded directly to the producer. * * - **Cacheable requests**: the signal is forwarded to `cache.getMany()`. If * the signal fires before the cache read completes, the function throws * without contacting the producer. Once the cache read resolves and the * producer must be called for cache misses, the signal is **not** forwarded * to the producer (because those calls go through the * `collapsedTaskCreator`), but the caller's wait is raced against the signal * so they can bail out early. The producer's result is always stored. See the * `wrapProducer` JSDoc for the full rationale. * * - **Background refresh** (stale-while-revalidate): these calls are * fire-and-forget and never receive a signal, since the caller has already * been given a (stale) result. * * @param cache - An instance of the cache class. This is where values returned * by the producer (see below) will actually be stored. * * @param options - See `WrapProducerOptions` for details. * * @param producer - The function that's actually responsible for returning the * results that will be sent to the user and/or stored in the cache. It acts * as the origin or "producer" for the cache. This function is passed an array * of requests (id and params) along with the caller's cache directives, which * may be needed in case this producer function is itself backed by a cache, * and it needs to decide whether to contact its origin. */ export declare function wrapBulkProducer(cache: PublicInterface>, options: WrapProducerOptions | undefined, producer: BulkProducer): { []>(reqs: Reqs, options?: { signal?: AbortSignal; }): Promise<{ -readonly [K in keyof Reqs]: Entry, Validators, Params> | ErrorType; }>; cache: PublicInterface>; }; //# sourceMappingURL=wrapBulkProducer.d.ts.map