Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

useLiveInfiniteQuery.ts

166 lines5.4 KB

1import { computed, shallowRef, toValue, watchEffect } from 'vue'
2import {
3 bindLiveQueryWindow,
4 fetchNextLiveQueryWindowPage,
5 readLiveQueryWindowPageShape,
6} from '@tanstack/db'
7import type {
8 Collection,
9 CollectionStatus,
10 Context,
11 GetResult,
12 InferResultType,
13 InitialQueryBuilder,
14 LiveQueryWindowBinding,
15 NonSingleResult,
16 QueryBuilder,
17 UtilsRecord,
18} from '@tanstack/db'
19import type { UseLiveQueryFlags } from './useLiveQuery'
20import type { ComputedRef, MaybeRefOrGetter } from 'vue'
21​
22type InfiniteQueryOptions = {
23 pageSize?: number
24 /** First result-page label, not a server cursor or remote offset. */
25 initialPageParam?: number
27​
28// Keep the generic parameter for existing typed config wrappers.
29export type LiveInfiniteQueryConfig<_TRow> = InfiniteQueryOptions
30​
31export type UseLiveInfiniteQueryConfig<
32 TContext extends Context & NonSingleResult,
33> = LiveInfiniteQueryConfig<InferResultType<TContext>[number]>
34​
35export interface UseLiveInfiniteQueryReturn<
36 TContext extends Context & NonSingleResult,
37> extends UseLiveQueryFlags {
38 state: ComputedRef<Map<string | number, GetResult<TContext>>>
39 data: ComputedRef<InferResultType<TContext>>
40 collection: ComputedRef<
41 Collection<GetResult<TContext>, string | number, UtilsRecord>
42 >
43 pages: ComputedRef<Array<Array<InferResultType<TContext>[number]>>>
44 pageParams: ComputedRef<Array<number>>
45 fetchNextPage: () => Promise<void>
46 hasNextPage: ComputedRef<boolean>
47 isFetchingNextPage: ComputedRef<boolean>
49​
50export interface UseLiveInfiniteQueryReturnWithCollection<
51 TResult extends object,
52 TKey extends string | number,
53 TUtils extends UtilsRecord,
54> extends UseLiveQueryFlags {
55 state: ComputedRef<Map<TKey, TResult>>
56 data: ComputedRef<Array<TResult>>
57 collection: ComputedRef<Collection<TResult, TKey, TUtils>>
58 pages: ComputedRef<Array<Array<TResult>>>
59 pageParams: ComputedRef<Array<number>>
60 fetchNextPage: () => Promise<void>
61 hasNextPage: ComputedRef<boolean>
62 isFetchingNextPage: ComputedRef<boolean>
64​
65/**
66 * Create a Vue-native reactive view over the shared live-query window
67 * controller. The query must include an `orderBy` clause.
68 */
69export function useLiveInfiniteQuery<
70 TResult extends object,
71 TKey extends string | number,
72 TUtils extends UtilsRecord,
73>(
74 liveQueryCollection: MaybeRefOrGetter<
75 Collection<TResult, TKey, TUtils> & NonSingleResult
76 >,
77 config: LiveInfiniteQueryConfig<TResult>,
78): UseLiveInfiniteQueryReturnWithCollection<TResult, TKey, TUtils>
79​
80export function useLiveInfiniteQuery<
81 TContext extends Context & NonSingleResult,
82>(
83 queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
84 config: UseLiveInfiniteQueryConfig<TContext>,
85 deps?: Array<MaybeRefOrGetter<unknown>>,
86): UseLiveInfiniteQueryReturn<TContext>
87​
88export function useLiveInfiniteQuery<
89 TContext extends Context & NonSingleResult,
90>(
91 queryFnOrCollection: unknown,
92 config: InfiniteQueryOptions,
93 deps: Array<MaybeRefOrGetter<unknown>> = [],
94): UseLiveInfiniteQueryReturn<TContext> {
95 let binding: LiveQueryWindowBinding | null = null
96​
97 // Vue reruns this only when something it reads changed: the input, a
98 // dependency, the page shape, or a ref the query callback reads.
99 const controller = computed(() => {
100 const previous = binding
101 binding = bindLiveQueryWindow(previous, {
102 ...readLiveQueryWindowPageShape(config),
103 input:
104 typeof queryFnOrCollection === `function`
105 ? queryFnOrCollection
106 : toValue(queryFnOrCollection),
107 dependencies: deps.map((dependency) => toValue(dependency)),
108 recompile: `always`,
109 })
110 if (binding !== previous && binding.warning) console.warn(binding.warning)
111 return binding.controller
112 })
113​
114 const snapshot = shallowRef(controller.value.getSnapshot())
115​
116 watchEffect(
117 (onInvalidate) => {
118 const currentController = controller.value
119 const updateSnapshot = () => {
120 snapshot.value = currentController.getSnapshot()
121 }
122​
123 updateSnapshot()
124 const unsubscribe = currentController.subscribe(updateSnapshot)
125 updateSnapshot()
126​
127 onInvalidate(() => {
128 unsubscribe()
129 currentController.dispose()
130 })
131 },
132 { flush: `sync` },
133 )
134​
135 return {
136 state: computed(
137 () => snapshot.value.state as Map<string | number, GetResult<TContext>>,
138 ),
139 data: computed(() => snapshot.value.data as InferResultType<TContext>),
140 collection: computed(
141 () =>
142 snapshot.value.collection as Collection<
143 GetResult<TContext>,
144 string | number,
145 UtilsRecord
146 >,
147 ),
148 status: computed(() => snapshot.value.status as CollectionStatus),
149 isLoading: computed(() => snapshot.value.isLoading),
150 isReady: computed(() => snapshot.value.isReady),
151 isIdle: computed(() => snapshot.value.isIdle),
152 isLoadingError: computed(() => snapshot.value.isLoadingError),
153 isCrashed: computed(() => snapshot.value.isCrashed),
154 isCleanedUp: computed(() => snapshot.value.isCleanedUp),
155 isSyncError: computed(() => snapshot.value.isSyncError),
156 pages: computed(
157 () =>
158 snapshot.value.pages as Array<Array<InferResultType<TContext>[number]>>,
159 ),
160 pageParams: computed(() => snapshot.value.pageParams as Array<number>),
161 fetchNextPage: () => fetchNextLiveQueryWindowPage(controller.value),
162 hasNextPage: computed(() => snapshot.value.hasNextPage),
163 isFetchingNextPage: computed(() => snapshot.value.isFetchingNextPage),
164 health: computed(() => snapshot.value.health),
165 }