@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
103 lines • 5.03 kB
text/typescript
import { t as Subscribable } from "./subscribable-czlHOxMP.cjs";
import { $n as Query, At as QueryObserverOptions, Pt as QueryObserverResult, on as QueryClient, rr as QueryObserver } from "./hydration-C6zghADG.cjs";
//#region src/queriesObserver.d.ts
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
/**
* Options for a `QueriesObserver` that apply to all of its queries at once.
*/
export interface QueriesObserverOptions<TCombinedResult = Array<QueryObserverResult>> {
/**
* A function that combines the array of `QueryObserverResult`s (one per
* observed query) into a single value. The combined value is memoized and
* only recomputed when one of the underlying results, the query hashes, or
* the `combine` function itself changes.
*
* Defaults to returning the array of `QueryObserverResult`s unchanged.
*/
combine?: CombineFn<TCombinedResult>;
}
/**
* A `QueriesObserver` watches an array of queries at once, exposing them as
* a single array of `QueryObserverResult`s (or, when a `combine` option is
* given, as a combined value derived from that array). It manages one
* internal `QueryObserver` per query, and is the primitive that framework
* adapters (e.g. `useQueries`) build their hooks on top of.
* @example
* ```ts
* const observer = new QueriesObserver(queryClient, [
* { queryKey: ['post', 1], queryFn: fetchPost },
* { queryKey: ['post', 2], queryFn: fetchPost },
* ])
*
* const unsubscribe = observer.subscribe((result) => {
* console.log(result)
* })
* ```
*/
export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult>> extends Subscribable<QueriesObserverListener> {
#private;
constructor(client: QueryClient, queries: Array<QueryObserverOptions<any, any, any, any, any>>, options?: QueriesObserverOptions<TCombinedResult>);
protected onSubscribe(): void;
protected onUnsubscribe(): void;
/**
* Stops observing all queries: clears all listeners and destroys every
* underlying `QueryObserver` this observer manages.
*/
destroy(): void;
/**
* Replaces the set of queries being observed. Existing `QueryObserver`s
* are reused for queries that match an already-observed query hash;
* observers for queries that are no longer present are destroyed, and new
* observers are created and subscribed to for newly added queries.
* @param queries - The options of the queries to observe.
* @param options - Replaces the observer's options, e.g. its `combine` function.
* @example
* ```ts
* observer.setQueries([
* { queryKey: ['post', 1], queryFn: fetchPost },
* { queryKey: ['post', 3], queryFn: fetchPost },
* ])
* ```
*/
setQueries(queries: Array<QueryObserverOptions>, options?: QueriesObserverOptions<TCombinedResult>): void;
/**
* Returns the most recently computed array of `QueryObserverResult`s, one
* per observed query, in the same order as the queries passed to the
* constructor or `setQueries`.
* @returns The current results.
* @example
* ```ts
* const results = observer.getCurrentResult()
* const data = results.map((result) => result.data)
* ```
*/
getCurrentResult(): Array<QueryObserverResult>;
/**
* Returns the underlying `Query` instances currently being observed, in
* the same order as the queries passed to the constructor or `setQueries`.
* @returns The observed queries.
*/
getQueries(): Query<unknown, Error, unknown, readonly unknown[]>[];
/**
* Returns the underlying `QueryObserver` instances this observer manages,
* in the same order as the queries passed to the constructor or
* `setQueries`.
* @returns The managed query observers.
*/
getObservers(): QueryObserver<unknown, Error, unknown, unknown, readonly unknown[]>[];
/**
* The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes
* the result for the given (already-defaulted) queries right now, synchronously. Called by
* framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
* per-query results, a function to compute the combined result from them, and a function to
* wrap the results for property-access tracking.
* @param queries - The defaulted options of the queries to compute the result for.
* @param combine - The `combine` function used by the returned `combineResult`, if any.
* @returns A tuple of the per-query results, a function that computes the combined result, and a
* function that returns the results wrapped for property-access tracking.
*/
getOptimisticResult(queries: Array<QueryObserverOptions>, combine: CombineFn<TCombinedResult> | undefined): [rawResult: Array<QueryObserverResult>, combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult, trackResult: () => Array<QueryObserverResult>];
}
//#endregion
//# sourceMappingURL=queriesObserver.d.cts.map