forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseLiveInfiniteQuery.ts
More file actions
307 lines (276 loc) · 9.66 KB
/
useLiveInfiniteQuery.ts
File metadata and controls
307 lines (276 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { CollectionImpl } from '@tanstack/db'
import { useLiveQuery } from './useLiveQuery'
import type {
Collection,
Context,
InferResultType,
InitialQueryBuilder,
LiveQueryCollectionUtils,
NonSingleResult,
QueryBuilder,
} from '@tanstack/db'
/**
* Type guard to check if utils object has setWindow method (LiveQueryCollectionUtils)
*/
function isLiveQueryCollectionUtils(
utils: unknown,
): utils is LiveQueryCollectionUtils {
return typeof (utils as any).setWindow === `function`
}
export type UseLiveInfiniteQueryConfig<TContext extends Context> = {
pageSize?: number
initialPageParam?: number
getNextPageParam: (
lastPage: Array<InferResultType<TContext>[number]>,
allPages: Array<Array<InferResultType<TContext>[number]>>,
lastPageParam: number,
allPageParams: Array<number>,
) => number | undefined
}
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: () => void
hasNextPage: boolean
isFetchingNextPage: boolean
}
/**
* 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 getNextPageParam
* @param deps - Array of dependencies that trigger query re-execution when changed
* @returns Object with pages, data, and pagination controls
*
* @example
* // Basic infinite query
* const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
* (q) => q
* .from({ posts: postsCollection })
* .orderBy(({ posts }) => posts.createdAt, 'desc')
* .select(({ posts }) => ({
* id: posts.id,
* title: posts.title
* })),
* {
* pageSize: 20,
* getNextPageParam: (lastPage, allPages) =>
* lastPage.length === 20 ? allPages.length : undefined
* }
* )
*
* @example
* // With dependencies
* const { pages, fetchNextPage } = useLiveInfiniteQuery(
* (q) => q
* .from({ posts: postsCollection })
* .where(({ posts }) => eq(posts.category, category))
* .orderBy(({ posts }) => posts.createdAt, 'desc'),
* {
* pageSize: 10,
* getNextPageParam: (lastPage) =>
* lastPage.length === 10 ? lastPage.length : undefined
* },
* [category]
* )
*
* @example
* // Router loader pattern with pre-created collection
* // In loader:
* const postsQuery = createLiveQueryCollection({
* query: (q) => q
* .from({ posts: postsCollection })
* .orderBy(({ posts }) => posts.createdAt, 'desc')
* .limit(20)
* })
* await postsQuery.preload()
* return { postsQuery }
*
* // In component:
* const { postsQuery } = useLoaderData()
* const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
* postsQuery,
* {
* pageSize: 20,
* getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
* }
* )
*/
// Overload for pre-created collection (non-single result)
export function useLiveInfiniteQuery<
TResult extends object,
TKey extends string | number,
TUtils extends Record<string, any>,
>(
liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult,
config: UseLiveInfiniteQueryConfig<any>,
): UseLiveInfiniteQueryReturn<any>
// Overload for query function
export function useLiveInfiniteQuery<TContext extends Context>(
queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
config: UseLiveInfiniteQueryConfig<TContext>,
deps?: Array<unknown>,
): UseLiveInfiniteQueryReturn<TContext>
// Implementation
export function useLiveInfiniteQuery<TContext extends Context>(
queryFnOrCollection: any,
config: UseLiveInfiniteQueryConfig<TContext>,
deps: Array<unknown> = [],
): UseLiveInfiniteQueryReturn<TContext> {
const pageSize = config.pageSize || 20
const initialPageParam = config.initialPageParam ?? 0
// Detect if input is a collection or query function
const isCollection = queryFnOrCollection instanceof CollectionImpl
// Validate input type
if (!isCollection && typeof queryFnOrCollection !== `function`) {
throw new Error(
`useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) ` +
`or a query function. Received: ${typeof queryFnOrCollection}`,
)
}
// Track how many pages have been loaded
const [loadedPageCount, setLoadedPageCount] = useState(1)
const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)
// Track collection instance and whether we've validated it (only for pre-created collections)
const collectionRef = useRef(isCollection ? queryFnOrCollection : null)
const hasValidatedCollectionRef = useRef(false)
// Track deps for query functions (stringify for comparison)
const depsKey = JSON.stringify(deps)
const prevDepsKeyRef = useRef(depsKey)
// Reset pagination when inputs change
useEffect(() => {
let shouldReset = false
if (isCollection) {
// Reset if collection instance changed
if (collectionRef.current !== queryFnOrCollection) {
collectionRef.current = queryFnOrCollection
hasValidatedCollectionRef.current = false
shouldReset = true
}
} else {
// Reset if deps changed (for query functions)
if (prevDepsKeyRef.current !== depsKey) {
prevDepsKeyRef.current = depsKey
shouldReset = true
}
}
if (shouldReset) {
setLoadedPageCount(1)
}
}, [isCollection, queryFnOrCollection, depsKey])
// Create a live query with initial limit and offset
// Either pass collection directly or wrap query function
// Use pageSize + 1 for peek-ahead detection (to know if there are more pages)
const queryResult = isCollection
? useLiveQuery(queryFnOrCollection)
: useLiveQuery(
(q) =>
queryFnOrCollection(q)
.limit(pageSize + 1)
.offset(0),
deps,
)
// Adjust window when pagination changes
useEffect(() => {
const utils = queryResult.collection.utils
const expectedOffset = 0
const expectedLimit = loadedPageCount * pageSize + 1 // +1 for peek ahead
// Check if collection has orderBy (required for setWindow)
if (!isLiveQueryCollectionUtils(utils)) {
// For pre-created collections, throw an error if no orderBy
if (isCollection) {
throw new Error(
`useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +
`Please add .orderBy() to your createLiveQueryCollection query.`,
)
}
return
}
// For pre-created collections, validate window on first check
if (isCollection && !hasValidatedCollectionRef.current) {
const currentWindow = utils.getWindow()
if (
currentWindow &&
(currentWindow.offset !== expectedOffset ||
currentWindow.limit !== expectedLimit)
) {
console.warn(
`useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} ` +
`but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`,
)
}
hasValidatedCollectionRef.current = true
}
// For query functions, wait until collection is ready
if (!isCollection && !queryResult.isReady) return
// Adjust the window
const result = utils.setWindow({
offset: expectedOffset,
limit: expectedLimit,
})
if (result !== true) {
setIsFetchingNextPage(true)
result.then(() => {
setIsFetchingNextPage(false)
})
} else {
setIsFetchingNextPage(false)
}
}, [
isCollection,
queryResult.collection,
queryResult.isReady,
loadedPageCount,
pageSize,
])
// Split the data array into pages and determine if there's a next page
const { pages, pageParams, hasNextPage, flatData } = useMemo(() => {
const dataArray = (
Array.isArray(queryResult.data) ? queryResult.data : []
) as InferResultType<TContext>
const totalItemsRequested = loadedPageCount * pageSize
// Check if we have more data than requested (the peek ahead item)
const hasMore = dataArray.length > totalItemsRequested
// Build pages array (without the peek ahead item)
const pagesResult: Array<Array<InferResultType<TContext>[number]>> = []
const pageParamsResult: Array<number> = []
for (let i = 0; i < loadedPageCount; i++) {
const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize)
pagesResult.push(pageData)
pageParamsResult.push(initialPageParam + i)
}
// Flatten the pages for the data return (without peek ahead item)
const flatDataResult = dataArray.slice(
0,
totalItemsRequested,
) as InferResultType<TContext>
return {
pages: pagesResult,
pageParams: pageParamsResult,
hasNextPage: hasMore,
flatData: flatDataResult,
}
}, [queryResult.data, loadedPageCount, pageSize, initialPageParam])
// Fetch next page
const fetchNextPage = useCallback(() => {
if (!hasNextPage || isFetchingNextPage) return
setLoadedPageCount((prev) => prev + 1)
}, [hasNextPage, isFetchingNextPage])
return {
...queryResult,
data: flatData,
pages,
pageParams,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
} as UseLiveInfiniteQueryReturn<TContext>
}