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

Function useLiveQuery

packages/vue-db/src/useLiveQuery.ts:247–478  ·  view source on GitHub ↗
(
  configOrQueryOrCollection: any,
  deps: Array<MaybeRefOrGetter<unknown>> = [],
)

Source from the content-addressed store, hash-verified

245
246// Implementation
247export function useLiveQuery(
248 configOrQueryOrCollection: any,
249 deps: Array<MaybeRefOrGetter<unknown>> = [],
250): UseLiveQueryReturn<any> | UseLiveQueryReturnWithCollection<any, any, any> {
251 const collection = computed(() => {
252 // First check if the original parameter might be a ref/getter
253 // by seeing if toValue returns something different than the original
254 // NOTE: Don't call toValue on functions - toValue treats functions as getters and calls them!
255 let unwrappedParam = configOrQueryOrCollection
256 if (typeof configOrQueryOrCollection !== `function`) {
257 try {
258 const potentiallyUnwrapped = toValue(configOrQueryOrCollection)
259 if (potentiallyUnwrapped !== configOrQueryOrCollection) {
260 unwrappedParam = potentiallyUnwrapped
261 }
262 } catch {
263 // If toValue fails, use original parameter
264 unwrappedParam = configOrQueryOrCollection
265 }
266 }
267
268 // Check if it's already a collection by checking for specific collection methods
269 const isCollection =
270 unwrappedParam &&
271 typeof unwrappedParam === `object` &&
272 typeof unwrappedParam.subscribeChanges === `function` &&
273 typeof unwrappedParam.startSyncImmediate === `function` &&
274 typeof unwrappedParam.id === `string`
275
276 if (isCollection) {
277 // Warn when passing a collection directly with on-demand sync mode
278 // In on-demand mode, data is only loaded when queries with predicates request it
279 // Passing the collection directly doesn't provide any predicates, so no data loads
280 const syncMode = (unwrappedParam as { config?: { syncMode?: string } })
281 .config?.syncMode
282 if (syncMode === `on-demand`) {
283 console.warn(
284 `[useLiveQuery] Warning: Passing a collection with syncMode "on-demand" directly to useLiveQuery ` +
285 `will not load any data. In on-demand mode, data is only loaded when queries with predicates request it.\n\n` +
286 `Instead, use a query builder function:\n` +
287 ` const { data } = useLiveQuery((q) => q.from({ c: myCollection }).select(({ c }) => c))\n\n` +
288 `Or switch to syncMode "eager" if you want all data to sync automatically.`,
289 )
290 }
291 // It's already a collection, ensure sync is started for Vue hooks
292 // Only start sync if the collection is in idle state
293 if (unwrappedParam.status === `idle`) {
294 unwrappedParam.startSyncImmediate()
295 }
296 return unwrappedParam
297 }
298
299 // Reference deps to make computed reactive to them
300 deps.forEach((dep) => toValue(dep))
301
302 // Ensure we always start sync for Vue hooks
303 if (typeof unwrappedParam === `function`) {
304 // To avoid calling the query function twice, we wrap it to handle null/undefined returns

Callers 2

Calls 13

toValueFunction · 0.85
startSyncImmediateMethod · 0.80
syncDataFromCollectionFunction · 0.70
refFunction · 0.50
forEachMethod · 0.45
clearMethod · 0.45
entriesMethod · 0.45
setMethod · 0.45
onFirstReadyMethod · 0.45
subscribeChangesMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…