( configOrQueryOrCollection: any, deps: Array<() => unknown> = [], )
| 283 | |
| 284 | // Implementation |
| 285 | export function useLiveQuery( |
| 286 | configOrQueryOrCollection: any, |
| 287 | deps: Array<() => unknown> = [], |
| 288 | ): UseLiveQueryReturn<any> | UseLiveQueryReturnWithCollection<any, any, any> { |
| 289 | const collection = $derived.by(() => { |
| 290 | // First check if the original parameter might be a getter |
| 291 | // by seeing if toValue returns something different than the original |
| 292 | let unwrappedParam = configOrQueryOrCollection |
| 293 | try { |
| 294 | const potentiallyUnwrapped = toValue(configOrQueryOrCollection) |
| 295 | if (potentiallyUnwrapped !== configOrQueryOrCollection) { |
| 296 | unwrappedParam = potentiallyUnwrapped |
| 297 | } |
| 298 | } catch { |
| 299 | // If toValue fails, use original parameter |
| 300 | unwrappedParam = configOrQueryOrCollection |
| 301 | } |
| 302 | |
| 303 | // Check if it's already a collection by checking for specific collection methods |
| 304 | const isCollection = |
| 305 | unwrappedParam && |
| 306 | typeof unwrappedParam === `object` && |
| 307 | typeof unwrappedParam.subscribeChanges === `function` && |
| 308 | typeof unwrappedParam.startSyncImmediate === `function` && |
| 309 | typeof unwrappedParam.id === `string` |
| 310 | |
| 311 | if (isCollection) { |
| 312 | // Warn when passing a collection directly with on-demand sync mode |
| 313 | // In on-demand mode, data is only loaded when queries with predicates request it |
| 314 | // Passing the collection directly doesn't provide any predicates, so no data loads |
| 315 | const syncMode = (unwrappedParam as { config?: { syncMode?: string } }) |
| 316 | .config?.syncMode |
| 317 | if (syncMode === `on-demand`) { |
| 318 | console.warn( |
| 319 | `[useLiveQuery] Warning: Passing a collection with syncMode "on-demand" directly to useLiveQuery ` + |
| 320 | `will not load any data. In on-demand mode, data is only loaded when queries with predicates request it.\n\n` + |
| 321 | `Instead, use a query builder function:\n` + |
| 322 | ` const { data } = useLiveQuery((q) => q.from({ c: myCollection }).select(({ c }) => c))\n\n` + |
| 323 | `Or switch to syncMode "eager" if you want all data to sync automatically.`, |
| 324 | ) |
| 325 | } |
| 326 | // It's already a collection, ensure sync is started for Svelte helpers |
| 327 | // Only start sync if the collection is in idle state |
| 328 | if (unwrappedParam.status === `idle`) { |
| 329 | unwrappedParam.startSyncImmediate() |
| 330 | } |
| 331 | return unwrappedParam |
| 332 | } |
| 333 | |
| 334 | // Reference deps to make computed reactive to them |
| 335 | deps.forEach((dep) => toValue(dep)) |
| 336 | |
| 337 | // Ensure we always start sync for Svelte helpers |
| 338 | if (typeof unwrappedParam === `function`) { |
| 339 | // Check if query function returns null/undefined (disabled query) |
| 340 | const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder |
| 341 | const result = unwrappedParam(queryBuilder) |
| 342 |
no test coverage detected