forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseLiveSuspenseQuery.ts
More file actions
219 lines (207 loc) · 7.22 KB
/
useLiveSuspenseQuery.ts
File metadata and controls
219 lines (207 loc) · 7.22 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
import { useRef } from 'react'
import { useLiveQuery } from './useLiveQuery'
import type {
Collection,
Context,
GetResult,
InferResultType,
InitialQueryBuilder,
LiveQueryCollectionConfig,
NonSingleResult,
QueryBuilder,
SingleResult,
} from '@tanstack/db'
/**
* Create a live query with React Suspense support
* @param queryFn - Query function that defines what data to fetch
* @param deps - Array of dependencies that trigger query re-execution when changed
* @returns Object with reactive data and state - data is guaranteed to be defined
* @throws Promise when data is loading (caught by Suspense boundary)
* @throws Error when collection fails (caught by Error boundary)
* @example
* // Basic usage with Suspense
* function TodoList() {
* const { data } = useLiveSuspenseQuery((q) =>
* q.from({ todos: todosCollection })
* .where(({ todos }) => eq(todos.completed, false))
* .select(({ todos }) => ({ id: todos.id, text: todos.text }))
* )
*
* return (
* <ul>
* {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
* </ul>
* )
* }
*
* function App() {
* return (
* <Suspense fallback={<div>Loading...</div>}>
* <TodoList />
* </Suspense>
* )
* }
*
* @example
* // Single result query
* const { data } = useLiveSuspenseQuery(
* (q) => q.from({ todos: todosCollection })
* .where(({ todos }) => eq(todos.id, 1))
* .findOne()
* )
* // data is guaranteed to be the single item (or undefined if not found)
*
* @example
* // With dependencies that trigger re-suspension
* const { data } = useLiveSuspenseQuery(
* (q) => q.from({ todos: todosCollection })
* .where(({ todos }) => gt(todos.priority, minPriority)),
* [minPriority] // Re-suspends when minPriority changes
* )
*
* @example
* // With Error boundary
* function App() {
* return (
* <ErrorBoundary fallback={<div>Error loading data</div>}>
* <Suspense fallback={<div>Loading...</div>}>
* <TodoList />
* </Suspense>
* </ErrorBoundary>
* )
* }
*
* @remarks
* **Important:** This hook does NOT support disabled queries (returning undefined/null).
* Following TanStack Query's useSuspenseQuery design, the query callback must always
* return a valid query, collection, or config object.
*
* ❌ **This will cause a type error:**
* ```ts
* useLiveSuspenseQuery(
* (q) => userId ? q.from({ users }) : undefined // ❌ Error!
* )
* ```
*
* ✅ **Use conditional rendering instead:**
* ```ts
* function Profile({ userId }: { userId: string }) {
* const { data } = useLiveSuspenseQuery(
* (q) => q.from({ users }).where(({ users }) => eq(users.id, userId))
* )
* return <div>{data.name}</div>
* }
*
* // In parent component:
* {userId ? <Profile userId={userId} /> : <div>No user</div>}
* ```
*
* ✅ **Or use useLiveQuery for conditional queries:**
* ```ts
* const { data, isEnabled } = useLiveQuery(
* (q) => userId ? q.from({ users }) : undefined, // ✅ Supported!
* [userId]
* )
* ```
*/
// Overload 1: Accept query function that always returns QueryBuilder
export function useLiveSuspenseQuery<TContext extends Context>(
queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
deps?: Array<unknown>,
): {
state: Map<string | number, GetResult<TContext>>
data: InferResultType<TContext>
collection: Collection<GetResult<TContext>, string | number, {}>
}
// Overload 2: Accept config object
export function useLiveSuspenseQuery<TContext extends Context>(
config: LiveQueryCollectionConfig<TContext>,
deps?: Array<unknown>,
): {
state: Map<string | number, GetResult<TContext>>
data: InferResultType<TContext>
collection: Collection<GetResult<TContext>, string | number, {}>
}
// Overload 3: Accept pre-created live query collection
export function useLiveSuspenseQuery<
TResult extends object,
TKey extends string | number,
TUtils extends Record<string, any>,
>(
liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult,
): {
state: Map<TKey, TResult>
data: Array<TResult>
collection: Collection<TResult, TKey, TUtils>
}
// Overload 4: Accept pre-created live query collection with singleResult: true
export function useLiveSuspenseQuery<
TResult extends object,
TKey extends string | number,
TUtils extends Record<string, any>,
>(
liveQueryCollection: Collection<TResult, TKey, TUtils> & SingleResult,
): {
state: Map<TKey, TResult>
data: TResult | undefined
collection: Collection<TResult, TKey, TUtils> & SingleResult
}
// Implementation - uses useLiveQuery internally and adds Suspense logic
export function useLiveSuspenseQuery(
configOrQueryOrCollection: any,
deps: Array<unknown> = [],
) {
const promiseRef = useRef<Promise<void> | null>(null)
const collectionRef = useRef<Collection<any, any, any> | null>(null)
const hasBeenReadyRef = useRef(false)
// Use useLiveQuery to handle collection management and reactivity
const result = useLiveQuery(configOrQueryOrCollection, deps)
// Reset promise and ready state when collection changes (deps changed)
if (collectionRef.current !== result.collection) {
promiseRef.current = null
collectionRef.current = result.collection
hasBeenReadyRef.current = false
}
// Track when we reach ready state
if (result.status === `ready`) {
hasBeenReadyRef.current = true
promiseRef.current = null
}
// SUSPENSE LOGIC: Throw promise or error based on collection status
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!result.isEnabled) {
// Suspense queries cannot be disabled - this matches TanStack Query's useSuspenseQuery behavior
throw new Error(
`useLiveSuspenseQuery does not support disabled queries (callback returned undefined/null). ` +
`The Suspense pattern requires data to always be defined (T, not T | undefined). ` +
`Solutions: ` +
`1) Use conditional rendering - don't render the component until the condition is met. ` +
`2) Use useLiveQuery instead, which supports disabled queries with the 'isEnabled' flag.`,
)
}
// Only throw errors during initial load (before first ready)
// After success, errors surface as stale data (matches TanStack Query behavior)
if (result.status === `error` && !hasBeenReadyRef.current) {
promiseRef.current = null
// TODO: Once collections hold a reference to their last error object (#671),
// we should rethrow that actual error instead of creating a generic message
throw new Error(`Collection "${result.collection.id}" failed to load`)
}
if (result.status === `loading` || result.status === `idle`) {
// Create or reuse promise for current collection
if (!promiseRef.current) {
promiseRef.current = result.collection.preload()
}
// THROW PROMISE - React Suspense catches this (React 18+ required)
// Note: We don't check React version here. In React <18, this will be caught
// by an Error Boundary, which provides a reasonable failure mode.
throw promiseRef.current
}
// Return data without status/loading flags (handled by Suspense/ErrorBoundary)
// If error after success, return last known good state (stale data)
return {
state: result.state,
data: result.data,
collection: result.collection,
}
}