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

infiniteQueryObserver.ts

184 lines4.6 KB

1import { QueryObserver } from './queryObserver'
2import { hasNextPage, hasPreviousPage } from './infiniteQueryBehavior'
3import type { Subscribable } from './subscribable'
4import type {
5 DefaultError,
6 DefaultedInfiniteQueryObserverOptions,
7 FetchNextPageOptions,
8 FetchPreviousPageOptions,
9 InfiniteData,
10 InfiniteQueryObserverBaseResult,
11 InfiniteQueryObserverOptions,
12 InfiniteQueryObserverResult,
13 QueryKey,
14} from './types'
15import type { QueryClient } from './queryClient'
16import type { Query } from './query'
17​
18type InfiniteQueryObserverListener<TData, TError> = (
19 result: InfiniteQueryObserverResult<TData, TError>,
20) => void
21​
22export class InfiniteQueryObserver<
23 TQueryFnData = unknown,
24 TError = DefaultError,
25 TData = InfiniteData<TQueryFnData>,
26 TQueryKey extends QueryKey = QueryKey,
27 TPageParam = unknown,
28> extends QueryObserver<
29 TQueryFnData,
30 TError,
31 TData,
32 InfiniteData<TQueryFnData, TPageParam>,
33 TQueryKey
34> {
35 // Type override
36 subscribe!: Subscribable<
37 InfiniteQueryObserverListener<TData, TError>
38 >['subscribe']
39​
40 // Type override
41 getCurrentResult!: ReplaceReturnType<
42 QueryObserver<
43 TQueryFnData,
44 TError,
45 TData,
46 InfiniteData<TQueryFnData, TPageParam>,
47 TQueryKey
48 >['getCurrentResult'],
49 InfiniteQueryObserverResult<TData, TError>
50 >
51​
52 // Type override
53 protected fetch!: ReplaceReturnType<
54 QueryObserver<
55 TQueryFnData,
56 TError,
57 TData,
58 InfiniteData<TQueryFnData, TPageParam>,
59 TQueryKey
60 >['fetch'],
61 Promise<InfiniteQueryObserverResult<TData, TError>>
62 >
63​
64 constructor(
65 client: QueryClient,
66 options: InfiniteQueryObserverOptions<
67 TQueryFnData,
68 TError,
69 TData,
70 TQueryKey,
71 TPageParam
72 >,
73 ) {
74 super(client, options)
75 }
76​
77 protected bindMethods(): void {
78 super.bindMethods()
79 this.fetchNextPage = this.fetchNextPage.bind(this)
80 this.fetchPreviousPage = this.fetchPreviousPage.bind(this)
81 }
82​
83 setOptions(
84 options: InfiniteQueryObserverOptions<
85 TQueryFnData,
86 TError,
87 TData,
88 TQueryKey,
89 TPageParam
90 >,
91 ): void {
92 options._type = 'infinite'
93 super.setOptions(options)
94 }
95​
96 getOptimisticResult(
97 options: DefaultedInfiniteQueryObserverOptions<
98 TQueryFnData,
99 TError,
100 TData,
101 TQueryKey,
102 TPageParam
103 >,
104 ): InfiniteQueryObserverResult<TData, TError> {
105 options._type = 'infinite'
106 return super.getOptimisticResult(options) as InfiniteQueryObserverResult<
107 TData,
108 TError
109 >
110 }
111​
112 fetchNextPage(
113 options?: FetchNextPageOptions,
114 ): Promise<InfiniteQueryObserverResult<TData, TError>> {
115 return this.fetch({
116 ...options,
117 meta: {
118 fetchMore: { direction: 'forward' },
119 },
120 })
121 }
122​
123 fetchPreviousPage(
124 options?: FetchPreviousPageOptions,
125 ): Promise<InfiniteQueryObserverResult<TData, TError>> {
126 return this.fetch({
127 ...options,
128 meta: {
129 fetchMore: { direction: 'backward' },
130 },
131 })
132 }
133​
134 protected createResult(
135 query: Query<
136 TQueryFnData,
137 TError,
138 InfiniteData<TQueryFnData, TPageParam>,
139 TQueryKey
140 >,
141 options: InfiniteQueryObserverOptions<
142 TQueryFnData,
143 TError,
144 TData,
145 TQueryKey,
146 TPageParam
147 >,
148 ): InfiniteQueryObserverResult<TData, TError> {
149 const { state } = query
150 const parentResult = super.createResult(query, options)
151​
152 const { isFetching, isRefetching, isError, isRefetchError } = parentResult
153 const fetchDirection = state.fetchMeta?.fetchMore?.direction
154​
155 const isFetchNextPageError = isError && fetchDirection === 'forward'
156 const isFetchingNextPage = isFetching && fetchDirection === 'forward'
157​
158 const isFetchPreviousPageError = isError && fetchDirection === 'backward'
159 const isFetchingPreviousPage = isFetching && fetchDirection === 'backward'
160​
161 const result: InfiniteQueryObserverBaseResult<TData, TError> = {
162 ...parentResult,
163 fetchNextPage: this.fetchNextPage,
164 fetchPreviousPage: this.fetchPreviousPage,
165 hasNextPage: hasNextPage(options, state.data),
166 hasPreviousPage: hasPreviousPage(options, state.data),
167 isFetchNextPageError,
168 isFetchingNextPage,
169 isFetchPreviousPageError,
170 isFetchingPreviousPage,
171 isRefetchError:
172 isRefetchError && !isFetchNextPageError && !isFetchPreviousPageError,
173 isRefetching:
174 isRefetching && !isFetchingNextPage && !isFetchingPreviousPage,
175 }
176​
177 return result as InfiniteQueryObserverResult<TData, TError>
178 }
180​
181type ReplaceReturnType<
182 TFunction extends (...args: Array<any>) => unknown,
183 TReturn,
184> = (...args: Parameters<TFunction>) => TReturn