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

Function useLiveQuery

packages/solid-db/src/useLiveQuery.ts:286–488  ·  view source on GitHub ↗
(
  configOrQueryOrCollection: (queryFn?: any) => any,
)

Source from the content-addressed store, hash-verified

284
285// Implementation - use function overloads to infer the actual collection type
286export function useLiveQuery(
287 configOrQueryOrCollection: (queryFn?: any) => any,
288) {
289 const collection = createMemo(
290 () => {
291 if (configOrQueryOrCollection.length === 1) {
292 // This is a query function - check if it returns null/undefined
293 const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder
294 const result = configOrQueryOrCollection(queryBuilder)
295
296 if (result === undefined || result === null) {
297 // Disabled query - return null
298 return null
299 }
300
301 return createLiveQueryCollection({
302 query: configOrQueryOrCollection,
303 startSync: true,
304 })
305 }
306
307 const innerCollection = configOrQueryOrCollection()
308
309 if (innerCollection === undefined || innerCollection === null) {
310 // Disabled query - return null
311 return null
312 }
313
314 if (innerCollection instanceof CollectionImpl) {
315 innerCollection.startSyncImmediate()
316 return innerCollection as Collection
317 }
318
319 return createLiveQueryCollection({
320 ...innerCollection,
321 startSync: true,
322 })
323 },
324 undefined,
325 { name: `TanstackDBCollectionMemo` },
326 )
327
328 // Reactive state that gets updated granularly through change events
329 const state = new ReactiveMap<string | number, any>()
330
331 // Reactive data array that maintains sorted order
332 const [data, setData] = createStore<Array<any>>([], {
333 name: `TanstackDBData`,
334 })
335
336 // Track collection status reactively
337 const [status, setStatus] = createSignal(
338 collection() ? collection()!.status : (`disabled` as const),
339 {
340 name: `TanstackDBStatus`,
341 },
342 )
343

Callers 6

TestComponentFunction · 0.90
QueryPageFunction · 0.90
TrailBasePageFunction · 0.90
ElectricPageFunction · 0.90

Calls 13

collectionFunction · 0.85
createEffectFunction · 0.85
startSyncImmediateMethod · 0.80
toArrayWhenReadyMethod · 0.80
syncDataFromCollectionFunction · 0.70
setStatusFunction · 0.50
clearMethod · 0.45
entriesMethod · 0.45
setMethod · 0.45
subscribeChangesMethod · 0.45
deleteMethod · 0.45

Tested by 1

TestComponentFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…