-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinifinite-query.ts
More file actions
383 lines (356 loc) · 10.4 KB
/
inifinite-query.ts
File metadata and controls
383 lines (356 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
type DefaultError,
type FetchNextPageOptions,
type FetchPreviousPageOptions,
type FetchStatus,
type InfiniteData,
InfiniteQueryObserver,
type InfiniteQueryObserverBaseResult,
type InfiniteQueryObserverResult,
type QueryKey,
type QueryStatus,
} from '@tanstack/query-core';
import { BaseQuery } from './base-query.js';
import type {
InfiniteQueryConfig,
InfiniteQueryDoneListener,
InfiniteQueryErrorListener,
InfiniteQueryFlattenConfig,
InfiniteQueryInvalidateParams,
InfiniteQueryOptions,
InfiniteQueryRemoveParams,
InfiniteQueryResetParams,
InfiniteQueryStartParams,
InfiniteQueryUpdateOptionsAllVariants,
} from './inifinite-query.types.js';
import type { QueryClient } from './query-client.js';
import type { AnyQueryClient } from './query-client.types.js';
const originalQueryProperties = [
'data',
'dataUpdatedAt',
'error',
'errorUpdatedAt',
'failureCount',
'failureReason',
'errorUpdateCount',
'isError',
'isFetched',
'isFetching',
'isLoading',
'isPending',
'isLoadingError',
'isPaused',
'isPlaceholderData',
'isRefetchError',
'isRefetching',
'isStale',
'isSuccess',
'status',
'fetchStatus',
'fetchNextPage',
'fetchPreviousPage',
'hasNextPage',
'hasPreviousPage',
'isFetchNextPageError',
'isFetchingNextPage',
'isFetchPreviousPageError',
'isFetchingPreviousPage',
] as const satisfies (keyof InfiniteQueryObserverBaseResult)[];
export class InfiniteQuery<
TQueryFnData = unknown,
TError = DefaultError,
TPageParam = unknown,
TData = InfiniteData<TQueryFnData, TPageParam>,
TQueryKey extends QueryKey = QueryKey,
>
extends BaseQuery<
InfiniteData<TQueryFnData, TPageParam>,
TError,
TData,
InfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData, TQueryKey>,
InfiniteQueryObserverResult<TData, TError>,
InfiniteQueryObserver<TData, TError, TData, TQueryKey, TPageParam>,
InfiniteQueryDoneListener<TData>,
InfiniteQueryErrorListener<TError>,
InfiniteQueryUpdateOptionsAllVariants<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>,
InfiniteQueryResetParams,
InfiniteQueryRemoveParams,
InfiniteQueryInvalidateParams,
InfiniteQueryStartParams<TQueryFnData, TError, TPageParam, TData, TQueryKey>
>
implements
Pick<
InfiniteQueryObserverBaseResult<TData, TError>,
(typeof originalQueryProperties)[number]
>
{
protected config: InfiniteQueryConfig<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>;
/**
* The last successfully resolved data for the query.
*/
data!: TData | undefined;
/**
* The timestamp for when the query most recently returned the `status` as `"success"`.
*/
dataUpdatedAt!: number;
/**
* The error object for the query, if an error was thrown.
* - Defaults to `null`.
*/
error!: TError | null;
/**
* The timestamp for when the query most recently returned the `status` as `"error"`.
*/
errorUpdatedAt!: number;
/**
* The failure count for the query.
* - Incremented every time the query fails.
* - Reset to `0` when the query succeeds.
*/
failureCount!: number;
/**
* The failure reason for the query retry.
* - Reset to `null` when the query succeeds.
*/
failureReason!: TError | null;
/**
* The sum of all errors.
*/
errorUpdateCount!: number;
/**
* A derived boolean from the `status` variable, provided for convenience.
* - `true` if the query attempt resulted in an error.
*/
isError!: boolean;
/**
* Will be `true` if the query has been fetched.
*/
isFetched!: boolean;
/**
* A derived boolean from the `fetchStatus` variable, provided for convenience.
* - `true` whenever the `queryFn` is executing, which includes initial `pending` as well as background refetch.
*/
isFetching!: boolean;
/**
* Is `true` whenever the first fetch for a query is in-flight.
* - Is the same as `isFetching && isPending`.
*/
isLoading!: boolean;
/**
* Will be `pending` if there's no cached data and no query attempt was finished yet.
*/
isPending!: boolean;
/**
* Will be `true` if the query failed while fetching for the first time.
*/
isLoadingError!: boolean;
/**
* A derived boolean from the `fetchStatus` variable, provided for convenience.
* - The query wanted to fetch, but has been `paused`.
*/
isPaused!: boolean;
/**
* Will be `true` if the data shown is the placeholder data.
*/
isPlaceholderData!: boolean;
/**
* Will be `true` if the query failed while refetching.
*/
isRefetchError!: boolean;
/**
* Is `true` whenever a background refetch is in-flight, which _does not_ include initial `pending`.
* - Is the same as `isFetching && !isPending`.
*/
isRefetching!: boolean;
/**
* Will be `true` if the data in the cache is invalidated or if the data is older than the given `staleTime`.
*/
isStale!: boolean;
/**
* A derived boolean from the `status` variable, provided for convenience.
* - `true` if the query has received a response with no errors and is ready to display its data.
*/
isSuccess!: boolean;
/**
* The status of the query.
* - Will be:
* - `pending` if there's no cached data and no query attempt was finished yet.
* - `error` if the query attempt resulted in an error.
* - `success` if the query has received a response with no errors and is ready to display its data.
*/
status!: QueryStatus;
/**
* The fetch status of the query.
* - `fetching`: Is `true` whenever the queryFn is executing, which includes initial `pending` as well as background refetch.
* - `paused`: The query wanted to fetch, but has been `paused`.
* - `idle`: The query is not fetching.
* - See [Network Mode](https://tanstack.com/query/latest/docs/framework/react/guides/network-mode) for more information.
*/
fetchStatus!: FetchStatus;
/**
* Will be `true` if there is a next page to be fetched (known via the `getNextPageParam` option).
*/
hasNextPage!: boolean;
/**
* Will be `true` if there is a previous page to be fetched (known via the `getPreviousPageParam` option).
*/
hasPreviousPage!: boolean;
/**
* Will be `true` if the query failed while fetching the next page.
*/
isFetchNextPageError!: boolean;
/**
* Will be `true` while fetching the next page with `fetchNextPage`.
*/
isFetchingNextPage!: boolean;
/**
* Will be `true` if the query failed while fetching the previous page.
*/
isFetchPreviousPageError!: boolean;
/**
* Will be `true` while fetching the previous page with `fetchPreviousPage`.
*/
isFetchingPreviousPage!: boolean;
constructor(
config: InfiniteQueryConfig<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>,
);
constructor(
queryClient: AnyQueryClient,
config: () => InfiniteQueryFlattenConfig<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>,
);
constructor(...args: any[]) {
let queryClient: AnyQueryClient;
let config: InfiniteQueryConfig<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>;
let getDynamicOptions:
| InfiniteQueryConfig<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>['options']
| undefined;
if (args.length === 2) {
queryClient = args[0];
config = args[1]();
getDynamicOptions = args[1];
} else {
queryClient = args[0].queryClient;
config = args[0];
getDynamicOptions = args[0].options;
}
super(config.abortSignal);
this.cumulativeQueryKeyHashesSet = new Set();
const { queryKey: queryKeyOrDynamicQueryKey, ...restOptions } = config;
this.config = {
...config,
queryClient,
};
const qc = queryClient as unknown as Partial<
Pick<QueryClient, 'queryFeatures' | 'hooks'>
>;
this.features = BaseQuery.mergeQueryFeatures(config, queryClient);
this.initializeBaseState(queryClient, this.features, qc.hooks);
const isQueryKeyDynamic = typeof queryKeyOrDynamicQueryKey === 'function';
if (!isQueryKeyDynamic) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
restOptions.queryKey = queryKeyOrDynamicQueryKey;
}
this.options = this.queryClient.defaultQueryOptions(
restOptions as any,
) as InfiniteQueryOptions<
TQueryFnData,
TError,
TPageParam,
TData,
TQueryKey
>;
this.options.structuralSharing = this.options.structuralSharing ?? false;
const getAllDynamicOptions =
getDynamicOptions || isQueryKeyDynamic
? () => {
const freshDynamicOptions = {
...getDynamicOptions?.(this),
};
if (isQueryKeyDynamic) {
freshDynamicOptions.queryKey = queryKeyOrDynamicQueryKey();
}
return freshDynamicOptions;
}
: undefined;
if (getAllDynamicOptions) {
Object.assign(this.options, getAllDynamicOptions());
} else if (!isQueryKeyDynamic) {
this.options.queryKey =
queryKeyOrDynamicQueryKey ?? this.options.queryKey ?? [];
}
// Tracking props visit should be done in MobX, by default.
this.options.notifyOnChangeProps =
restOptions.notifyOnChangeProps ??
queryClient.getDefaultOptions().queries?.notifyOnChangeProps ??
'all';
this.processOptions(this.options);
// @ts-expect-error
this.queryObserver = new InfiniteQueryObserver(queryClient, this.options);
this.finalizeInitialization({
originalQueryProperties,
getAllDynamicOptions,
abortSignal: config.abortSignal,
preserveExistingProperties: true,
});
this.registerInitialListeners({
onDone: config.onDone,
onError: config.onError,
});
this.config.onInit?.(this);
this.hooks?.onInfiniteQueryInit?.(this);
}
/**
* This function allows you to fetch the next "page" of results.
*/
fetchNextPage(options?: FetchNextPageOptions | undefined) {
return this._result.fetchNextPage(options);
}
/**
* This function allows you to fetch the previous "page" of results.
*/
fetchPreviousPage(options?: FetchPreviousPageOptions | undefined) {
return this._result.fetchPreviousPage(options);
}
protected handleDestroy() {
this.cleanup();
this.hooks?.onInfiniteQueryDestroy?.(this);
}
}