feat(svelte-db): add infinite query binding #1723
Mergedtannerlinsleymerged 3 commits into
main fromrefs/pull/1723/headmerged by KyleAMathews(GitHub profile, opens in a new tab) - @@ -0,0 +83,21 @@83+ initialPageParam?: number84+ /**85+ * @deprecated Pagination uses the shared controller's peek-ahead strategy.86+ * This remains for compatibility with TanStack Query conventions.87+ */88+ getNextPageParam?: (89+ lastPage: Array<InferResultType<TContext>[number]>,90+ allPages: Array<Array<InferResultType<TContext>[number]>>,91+ lastPageParam: number,92+ allPageParams: Array<number>,93+ ) => number | undefined94+}95+96+export type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<97+ ReturnType<typeof useLiveQuery<TContext>>,98+ `data`99+> & {100+ data: InferResultType<TContext>101+ pages: Array<Array<InferResultType<TContext>[number]>>102+ pageParams: Array<number>103+ fetchNextPage: () => void@@ -0,0 +106,21 @@106+ error: unknown107+}108+109+type EnabledLiveQueryReturn<TContext extends Context> = ReturnType<110+ typeof useLiveQuery<TContext>111+>112+113+/**114+ * Create a Svelte-native reactive view over the shared live-query window115+ * controller. The query must include an `orderBy` clause.116+ */117+export function useLiveInfiniteQuery<118+ TResult extends object,119+ TKey extends string | number,120+ TUtils extends Record<string, any>,121+>(122+ liveQueryCollection: MaybeGetter<123+ Collection<TResult, TKey, TUtils> & NonSingleResult124+ >,125+ config: UseLiveInfiniteQueryConfig<any>,126+): UseLiveInfiniteQueryReturn<any>@@ -0,0 +125,21 @@125+ config: UseLiveInfiniteQueryConfig<any>,126+): UseLiveInfiniteQueryReturn<any>127+128+export function useLiveInfiniteQuery<TContext extends Context>(129+ queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,130+ config: UseLiveInfiniteQueryConfig<TContext>,131+ deps?: Array<() => unknown>,132+): UseLiveInfiniteQueryReturn<TContext>133+134+export function useLiveInfiniteQuery<TContext extends Context>(135+ queryFnOrCollection: unknown,136+ config: UseLiveInfiniteQueryConfig<TContext>,137+ deps: Array<() => unknown> = [],138+): UseLiveInfiniteQueryReturn<TContext> {139+ let validatedCollection: Collection<any, any, any> | null = null140+141+ const pageSize = $derived(142+ config.pageSize !== undefined && config.pageSize > 0143+ ? config.pageSize144+ : DEFAULT_PAGE_SIZE,145+ )@@ -0,0 +162,21 @@162+ query: (q) =>163+ q164+ .from({ posts })165+ .orderBy(({ posts: post }) => post.createdAt, `desc`)166+ .limit(2)167+ .offset(1),168+ })169+ await livePosts.preload()170+ const warning = vi.spyOn(console, `warn`).mockImplementation(() => {})171+172+ cleanup = $effect.root(() => {173+ const query = useLiveInfiniteQuery(() => livePosts, { pageSize: 3 })174+ flushSync()175+176+ expect(query.collection).toBe(livePosts)177+ expect(query.data!.map((post: Post) => post.id)).toEqual([`1`, `2`, `3`])178+ expect(query.hasNextPage).toBe(true)179+ expect(warning).toHaveBeenCalledOnce()180+ expect(livePosts.utils.getWindow()).toEqual({ offset: 0, limit: 4 })181+ })182+ })
KyleAMathews(GitHub profile, opens in a new tab) approved these changes
KyleAMathews(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
29ffe0eb-39f9-49e4-b090-1addc0e24524📥 Commits
Reviewing files that changed from the base of the PR and between f1c24cd96daca0b6b66db78684b8a327324076a3 and 6d3395bfd3e551d1394853d7acd74e5c4799089e.
📒 Files selected for processing (1)
packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts🚧 Files skipped from review as they are similar to previous changes (1)
📝 Walkthrough
Walkthrough#
Changes#
Adds the Svelte
useLiveInfiniteQuerybinding over the shared live-query window controller. It supports query functions and pre-created ordered collections, exposes reactive pagination state, and includes tests and release metadata.Svelte live infinite query
packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts,packages/svelte-db/src/index.tspackages/svelte-db/src/useLiveInfiniteQuery.svelte.tspackages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts,.changeset/spicy-roses-hide.mdEstimated code review effort: 4 (Complex) | ~45 minutes
Mergeability Score: ⚪ Minimal · up to
6d339This PR adds the Svelte infinite-query binding and associated tests and release metadata; no actionable merge-blocking risk remains beyond normal checks and review.
Sequence Diagram(s)#
Possibly related PRs
createLiveQueryWindowControllerused by this binding.Suggested reviewers:
kyleamathews🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share