@tanstack/query-core 4.24.10 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/focusManager.d.ts +1 -3
- package/build/lib/focusManager.esm.js +19 -36
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +19 -38
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/focusManager.mjs +19 -36
- package/build/lib/focusManager.mjs.map +1 -1
- package/build/lib/hydration.esm.js +21 -23
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +21 -25
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +21 -23
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +1 -2
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +2 -8
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +3 -7
- package/build/lib/infiniteQueryBehavior.esm.js +52 -75
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +50 -77
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +52 -75
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +4 -4
- package/build/lib/infiniteQueryObserver.esm.js +18 -26
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +18 -28
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +18 -26
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +11 -22
- package/build/lib/mutation.esm.js +73 -105
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +73 -107
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +73 -105
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +4 -6
- package/build/lib/mutationCache.esm.js +23 -32
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +23 -34
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +23 -32
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +4 -9
- package/build/lib/mutationObserver.esm.js +42 -73
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +42 -75
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +42 -73
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/notifyManager.esm.js +7 -17
- package/build/lib/notifyManager.esm.js.map +1 -1
- package/build/lib/notifyManager.js +7 -19
- package/build/lib/notifyManager.js.map +1 -1
- package/build/lib/notifyManager.mjs +7 -17
- package/build/lib/notifyManager.mjs.map +1 -1
- package/build/lib/onlineManager.d.ts +1 -3
- package/build/lib/onlineManager.esm.js +16 -30
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +16 -32
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/onlineManager.mjs +16 -30
- package/build/lib/onlineManager.mjs.map +1 -1
- package/build/lib/queriesObserver.d.ts +3 -10
- package/build/lib/queriesObserver.esm.js +47 -71
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +49 -75
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +47 -71
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +14 -21
- package/build/lib/query.esm.js +140 -194
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +139 -195
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +140 -194
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +12 -7
- package/build/lib/queryCache.esm.js +21 -45
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +20 -46
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +21 -45
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +18 -46
- package/build/lib/queryClient.esm.js +137 -216
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +136 -217
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +137 -216
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +4 -29
- package/build/lib/queryObserver.esm.js +176 -258
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +176 -260
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +176 -258
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts +3 -3
- package/build/lib/removable.esm.js +10 -14
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +10 -16
- package/build/lib/removable.js.map +1 -1
- package/build/lib/removable.mjs +10 -14
- package/build/lib/removable.mjs.map +1 -1
- package/build/lib/retryer.d.ts +5 -5
- package/build/lib/retryer.esm.js +27 -44
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +27 -46
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/retryer.mjs +27 -44
- package/build/lib/retryer.mjs.map +1 -1
- package/build/lib/subscribable.esm.js +4 -7
- package/build/lib/subscribable.esm.js.map +1 -1
- package/build/lib/subscribable.js +4 -9
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/subscribable.mjs +4 -7
- package/build/lib/subscribable.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +3 -12
- package/build/lib/types.d.ts +111 -99
- package/build/lib/utils.d.ts +8 -18
- package/build/lib/utils.esm.js +39 -132
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +42 -144
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +39 -132
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +867 -1399
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/focusManager.ts +17 -24
- package/src/index.ts +1 -11
- package/src/infiniteQueryBehavior.ts +54 -94
- package/src/infiniteQueryObserver.ts +10 -12
- package/src/mutation.ts +68 -92
- package/src/mutationCache.ts +27 -27
- package/src/mutationObserver.ts +58 -97
- package/src/onlineManager.ts +14 -14
- package/src/queriesObserver.ts +50 -54
- package/src/query.ts +107 -111
- package/src/queryCache.ts +42 -41
- package/src/queryClient.ts +155 -434
- package/src/queryObserver.ts +155 -192
- package/src/removable.ts +13 -13
- package/src/retryer.ts +5 -5
- package/src/subscribable.ts +1 -1
- package/src/tests/focusManager.test.tsx +25 -25
- package/src/tests/hydration.test.tsx +167 -81
- package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
- package/src/tests/infiniteQueryObserver.test.tsx +6 -2
- package/src/tests/mutationCache.test.tsx +127 -127
- package/src/tests/mutationObserver.test.tsx +1 -31
- package/src/tests/mutations.test.tsx +62 -43
- package/src/tests/onlineManager.test.tsx +12 -4
- package/src/tests/queriesObserver.test.tsx +41 -77
- package/src/tests/query.test.tsx +175 -243
- package/src/tests/queryCache.test.tsx +170 -93
- package/src/tests/queryClient.test.tsx +229 -378
- package/src/tests/queryObserver.test.tsx +23 -147
- package/src/tests/utils.test.tsx +84 -29
- package/src/tests/utils.ts +9 -18
- package/src/types.ts +187 -140
- package/src/utils.ts +31 -124
- package/build/lib/logger.d.ts +0 -8
- package/build/lib/logger.esm.js +0 -4
- package/build/lib/logger.esm.js.map +0 -1
- package/build/lib/logger.js +0 -8
- package/build/lib/logger.js.map +0 -1
- package/build/lib/logger.mjs +0 -4
- package/build/lib/logger.mjs.map +0 -1
- package/build/lib/logger.native.d.ts +0 -6
- package/build/lib/logger.native.esm.js +0 -12
- package/build/lib/logger.native.esm.js.map +0 -1
- package/build/lib/logger.native.js +0 -16
- package/build/lib/logger.native.js.map +0 -1
- package/build/lib/logger.native.mjs +0 -12
- package/build/lib/logger.native.mjs.map +0 -1
- package/src/logger.native.ts +0 -11
- package/src/logger.ts +0 -9
package/src/queryObserver.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DefaultedQueryObserverOptions,
|
|
1
|
+
import type { DefaultedQueryObserverOptions, RegisteredError } from './types'
|
|
2
2
|
import {
|
|
3
3
|
isServer,
|
|
4
4
|
isValidTimeout,
|
|
@@ -28,7 +28,6 @@ type QueryObserverListener<TData, TError> = (
|
|
|
28
28
|
) => void
|
|
29
29
|
|
|
30
30
|
export interface NotifyOptions {
|
|
31
|
-
cache?: boolean
|
|
32
31
|
listeners?: boolean
|
|
33
32
|
onError?: boolean
|
|
34
33
|
onSuccess?: boolean
|
|
@@ -40,7 +39,7 @@ export interface ObserverFetchOptions extends FetchOptions {
|
|
|
40
39
|
|
|
41
40
|
export class QueryObserver<
|
|
42
41
|
TQueryFnData = unknown,
|
|
43
|
-
TError =
|
|
42
|
+
TError = RegisteredError,
|
|
44
43
|
TData = TQueryFnData,
|
|
45
44
|
TQueryData = TQueryFnData,
|
|
46
45
|
TQueryKey extends QueryKey = QueryKey,
|
|
@@ -53,26 +52,26 @@ export class QueryObserver<
|
|
|
53
52
|
TQueryKey
|
|
54
53
|
>
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
#client: QueryClient
|
|
56
|
+
#currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!
|
|
57
|
+
#currentQueryInitialState: QueryState<TQueryData, TError> = undefined!
|
|
58
|
+
#currentResult: QueryObserverResult<TData, TError> = undefined!
|
|
59
|
+
#currentResultState?: QueryState<TQueryData, TError>
|
|
60
|
+
#currentResultOptions?: QueryObserverOptions<
|
|
62
61
|
TQueryFnData,
|
|
63
62
|
TError,
|
|
64
63
|
TData,
|
|
65
64
|
TQueryData,
|
|
66
65
|
TQueryKey
|
|
67
66
|
>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
#previousQueryResult?: QueryObserverResult<TData, TError>
|
|
68
|
+
#selectError: TError | null
|
|
69
|
+
#selectFn?: (data: TQueryData) => TData
|
|
70
|
+
#selectResult?: TData
|
|
71
|
+
#staleTimeoutId?: ReturnType<typeof setTimeout>
|
|
72
|
+
#refetchIntervalId?: ReturnType<typeof setInterval>
|
|
73
|
+
#currentRefetchInterval?: number | false
|
|
74
|
+
#trackedProps: Set<keyof QueryObserverResult> = new Set()
|
|
76
75
|
|
|
77
76
|
constructor(
|
|
78
77
|
client: QueryClient,
|
|
@@ -86,28 +85,26 @@ export class QueryObserver<
|
|
|
86
85
|
) {
|
|
87
86
|
super()
|
|
88
87
|
|
|
89
|
-
this
|
|
88
|
+
this.#client = client
|
|
90
89
|
this.options = options
|
|
91
|
-
this
|
|
92
|
-
this.selectError = null
|
|
90
|
+
this.#selectError = null
|
|
93
91
|
this.bindMethods()
|
|
94
92
|
this.setOptions(options)
|
|
95
93
|
}
|
|
96
94
|
|
|
97
95
|
protected bindMethods(): void {
|
|
98
|
-
this.remove = this.remove.bind(this)
|
|
99
96
|
this.refetch = this.refetch.bind(this)
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
protected onSubscribe(): void {
|
|
103
100
|
if (this.listeners.length === 1) {
|
|
104
|
-
this
|
|
101
|
+
this.#currentQuery.addObserver(this)
|
|
105
102
|
|
|
106
|
-
if (shouldFetchOnMount(this
|
|
107
|
-
this
|
|
103
|
+
if (shouldFetchOnMount(this.#currentQuery, this.options)) {
|
|
104
|
+
this.#executeFetch()
|
|
108
105
|
}
|
|
109
106
|
|
|
110
|
-
this
|
|
107
|
+
this.#updateTimers()
|
|
111
108
|
}
|
|
112
109
|
}
|
|
113
110
|
|
|
@@ -119,7 +116,7 @@ export class QueryObserver<
|
|
|
119
116
|
|
|
120
117
|
shouldFetchOnReconnect(): boolean {
|
|
121
118
|
return shouldFetchOn(
|
|
122
|
-
this
|
|
119
|
+
this.#currentQuery,
|
|
123
120
|
this.options,
|
|
124
121
|
this.options.refetchOnReconnect,
|
|
125
122
|
)
|
|
@@ -127,7 +124,7 @@ export class QueryObserver<
|
|
|
127
124
|
|
|
128
125
|
shouldFetchOnWindowFocus(): boolean {
|
|
129
126
|
return shouldFetchOn(
|
|
130
|
-
this
|
|
127
|
+
this.#currentQuery,
|
|
131
128
|
this.options,
|
|
132
129
|
this.options.refetchOnWindowFocus,
|
|
133
130
|
)
|
|
@@ -135,9 +132,9 @@ export class QueryObserver<
|
|
|
135
132
|
|
|
136
133
|
destroy(): void {
|
|
137
134
|
this.listeners = []
|
|
138
|
-
this
|
|
139
|
-
this
|
|
140
|
-
this
|
|
135
|
+
this.#clearStaleTimeout()
|
|
136
|
+
this.#clearRefetchInterval()
|
|
137
|
+
this.#currentQuery.removeObserver(this)
|
|
141
138
|
}
|
|
142
139
|
|
|
143
140
|
setOptions(
|
|
@@ -151,25 +148,14 @@ export class QueryObserver<
|
|
|
151
148
|
notifyOptions?: NotifyOptions,
|
|
152
149
|
): void {
|
|
153
150
|
const prevOptions = this.options
|
|
154
|
-
const prevQuery = this
|
|
151
|
+
const prevQuery = this.#currentQuery
|
|
155
152
|
|
|
156
|
-
this.options = this
|
|
157
|
-
|
|
158
|
-
if (
|
|
159
|
-
process.env.NODE_ENV !== 'production' &&
|
|
160
|
-
typeof options?.isDataEqual !== 'undefined'
|
|
161
|
-
) {
|
|
162
|
-
this.client
|
|
163
|
-
.getLogger()
|
|
164
|
-
.error(
|
|
165
|
-
`The isDataEqual option has been deprecated and will be removed in the next major version. You can achieve the same functionality by passing a function as the structuralSharing option`,
|
|
166
|
-
)
|
|
167
|
-
}
|
|
153
|
+
this.options = this.#client.defaultQueryOptions(options)
|
|
168
154
|
|
|
169
155
|
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
170
|
-
this
|
|
156
|
+
this.#client.getQueryCache().notify({
|
|
171
157
|
type: 'observerOptionsUpdated',
|
|
172
|
-
query: this
|
|
158
|
+
query: this.#currentQuery,
|
|
173
159
|
observer: this,
|
|
174
160
|
})
|
|
175
161
|
}
|
|
@@ -186,7 +172,7 @@ export class QueryObserver<
|
|
|
186
172
|
this.options.queryKey = prevOptions.queryKey
|
|
187
173
|
}
|
|
188
174
|
|
|
189
|
-
this
|
|
175
|
+
this.#updateQuery()
|
|
190
176
|
|
|
191
177
|
const mounted = this.hasListeners()
|
|
192
178
|
|
|
@@ -194,38 +180,38 @@ export class QueryObserver<
|
|
|
194
180
|
if (
|
|
195
181
|
mounted &&
|
|
196
182
|
shouldFetchOptionally(
|
|
197
|
-
this
|
|
183
|
+
this.#currentQuery,
|
|
198
184
|
prevQuery,
|
|
199
185
|
this.options,
|
|
200
186
|
prevOptions,
|
|
201
187
|
)
|
|
202
188
|
) {
|
|
203
|
-
this
|
|
189
|
+
this.#executeFetch()
|
|
204
190
|
}
|
|
205
191
|
|
|
206
192
|
// Update result
|
|
207
|
-
this
|
|
193
|
+
this.#updateResult(notifyOptions)
|
|
208
194
|
|
|
209
195
|
// Update stale interval if needed
|
|
210
196
|
if (
|
|
211
197
|
mounted &&
|
|
212
|
-
(this
|
|
198
|
+
(this.#currentQuery !== prevQuery ||
|
|
213
199
|
this.options.enabled !== prevOptions.enabled ||
|
|
214
200
|
this.options.staleTime !== prevOptions.staleTime)
|
|
215
201
|
) {
|
|
216
|
-
this
|
|
202
|
+
this.#updateStaleTimeout()
|
|
217
203
|
}
|
|
218
204
|
|
|
219
|
-
const nextRefetchInterval = this
|
|
205
|
+
const nextRefetchInterval = this.#computeRefetchInterval()
|
|
220
206
|
|
|
221
207
|
// Update refetch interval if needed
|
|
222
208
|
if (
|
|
223
209
|
mounted &&
|
|
224
|
-
(this
|
|
210
|
+
(this.#currentQuery !== prevQuery ||
|
|
225
211
|
this.options.enabled !== prevOptions.enabled ||
|
|
226
|
-
nextRefetchInterval !== this
|
|
212
|
+
nextRefetchInterval !== this.#currentRefetchInterval)
|
|
227
213
|
) {
|
|
228
|
-
this
|
|
214
|
+
this.#updateRefetchInterval(nextRefetchInterval)
|
|
229
215
|
}
|
|
230
216
|
}
|
|
231
217
|
|
|
@@ -238,13 +224,13 @@ export class QueryObserver<
|
|
|
238
224
|
TQueryKey
|
|
239
225
|
>,
|
|
240
226
|
): QueryObserverResult<TData, TError> {
|
|
241
|
-
const query = this
|
|
227
|
+
const query = this.#client.getQueryCache().build(this.#client, options)
|
|
242
228
|
|
|
243
229
|
return this.createResult(query, options)
|
|
244
230
|
}
|
|
245
231
|
|
|
246
232
|
getCurrentResult(): QueryObserverResult<TData, TError> {
|
|
247
|
-
return this
|
|
233
|
+
return this.#currentResult
|
|
248
234
|
}
|
|
249
235
|
|
|
250
236
|
trackResult(
|
|
@@ -257,7 +243,7 @@ export class QueryObserver<
|
|
|
257
243
|
configurable: false,
|
|
258
244
|
enumerable: true,
|
|
259
245
|
get: () => {
|
|
260
|
-
this
|
|
246
|
+
this.#trackedProps.add(key as keyof QueryObserverResult)
|
|
261
247
|
return result[key as keyof QueryObserverResult]
|
|
262
248
|
},
|
|
263
249
|
})
|
|
@@ -267,22 +253,14 @@ export class QueryObserver<
|
|
|
267
253
|
}
|
|
268
254
|
|
|
269
255
|
getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
|
|
270
|
-
return this
|
|
256
|
+
return this.#currentQuery
|
|
271
257
|
}
|
|
272
258
|
|
|
273
|
-
|
|
274
|
-
this.client.getQueryCache().remove(this.currentQuery)
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
refetch<TPageData>({
|
|
278
|
-
refetchPage,
|
|
279
|
-
...options
|
|
280
|
-
}: RefetchOptions & RefetchPageFilters<TPageData> = {}): Promise<
|
|
259
|
+
refetch({ ...options }: RefetchOptions = {}): Promise<
|
|
281
260
|
QueryObserverResult<TData, TError>
|
|
282
261
|
> {
|
|
283
262
|
return this.fetch({
|
|
284
263
|
...options,
|
|
285
|
-
meta: { refetchPage },
|
|
286
264
|
})
|
|
287
265
|
}
|
|
288
266
|
|
|
@@ -295,11 +273,11 @@ export class QueryObserver<
|
|
|
295
273
|
TQueryKey
|
|
296
274
|
>,
|
|
297
275
|
): Promise<QueryObserverResult<TData, TError>> {
|
|
298
|
-
const defaultedOptions = this
|
|
276
|
+
const defaultedOptions = this.#client.defaultQueryOptions(options)
|
|
299
277
|
|
|
300
|
-
const query = this
|
|
278
|
+
const query = this.#client
|
|
301
279
|
.getQueryCache()
|
|
302
|
-
.build(this
|
|
280
|
+
.build(this.#client, defaultedOptions)
|
|
303
281
|
query.isFetchingOptimistic = true
|
|
304
282
|
|
|
305
283
|
return query.fetch().then(() => this.createResult(query, defaultedOptions))
|
|
@@ -308,23 +286,23 @@ export class QueryObserver<
|
|
|
308
286
|
protected fetch(
|
|
309
287
|
fetchOptions: ObserverFetchOptions,
|
|
310
288
|
): Promise<QueryObserverResult<TData, TError>> {
|
|
311
|
-
return this
|
|
289
|
+
return this.#executeFetch({
|
|
312
290
|
...fetchOptions,
|
|
313
291
|
cancelRefetch: fetchOptions.cancelRefetch ?? true,
|
|
314
292
|
}).then(() => {
|
|
315
|
-
this
|
|
316
|
-
return this
|
|
293
|
+
this.#updateResult()
|
|
294
|
+
return this.#currentResult
|
|
317
295
|
})
|
|
318
296
|
}
|
|
319
297
|
|
|
320
|
-
|
|
298
|
+
#executeFetch(
|
|
321
299
|
fetchOptions?: ObserverFetchOptions,
|
|
322
300
|
): Promise<TQueryData | undefined> {
|
|
323
301
|
// Make sure we reference the latest query as the current one might have been removed
|
|
324
|
-
this
|
|
302
|
+
this.#updateQuery()
|
|
325
303
|
|
|
326
304
|
// Fetch
|
|
327
|
-
let promise: Promise<TQueryData | undefined> = this
|
|
305
|
+
let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(
|
|
328
306
|
this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,
|
|
329
307
|
fetchOptions,
|
|
330
308
|
)
|
|
@@ -336,19 +314,19 @@ export class QueryObserver<
|
|
|
336
314
|
return promise
|
|
337
315
|
}
|
|
338
316
|
|
|
339
|
-
|
|
340
|
-
this
|
|
317
|
+
#updateStaleTimeout(): void {
|
|
318
|
+
this.#clearStaleTimeout()
|
|
341
319
|
|
|
342
320
|
if (
|
|
343
321
|
isServer ||
|
|
344
|
-
this
|
|
322
|
+
this.#currentResult.isStale ||
|
|
345
323
|
!isValidTimeout(this.options.staleTime)
|
|
346
324
|
) {
|
|
347
325
|
return
|
|
348
326
|
}
|
|
349
327
|
|
|
350
328
|
const time = timeUntilStale(
|
|
351
|
-
this
|
|
329
|
+
this.#currentResult.dataUpdatedAt,
|
|
352
330
|
this.options.staleTime,
|
|
353
331
|
)
|
|
354
332
|
|
|
@@ -356,59 +334,62 @@ export class QueryObserver<
|
|
|
356
334
|
// To mitigate this issue we always add 1 ms to the timeout.
|
|
357
335
|
const timeout = time + 1
|
|
358
336
|
|
|
359
|
-
this
|
|
360
|
-
if (!this
|
|
361
|
-
this
|
|
337
|
+
this.#staleTimeoutId = setTimeout(() => {
|
|
338
|
+
if (!this.#currentResult.isStale) {
|
|
339
|
+
this.#updateResult()
|
|
362
340
|
}
|
|
363
341
|
}, timeout)
|
|
364
342
|
}
|
|
365
343
|
|
|
366
|
-
|
|
344
|
+
#computeRefetchInterval() {
|
|
367
345
|
return typeof this.options.refetchInterval === 'function'
|
|
368
|
-
? this.options.refetchInterval(
|
|
346
|
+
? this.options.refetchInterval(
|
|
347
|
+
this.#currentResult.data,
|
|
348
|
+
this.#currentQuery,
|
|
349
|
+
)
|
|
369
350
|
: this.options.refetchInterval ?? false
|
|
370
351
|
}
|
|
371
352
|
|
|
372
|
-
|
|
373
|
-
this
|
|
353
|
+
#updateRefetchInterval(nextInterval: number | false): void {
|
|
354
|
+
this.#clearRefetchInterval()
|
|
374
355
|
|
|
375
|
-
this
|
|
356
|
+
this.#currentRefetchInterval = nextInterval
|
|
376
357
|
|
|
377
358
|
if (
|
|
378
359
|
isServer ||
|
|
379
360
|
this.options.enabled === false ||
|
|
380
|
-
!isValidTimeout(this
|
|
381
|
-
this
|
|
361
|
+
!isValidTimeout(this.#currentRefetchInterval) ||
|
|
362
|
+
this.#currentRefetchInterval === 0
|
|
382
363
|
) {
|
|
383
364
|
return
|
|
384
365
|
}
|
|
385
366
|
|
|
386
|
-
this
|
|
367
|
+
this.#refetchIntervalId = setInterval(() => {
|
|
387
368
|
if (
|
|
388
369
|
this.options.refetchIntervalInBackground ||
|
|
389
370
|
focusManager.isFocused()
|
|
390
371
|
) {
|
|
391
|
-
this
|
|
372
|
+
this.#executeFetch()
|
|
392
373
|
}
|
|
393
|
-
}, this
|
|
374
|
+
}, this.#currentRefetchInterval)
|
|
394
375
|
}
|
|
395
376
|
|
|
396
|
-
|
|
397
|
-
this
|
|
398
|
-
this
|
|
377
|
+
#updateTimers(): void {
|
|
378
|
+
this.#updateStaleTimeout()
|
|
379
|
+
this.#updateRefetchInterval(this.#computeRefetchInterval())
|
|
399
380
|
}
|
|
400
381
|
|
|
401
|
-
|
|
402
|
-
if (this
|
|
403
|
-
clearTimeout(this
|
|
404
|
-
this
|
|
382
|
+
#clearStaleTimeout(): void {
|
|
383
|
+
if (this.#staleTimeoutId) {
|
|
384
|
+
clearTimeout(this.#staleTimeoutId)
|
|
385
|
+
this.#staleTimeoutId = undefined
|
|
405
386
|
}
|
|
406
387
|
}
|
|
407
388
|
|
|
408
|
-
|
|
409
|
-
if (this
|
|
410
|
-
clearInterval(this
|
|
411
|
-
this
|
|
389
|
+
#clearRefetchInterval(): void {
|
|
390
|
+
if (this.#refetchIntervalId) {
|
|
391
|
+
clearInterval(this.#refetchIntervalId)
|
|
392
|
+
this.#refetchIntervalId = undefined
|
|
412
393
|
}
|
|
413
394
|
}
|
|
414
395
|
|
|
@@ -422,24 +403,23 @@ export class QueryObserver<
|
|
|
422
403
|
TQueryKey
|
|
423
404
|
>,
|
|
424
405
|
): QueryObserverResult<TData, TError> {
|
|
425
|
-
const prevQuery = this
|
|
406
|
+
const prevQuery = this.#currentQuery
|
|
426
407
|
const prevOptions = this.options
|
|
427
|
-
const prevResult = this
|
|
408
|
+
const prevResult = this.#currentResult as
|
|
428
409
|
| QueryObserverResult<TData, TError>
|
|
429
410
|
| undefined
|
|
430
|
-
const prevResultState = this
|
|
431
|
-
const prevResultOptions = this
|
|
411
|
+
const prevResultState = this.#currentResultState
|
|
412
|
+
const prevResultOptions = this.#currentResultOptions
|
|
432
413
|
const queryChange = query !== prevQuery
|
|
433
414
|
const queryInitialState = queryChange
|
|
434
415
|
? query.state
|
|
435
|
-
: this
|
|
416
|
+
: this.#currentQueryInitialState
|
|
436
417
|
const prevQueryResult = queryChange
|
|
437
|
-
? this
|
|
438
|
-
: this
|
|
418
|
+
? this.#currentResult
|
|
419
|
+
: this.#previousQueryResult
|
|
439
420
|
|
|
440
421
|
const { state } = query
|
|
441
|
-
let {
|
|
442
|
-
let isPreviousData = false
|
|
422
|
+
let { error, errorUpdatedAt, fetchStatus, status } = state
|
|
443
423
|
let isPlaceholderData = false
|
|
444
424
|
let data: TData | undefined
|
|
445
425
|
|
|
@@ -456,8 +436,8 @@ export class QueryObserver<
|
|
|
456
436
|
fetchStatus = canFetch(query.options.networkMode)
|
|
457
437
|
? 'fetching'
|
|
458
438
|
: 'paused'
|
|
459
|
-
if (!dataUpdatedAt) {
|
|
460
|
-
status = '
|
|
439
|
+
if (!state.dataUpdatedAt) {
|
|
440
|
+
status = 'pending'
|
|
461
441
|
}
|
|
462
442
|
}
|
|
463
443
|
if (options._optimisticResults === 'isRestoring') {
|
|
@@ -465,39 +445,24 @@ export class QueryObserver<
|
|
|
465
445
|
}
|
|
466
446
|
}
|
|
467
447
|
|
|
468
|
-
// Keep previous data if needed
|
|
469
|
-
if (
|
|
470
|
-
options.keepPreviousData &&
|
|
471
|
-
!state.dataUpdatedAt &&
|
|
472
|
-
prevQueryResult?.isSuccess &&
|
|
473
|
-
status !== 'error'
|
|
474
|
-
) {
|
|
475
|
-
data = prevQueryResult.data
|
|
476
|
-
dataUpdatedAt = prevQueryResult.dataUpdatedAt
|
|
477
|
-
status = prevQueryResult.status
|
|
478
|
-
isPreviousData = true
|
|
479
|
-
}
|
|
480
448
|
// Select data if needed
|
|
481
|
-
|
|
449
|
+
if (options.select && typeof state.data !== 'undefined') {
|
|
482
450
|
// Memoize select result
|
|
483
451
|
if (
|
|
484
452
|
prevResult &&
|
|
485
453
|
state.data === prevResultState?.data &&
|
|
486
|
-
options.select === this
|
|
454
|
+
options.select === this.#selectFn
|
|
487
455
|
) {
|
|
488
|
-
data = this
|
|
456
|
+
data = this.#selectResult
|
|
489
457
|
} else {
|
|
490
458
|
try {
|
|
491
|
-
this
|
|
459
|
+
this.#selectFn = options.select
|
|
492
460
|
data = options.select(state.data)
|
|
493
461
|
data = replaceData(prevResult?.data, data, options)
|
|
494
|
-
this
|
|
495
|
-
this
|
|
462
|
+
this.#selectResult = data
|
|
463
|
+
this.#selectError = null
|
|
496
464
|
} catch (selectError) {
|
|
497
|
-
|
|
498
|
-
this.client.getLogger().error(selectError)
|
|
499
|
-
}
|
|
500
|
-
this.selectError = selectError as TError
|
|
465
|
+
this.#selectError = selectError as TError
|
|
501
466
|
}
|
|
502
467
|
}
|
|
503
468
|
}
|
|
@@ -510,7 +475,7 @@ export class QueryObserver<
|
|
|
510
475
|
if (
|
|
511
476
|
typeof options.placeholderData !== 'undefined' &&
|
|
512
477
|
typeof data === 'undefined' &&
|
|
513
|
-
status === '
|
|
478
|
+
status === 'pending'
|
|
514
479
|
) {
|
|
515
480
|
let placeholderData
|
|
516
481
|
|
|
@@ -523,17 +488,16 @@ export class QueryObserver<
|
|
|
523
488
|
} else {
|
|
524
489
|
placeholderData =
|
|
525
490
|
typeof options.placeholderData === 'function'
|
|
526
|
-
? (
|
|
491
|
+
? (
|
|
492
|
+
options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
|
|
493
|
+
)(prevQueryResult?.data as TQueryData | undefined)
|
|
527
494
|
: options.placeholderData
|
|
528
495
|
if (options.select && typeof placeholderData !== 'undefined') {
|
|
529
496
|
try {
|
|
530
497
|
placeholderData = options.select(placeholderData)
|
|
531
|
-
this
|
|
498
|
+
this.#selectError = null
|
|
532
499
|
} catch (selectError) {
|
|
533
|
-
|
|
534
|
-
this.client.getLogger().error(selectError)
|
|
535
|
-
}
|
|
536
|
-
this.selectError = selectError as TError
|
|
500
|
+
this.#selectError = selectError as TError
|
|
537
501
|
}
|
|
538
502
|
}
|
|
539
503
|
}
|
|
@@ -545,26 +509,29 @@ export class QueryObserver<
|
|
|
545
509
|
}
|
|
546
510
|
}
|
|
547
511
|
|
|
548
|
-
if (this
|
|
549
|
-
error = this
|
|
550
|
-
data = this
|
|
512
|
+
if (this.#selectError) {
|
|
513
|
+
error = this.#selectError as any
|
|
514
|
+
data = this.#selectResult
|
|
551
515
|
errorUpdatedAt = Date.now()
|
|
552
516
|
status = 'error'
|
|
553
517
|
}
|
|
554
518
|
|
|
555
519
|
const isFetching = fetchStatus === 'fetching'
|
|
556
|
-
const
|
|
520
|
+
const isPending = status === 'pending'
|
|
557
521
|
const isError = status === 'error'
|
|
558
522
|
|
|
523
|
+
const isLoading = isPending && isFetching
|
|
524
|
+
|
|
559
525
|
const result: QueryObserverBaseResult<TData, TError> = {
|
|
560
526
|
status,
|
|
561
527
|
fetchStatus,
|
|
562
|
-
|
|
528
|
+
isPending,
|
|
563
529
|
isSuccess: status === 'success',
|
|
564
530
|
isError,
|
|
565
|
-
isInitialLoading: isLoading
|
|
531
|
+
isInitialLoading: isLoading,
|
|
532
|
+
isLoading,
|
|
566
533
|
data,
|
|
567
|
-
dataUpdatedAt,
|
|
534
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
568
535
|
error,
|
|
569
536
|
errorUpdatedAt,
|
|
570
537
|
failureCount: state.fetchFailureCount,
|
|
@@ -575,38 +542,36 @@ export class QueryObserver<
|
|
|
575
542
|
state.dataUpdateCount > queryInitialState.dataUpdateCount ||
|
|
576
543
|
state.errorUpdateCount > queryInitialState.errorUpdateCount,
|
|
577
544
|
isFetching,
|
|
578
|
-
isRefetching: isFetching && !
|
|
545
|
+
isRefetching: isFetching && !isPending,
|
|
579
546
|
isLoadingError: isError && state.dataUpdatedAt === 0,
|
|
580
547
|
isPaused: fetchStatus === 'paused',
|
|
581
548
|
isPlaceholderData,
|
|
582
|
-
isPreviousData,
|
|
583
549
|
isRefetchError: isError && state.dataUpdatedAt !== 0,
|
|
584
550
|
isStale: isStale(query, options),
|
|
585
551
|
refetch: this.refetch,
|
|
586
|
-
remove: this.remove,
|
|
587
552
|
}
|
|
588
553
|
|
|
589
554
|
return result as QueryObserverResult<TData, TError>
|
|
590
555
|
}
|
|
591
556
|
|
|
592
|
-
updateResult(notifyOptions?: NotifyOptions): void {
|
|
593
|
-
const prevResult = this
|
|
557
|
+
#updateResult(notifyOptions?: NotifyOptions): void {
|
|
558
|
+
const prevResult = this.#currentResult as
|
|
594
559
|
| QueryObserverResult<TData, TError>
|
|
595
560
|
| undefined
|
|
596
561
|
|
|
597
|
-
const nextResult = this.createResult(this
|
|
598
|
-
this
|
|
599
|
-
this
|
|
562
|
+
const nextResult = this.createResult(this.#currentQuery, this.options)
|
|
563
|
+
this.#currentResultState = this.#currentQuery.state
|
|
564
|
+
this.#currentResultOptions = this.options
|
|
600
565
|
|
|
601
566
|
// Only notify and update result if something has changed
|
|
602
567
|
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
603
568
|
return
|
|
604
569
|
}
|
|
605
570
|
|
|
606
|
-
this
|
|
571
|
+
this.#currentResult = nextResult
|
|
607
572
|
|
|
608
573
|
// Determine which callbacks to trigger
|
|
609
|
-
const defaultNotifyOptions: NotifyOptions = {
|
|
574
|
+
const defaultNotifyOptions: NotifyOptions = {}
|
|
610
575
|
|
|
611
576
|
const shouldNotifyListeners = (): boolean => {
|
|
612
577
|
if (!prevResult) {
|
|
@@ -617,20 +582,20 @@ export class QueryObserver<
|
|
|
617
582
|
|
|
618
583
|
if (
|
|
619
584
|
notifyOnChangeProps === 'all' ||
|
|
620
|
-
(!notifyOnChangeProps && !this
|
|
585
|
+
(!notifyOnChangeProps && !this.#trackedProps.size)
|
|
621
586
|
) {
|
|
622
587
|
return true
|
|
623
588
|
}
|
|
624
589
|
|
|
625
|
-
const includedProps = new Set(notifyOnChangeProps ?? this
|
|
590
|
+
const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps)
|
|
626
591
|
|
|
627
|
-
if (this.options.
|
|
592
|
+
if (this.options.throwErrors) {
|
|
628
593
|
includedProps.add('error')
|
|
629
594
|
}
|
|
630
595
|
|
|
631
|
-
return Object.keys(this
|
|
596
|
+
return Object.keys(this.#currentResult).some((key) => {
|
|
632
597
|
const typedKey = key as keyof QueryObserverResult
|
|
633
|
-
const changed = this
|
|
598
|
+
const changed = this.#currentResult[typedKey] !== prevResult[typedKey]
|
|
634
599
|
return changed && includedProps.has(typedKey)
|
|
635
600
|
})
|
|
636
601
|
}
|
|
@@ -639,22 +604,22 @@ export class QueryObserver<
|
|
|
639
604
|
defaultNotifyOptions.listeners = true
|
|
640
605
|
}
|
|
641
606
|
|
|
642
|
-
this
|
|
607
|
+
this.#notify({ ...defaultNotifyOptions, ...notifyOptions })
|
|
643
608
|
}
|
|
644
609
|
|
|
645
|
-
|
|
646
|
-
const query = this
|
|
610
|
+
#updateQuery(): void {
|
|
611
|
+
const query = this.#client.getQueryCache().build(this.#client, this.options)
|
|
647
612
|
|
|
648
|
-
if (query === this
|
|
613
|
+
if (query === this.#currentQuery) {
|
|
649
614
|
return
|
|
650
615
|
}
|
|
651
616
|
|
|
652
|
-
const prevQuery = this
|
|
617
|
+
const prevQuery = this.#currentQuery as
|
|
653
618
|
| Query<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
654
619
|
| undefined
|
|
655
|
-
this
|
|
656
|
-
this
|
|
657
|
-
this
|
|
620
|
+
this.#currentQuery = query
|
|
621
|
+
this.#currentQueryInitialState = query.state
|
|
622
|
+
this.#previousQueryResult = this.#currentResult
|
|
658
623
|
|
|
659
624
|
if (this.hasListeners()) {
|
|
660
625
|
prevQuery?.removeObserver(this)
|
|
@@ -671,38 +636,36 @@ export class QueryObserver<
|
|
|
671
636
|
notifyOptions.onError = true
|
|
672
637
|
}
|
|
673
638
|
|
|
674
|
-
this
|
|
639
|
+
this.#updateResult(notifyOptions)
|
|
675
640
|
|
|
676
641
|
if (this.hasListeners()) {
|
|
677
|
-
this
|
|
642
|
+
this.#updateTimers()
|
|
678
643
|
}
|
|
679
644
|
}
|
|
680
645
|
|
|
681
|
-
|
|
646
|
+
#notify(notifyOptions: NotifyOptions): void {
|
|
682
647
|
notifyManager.batch(() => {
|
|
683
648
|
// First trigger the configuration callbacks
|
|
684
649
|
if (notifyOptions.onSuccess) {
|
|
685
|
-
this.options.onSuccess?.(this
|
|
686
|
-
this.options.onSettled?.(this
|
|
650
|
+
this.options.onSuccess?.(this.#currentResult.data!)
|
|
651
|
+
this.options.onSettled?.(this.#currentResult.data, null)
|
|
687
652
|
} else if (notifyOptions.onError) {
|
|
688
|
-
this.options.onError?.(this
|
|
689
|
-
this.options.onSettled?.(undefined, this
|
|
653
|
+
this.options.onError?.(this.#currentResult.error!)
|
|
654
|
+
this.options.onSettled?.(undefined, this.#currentResult.error)
|
|
690
655
|
}
|
|
691
656
|
|
|
692
657
|
// Then trigger the listeners
|
|
693
658
|
if (notifyOptions.listeners) {
|
|
694
659
|
this.listeners.forEach((listener) => {
|
|
695
|
-
listener(this
|
|
660
|
+
listener(this.#currentResult)
|
|
696
661
|
})
|
|
697
662
|
}
|
|
698
663
|
|
|
699
664
|
// Then the cache listeners
|
|
700
|
-
|
|
701
|
-
this
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
})
|
|
705
|
-
}
|
|
665
|
+
this.#client.getQueryCache().notify({
|
|
666
|
+
query: this.#currentQuery,
|
|
667
|
+
type: 'observerResultsUpdated',
|
|
668
|
+
})
|
|
706
669
|
})
|
|
707
670
|
}
|
|
708
671
|
}
|
|
@@ -732,9 +695,9 @@ function shouldFetchOnMount(
|
|
|
732
695
|
function shouldFetchOn(
|
|
733
696
|
query: Query<any, any, any, any>,
|
|
734
697
|
options: QueryObserverOptions<any, any, any, any, any>,
|
|
735
|
-
field: typeof options['refetchOnMount'] &
|
|
736
|
-
typeof options['refetchOnWindowFocus'] &
|
|
737
|
-
typeof options['refetchOnReconnect'],
|
|
698
|
+
field: (typeof options)['refetchOnMount'] &
|
|
699
|
+
(typeof options)['refetchOnWindowFocus'] &
|
|
700
|
+
(typeof options)['refetchOnReconnect'],
|
|
738
701
|
) {
|
|
739
702
|
if (options.enabled !== false) {
|
|
740
703
|
const value = typeof field === 'function' ? field(query) : field
|