UNPKG

@tanstack/react-db

Version:

React integration for @tanstack/db

59 lines (58 loc) • 3.07 kB
import { LiveQueryKey, useLiveQuery } from './useLiveQuery.js'; import { Collection, CollectionStatus, Context, DbClient, InferResultType, InitialQueryBuilder, LiveQueryPersistedStatus, NonSingleResult, QueryBuilder } from '@tanstack/db'; export type UseLiveInfiniteQueryConfig<_TContext extends Context> = { /** * Explicit identity for queries that contain opaque functional variants or * are hot enough that deriving identity from structured IR is too expensive. * Structured queries should omit this so DB can derive identity directly. */ queryKey?: LiveQueryKey; /** Override the nearest DbProvider for this query. */ client?: DbClient; pageSize?: number; /** First result-page label, not a server cursor or remote offset. */ initialPageParam?: number; }; export type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<ReturnType<typeof useLiveQuery<TContext>>, `data`> & { data: InferResultType<TContext>; pages: Array<Array<InferResultType<TContext>[number]>>; pageParams: Array<number>; fetchNextPage: () => Promise<void>; hasNextPage: boolean; isFetchingNextPage: boolean; error: unknown; }; export type UseLiveInfiniteQueryReturnWithCollection<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>> = { data: Array<TResult>; state: Map<TKey, TResult>; collection: Collection<TResult, TKey, TUtils> & NonSingleResult; status: CollectionStatus; isLoading: boolean; isReady: boolean; persistedStatus: LiveQueryPersistedStatus; isPersistedReady: boolean; persistedError: unknown | undefined; isIdle: boolean; isError: boolean; isCleanedUp: boolean; isEnabled: true; pages: Array<Array<TResult>>; pageParams: Array<number>; fetchNextPage: () => Promise<void>; hasNextPage: boolean; isFetchingNextPage: boolean; error: unknown; }; /** * Create an infinite query using a query function with live updates. * * Uses `utils.setWindow()` to dynamically adjust the limit/offset window * without recreating the live query collection on each page change. * * @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work. * @param config - Configuration including pageSize and an optional initial page label * @param deps - Deprecated array of dependencies that trigger query re-execution when changed * @returns Object with pages, data, and pagination controls */ export declare function useLiveInfiniteQuery<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>>(liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult, config: UseLiveInfiniteQueryConfig<any>): UseLiveInfiniteQueryReturnWithCollection<TResult, TKey, TUtils>; export declare function useLiveInfiniteQuery<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>, config: UseLiveInfiniteQueryConfig<TContext>, deps?: Array<unknown>): UseLiveInfiniteQueryReturn<TContext>;