MCPcopy Index your code
hub / github.com/TanStack/db / useLiveInfiniteQuery

Function useLiveInfiniteQuery

packages/react-db/src/useLiveInfiniteQuery.ts:135–330  ·  view source on GitHub ↗
(
  queryFnOrCollection: any,
  config: UseLiveInfiniteQueryConfig<TContext>,
  deps: Array<unknown> = [],
)

Source from the content-addressed store, hash-verified

133
134// Implementation
135export function useLiveInfiniteQuery<TContext extends Context>(
136 queryFnOrCollection: any,
137 config: UseLiveInfiniteQueryConfig<TContext>,
138 deps: Array<unknown> = [],
139): UseLiveInfiniteQueryReturn<TContext> {
140 const pageSize = config.pageSize || 20
141 const initialPageParam = config.initialPageParam ?? 0
142
143 // Detect if input is a collection or query function
144 const isCollection = queryFnOrCollection instanceof CollectionImpl
145
146 // Validate input type
147 if (!isCollection && typeof queryFnOrCollection !== `function`) {
148 throw new Error(
149 `useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) ` +
150 `or a query function. Received: ${typeof queryFnOrCollection}`,
151 )
152 }
153
154 // Track how many pages have been loaded
155 const [loadedPageCount, setLoadedPageCount] = useState(1)
156 const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)
157
158 // Track collection instance and whether we've validated it (only for pre-created collections)
159 const collectionRef = useRef(isCollection ? queryFnOrCollection : null)
160 const hasValidatedCollectionRef = useRef(false)
161
162 // Track deps for query functions (stringify for comparison)
163 let depsKey: string
164 try {
165 depsKey = JSON.stringify(deps)
166 } catch {
167 throw new Error(
168 `useLiveInfiniteQuery: dependency array contains values that cannot be serialized (e.g. circular references). ` +
169 `Ensure all dependency values are JSON-serializable.`,
170 )
171 }
172 const prevDepsKeyRef = useRef(depsKey)
173
174 // Reset pagination when inputs change
175 useEffect(() => {
176 let shouldReset = false
177
178 if (isCollection) {
179 // Reset if collection instance changed
180 if (collectionRef.current !== queryFnOrCollection) {
181 collectionRef.current = queryFnOrCollection
182 hasValidatedCollectionRef.current = false
183 shouldReset = true
184 }
185 } else {
186 // Reset if deps changed (for query functions)
187 if (prevDepsKeyRef.current !== depsKey) {
188 prevDepsKeyRef.current = depsKey
189 shouldReset = true
190 }
191 }
192

Callers 1

Calls 6

useLiveQueryFunction · 0.90
offsetMethod · 0.80
limitMethod · 0.80
getWindowMethod · 0.80
setWindowMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…