@tanstack/query-core 4.24.10 → 5.0.0-alpha.0

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.
Files changed (183) hide show
  1. package/build/lib/focusManager.d.ts +1 -3
  2. package/build/lib/focusManager.esm.js +19 -36
  3. package/build/lib/focusManager.esm.js.map +1 -1
  4. package/build/lib/focusManager.js +19 -38
  5. package/build/lib/focusManager.js.map +1 -1
  6. package/build/lib/focusManager.mjs +19 -36
  7. package/build/lib/focusManager.mjs.map +1 -1
  8. package/build/lib/hydration.esm.js +21 -23
  9. package/build/lib/hydration.esm.js.map +1 -1
  10. package/build/lib/hydration.js +21 -25
  11. package/build/lib/hydration.js.map +1 -1
  12. package/build/lib/hydration.mjs +21 -23
  13. package/build/lib/hydration.mjs.map +1 -1
  14. package/build/lib/index.d.ts +1 -2
  15. package/build/lib/index.esm.js +1 -1
  16. package/build/lib/index.js +2 -8
  17. package/build/lib/index.js.map +1 -1
  18. package/build/lib/index.mjs +1 -1
  19. package/build/lib/infiniteQueryBehavior.d.ts +3 -7
  20. package/build/lib/infiniteQueryBehavior.esm.js +52 -75
  21. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  22. package/build/lib/infiniteQueryBehavior.js +50 -77
  23. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.mjs +52 -75
  25. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  26. package/build/lib/infiniteQueryObserver.d.ts +4 -4
  27. package/build/lib/infiniteQueryObserver.esm.js +18 -26
  28. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  29. package/build/lib/infiniteQueryObserver.js +18 -28
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.mjs +18 -26
  32. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  33. package/build/lib/mutation.d.ts +11 -22
  34. package/build/lib/mutation.esm.js +73 -105
  35. package/build/lib/mutation.esm.js.map +1 -1
  36. package/build/lib/mutation.js +73 -107
  37. package/build/lib/mutation.js.map +1 -1
  38. package/build/lib/mutation.mjs +73 -105
  39. package/build/lib/mutation.mjs.map +1 -1
  40. package/build/lib/mutationCache.d.ts +4 -6
  41. package/build/lib/mutationCache.esm.js +23 -32
  42. package/build/lib/mutationCache.esm.js.map +1 -1
  43. package/build/lib/mutationCache.js +23 -34
  44. package/build/lib/mutationCache.js.map +1 -1
  45. package/build/lib/mutationCache.mjs +23 -32
  46. package/build/lib/mutationCache.mjs.map +1 -1
  47. package/build/lib/mutationObserver.d.ts +4 -9
  48. package/build/lib/mutationObserver.esm.js +43 -72
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +43 -74
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +43 -72
  53. package/build/lib/mutationObserver.mjs.map +1 -1
  54. package/build/lib/notifyManager.esm.js +7 -17
  55. package/build/lib/notifyManager.esm.js.map +1 -1
  56. package/build/lib/notifyManager.js +7 -19
  57. package/build/lib/notifyManager.js.map +1 -1
  58. package/build/lib/notifyManager.mjs +7 -17
  59. package/build/lib/notifyManager.mjs.map +1 -1
  60. package/build/lib/onlineManager.d.ts +1 -3
  61. package/build/lib/onlineManager.esm.js +16 -30
  62. package/build/lib/onlineManager.esm.js.map +1 -1
  63. package/build/lib/onlineManager.js +16 -32
  64. package/build/lib/onlineManager.js.map +1 -1
  65. package/build/lib/onlineManager.mjs +16 -30
  66. package/build/lib/onlineManager.mjs.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +3 -10
  68. package/build/lib/queriesObserver.esm.js +47 -71
  69. package/build/lib/queriesObserver.esm.js.map +1 -1
  70. package/build/lib/queriesObserver.js +49 -75
  71. package/build/lib/queriesObserver.js.map +1 -1
  72. package/build/lib/queriesObserver.mjs +47 -71
  73. package/build/lib/queriesObserver.mjs.map +1 -1
  74. package/build/lib/query.d.ts +14 -21
  75. package/build/lib/query.esm.js +140 -194
  76. package/build/lib/query.esm.js.map +1 -1
  77. package/build/lib/query.js +139 -195
  78. package/build/lib/query.js.map +1 -1
  79. package/build/lib/query.mjs +140 -194
  80. package/build/lib/query.mjs.map +1 -1
  81. package/build/lib/queryCache.d.ts +12 -7
  82. package/build/lib/queryCache.esm.js +21 -45
  83. package/build/lib/queryCache.esm.js.map +1 -1
  84. package/build/lib/queryCache.js +20 -46
  85. package/build/lib/queryCache.js.map +1 -1
  86. package/build/lib/queryCache.mjs +21 -45
  87. package/build/lib/queryCache.mjs.map +1 -1
  88. package/build/lib/queryClient.d.ts +18 -46
  89. package/build/lib/queryClient.esm.js +137 -216
  90. package/build/lib/queryClient.esm.js.map +1 -1
  91. package/build/lib/queryClient.js +136 -217
  92. package/build/lib/queryClient.js.map +1 -1
  93. package/build/lib/queryClient.mjs +137 -216
  94. package/build/lib/queryClient.mjs.map +1 -1
  95. package/build/lib/queryObserver.d.ts +4 -29
  96. package/build/lib/queryObserver.esm.js +176 -258
  97. package/build/lib/queryObserver.esm.js.map +1 -1
  98. package/build/lib/queryObserver.js +176 -260
  99. package/build/lib/queryObserver.js.map +1 -1
  100. package/build/lib/queryObserver.mjs +176 -258
  101. package/build/lib/queryObserver.mjs.map +1 -1
  102. package/build/lib/removable.d.ts +3 -3
  103. package/build/lib/removable.esm.js +10 -14
  104. package/build/lib/removable.esm.js.map +1 -1
  105. package/build/lib/removable.js +10 -16
  106. package/build/lib/removable.js.map +1 -1
  107. package/build/lib/removable.mjs +10 -14
  108. package/build/lib/removable.mjs.map +1 -1
  109. package/build/lib/retryer.d.ts +5 -5
  110. package/build/lib/retryer.esm.js +27 -44
  111. package/build/lib/retryer.esm.js.map +1 -1
  112. package/build/lib/retryer.js +27 -46
  113. package/build/lib/retryer.js.map +1 -1
  114. package/build/lib/retryer.mjs +27 -44
  115. package/build/lib/retryer.mjs.map +1 -1
  116. package/build/lib/subscribable.esm.js +4 -7
  117. package/build/lib/subscribable.esm.js.map +1 -1
  118. package/build/lib/subscribable.js +4 -9
  119. package/build/lib/subscribable.js.map +1 -1
  120. package/build/lib/subscribable.mjs +4 -7
  121. package/build/lib/subscribable.mjs.map +1 -1
  122. package/build/lib/tests/utils.d.ts +3 -12
  123. package/build/lib/types.d.ts +111 -99
  124. package/build/lib/utils.d.ts +8 -18
  125. package/build/lib/utils.esm.js +39 -132
  126. package/build/lib/utils.esm.js.map +1 -1
  127. package/build/lib/utils.js +42 -144
  128. package/build/lib/utils.js.map +1 -1
  129. package/build/lib/utils.mjs +39 -132
  130. package/build/lib/utils.mjs.map +1 -1
  131. package/build/umd/index.development.js +868 -1398
  132. package/build/umd/index.development.js.map +1 -1
  133. package/build/umd/index.production.js +1 -1
  134. package/build/umd/index.production.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/focusManager.ts +17 -24
  137. package/src/index.ts +1 -11
  138. package/src/infiniteQueryBehavior.ts +54 -94
  139. package/src/infiniteQueryObserver.ts +10 -12
  140. package/src/mutation.ts +68 -92
  141. package/src/mutationCache.ts +27 -27
  142. package/src/mutationObserver.ts +60 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +106 -110
  146. package/src/queryCache.ts +42 -41
  147. package/src/queryClient.ts +155 -434
  148. package/src/queryObserver.ts +155 -192
  149. package/src/removable.ts +13 -13
  150. package/src/retryer.ts +5 -5
  151. package/src/tests/focusManager.test.tsx +25 -25
  152. package/src/tests/hydration.test.tsx +167 -81
  153. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  154. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  155. package/src/tests/mutationCache.test.tsx +127 -127
  156. package/src/tests/mutationObserver.test.tsx +1 -31
  157. package/src/tests/mutations.test.tsx +62 -43
  158. package/src/tests/onlineManager.test.tsx +12 -4
  159. package/src/tests/queriesObserver.test.tsx +41 -77
  160. package/src/tests/query.test.tsx +175 -243
  161. package/src/tests/queryCache.test.tsx +170 -93
  162. package/src/tests/queryClient.test.tsx +229 -378
  163. package/src/tests/queryObserver.test.tsx +23 -147
  164. package/src/tests/utils.test.tsx +84 -29
  165. package/src/tests/utils.ts +9 -18
  166. package/src/types.ts +187 -140
  167. package/src/utils.ts +31 -124
  168. package/build/lib/logger.d.ts +0 -8
  169. package/build/lib/logger.esm.js +0 -4
  170. package/build/lib/logger.esm.js.map +0 -1
  171. package/build/lib/logger.js +0 -8
  172. package/build/lib/logger.js.map +0 -1
  173. package/build/lib/logger.mjs +0 -4
  174. package/build/lib/logger.mjs.map +0 -1
  175. package/build/lib/logger.native.d.ts +0 -6
  176. package/build/lib/logger.native.esm.js +0 -12
  177. package/build/lib/logger.native.esm.js.map +0 -1
  178. package/build/lib/logger.native.js +0 -16
  179. package/build/lib/logger.native.js.map +0 -1
  180. package/build/lib/logger.native.mjs +0 -12
  181. package/build/lib/logger.native.mjs.map +0 -1
  182. package/src/logger.native.ts +0 -11
  183. package/src/logger.ts +0 -9
@@ -1,4 +1,4 @@
1
- import type { DefaultedQueryObserverOptions, RefetchPageFilters } from './types'
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 = unknown,
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
- private client: QueryClient
57
- private currentQuery!: Query<TQueryFnData, TError, TQueryData, TQueryKey>
58
- private currentQueryInitialState!: QueryState<TQueryData, TError>
59
- private currentResult!: QueryObserverResult<TData, TError>
60
- private currentResultState?: QueryState<TQueryData, TError>
61
- private currentResultOptions?: QueryObserverOptions<
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
- private previousQueryResult?: QueryObserverResult<TData, TError>
69
- private selectError: TError | null
70
- private selectFn?: (data: TQueryData) => TData
71
- private selectResult?: TData
72
- private staleTimeoutId?: ReturnType<typeof setTimeout>
73
- private refetchIntervalId?: ReturnType<typeof setInterval>
74
- private currentRefetchInterval?: number | false
75
- private trackedProps!: Set<keyof QueryObserverResult>
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.client = client
88
+ this.#client = client
90
89
  this.options = options
91
- this.trackedProps = new Set()
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.currentQuery.addObserver(this)
101
+ this.#currentQuery.addObserver(this)
105
102
 
106
- if (shouldFetchOnMount(this.currentQuery, this.options)) {
107
- this.executeFetch()
103
+ if (shouldFetchOnMount(this.#currentQuery, this.options)) {
104
+ this.#executeFetch()
108
105
  }
109
106
 
110
- this.updateTimers()
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.currentQuery,
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.currentQuery,
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.clearStaleTimeout()
139
- this.clearRefetchInterval()
140
- this.currentQuery.removeObserver(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.currentQuery
151
+ const prevQuery = this.#currentQuery
155
152
 
156
- this.options = this.client.defaultQueryOptions(options)
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.client.getQueryCache().notify({
156
+ this.#client.getQueryCache().notify({
171
157
  type: 'observerOptionsUpdated',
172
- query: this.currentQuery,
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.updateQuery()
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.currentQuery,
183
+ this.#currentQuery,
198
184
  prevQuery,
199
185
  this.options,
200
186
  prevOptions,
201
187
  )
202
188
  ) {
203
- this.executeFetch()
189
+ this.#executeFetch()
204
190
  }
205
191
 
206
192
  // Update result
207
- this.updateResult(notifyOptions)
193
+ this.#updateResult(notifyOptions)
208
194
 
209
195
  // Update stale interval if needed
210
196
  if (
211
197
  mounted &&
212
- (this.currentQuery !== prevQuery ||
198
+ (this.#currentQuery !== prevQuery ||
213
199
  this.options.enabled !== prevOptions.enabled ||
214
200
  this.options.staleTime !== prevOptions.staleTime)
215
201
  ) {
216
- this.updateStaleTimeout()
202
+ this.#updateStaleTimeout()
217
203
  }
218
204
 
219
- const nextRefetchInterval = this.computeRefetchInterval()
205
+ const nextRefetchInterval = this.#computeRefetchInterval()
220
206
 
221
207
  // Update refetch interval if needed
222
208
  if (
223
209
  mounted &&
224
- (this.currentQuery !== prevQuery ||
210
+ (this.#currentQuery !== prevQuery ||
225
211
  this.options.enabled !== prevOptions.enabled ||
226
- nextRefetchInterval !== this.currentRefetchInterval)
212
+ nextRefetchInterval !== this.#currentRefetchInterval)
227
213
  ) {
228
- this.updateRefetchInterval(nextRefetchInterval)
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.client.getQueryCache().build(this.client, options)
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.currentResult
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.trackedProps.add(key as keyof QueryObserverResult)
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.currentQuery
256
+ return this.#currentQuery
271
257
  }
272
258
 
273
- remove(): void {
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.client.defaultQueryOptions(options)
276
+ const defaultedOptions = this.#client.defaultQueryOptions(options)
299
277
 
300
- const query = this.client
278
+ const query = this.#client
301
279
  .getQueryCache()
302
- .build(this.client, defaultedOptions)
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.executeFetch({
289
+ return this.#executeFetch({
312
290
  ...fetchOptions,
313
291
  cancelRefetch: fetchOptions.cancelRefetch ?? true,
314
292
  }).then(() => {
315
- this.updateResult()
316
- return this.currentResult
293
+ this.#updateResult()
294
+ return this.#currentResult
317
295
  })
318
296
  }
319
297
 
320
- private executeFetch(
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.updateQuery()
302
+ this.#updateQuery()
325
303
 
326
304
  // Fetch
327
- let promise: Promise<TQueryData | undefined> = this.currentQuery.fetch(
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
- private updateStaleTimeout(): void {
340
- this.clearStaleTimeout()
317
+ #updateStaleTimeout(): void {
318
+ this.#clearStaleTimeout()
341
319
 
342
320
  if (
343
321
  isServer ||
344
- this.currentResult.isStale ||
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.currentResult.dataUpdatedAt,
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.staleTimeoutId = setTimeout(() => {
360
- if (!this.currentResult.isStale) {
361
- this.updateResult()
337
+ this.#staleTimeoutId = setTimeout(() => {
338
+ if (!this.#currentResult.isStale) {
339
+ this.#updateResult()
362
340
  }
363
341
  }, timeout)
364
342
  }
365
343
 
366
- private computeRefetchInterval() {
344
+ #computeRefetchInterval() {
367
345
  return typeof this.options.refetchInterval === 'function'
368
- ? this.options.refetchInterval(this.currentResult.data, this.currentQuery)
346
+ ? this.options.refetchInterval(
347
+ this.#currentResult.data,
348
+ this.#currentQuery,
349
+ )
369
350
  : this.options.refetchInterval ?? false
370
351
  }
371
352
 
372
- private updateRefetchInterval(nextInterval: number | false): void {
373
- this.clearRefetchInterval()
353
+ #updateRefetchInterval(nextInterval: number | false): void {
354
+ this.#clearRefetchInterval()
374
355
 
375
- this.currentRefetchInterval = nextInterval
356
+ this.#currentRefetchInterval = nextInterval
376
357
 
377
358
  if (
378
359
  isServer ||
379
360
  this.options.enabled === false ||
380
- !isValidTimeout(this.currentRefetchInterval) ||
381
- this.currentRefetchInterval === 0
361
+ !isValidTimeout(this.#currentRefetchInterval) ||
362
+ this.#currentRefetchInterval === 0
382
363
  ) {
383
364
  return
384
365
  }
385
366
 
386
- this.refetchIntervalId = setInterval(() => {
367
+ this.#refetchIntervalId = setInterval(() => {
387
368
  if (
388
369
  this.options.refetchIntervalInBackground ||
389
370
  focusManager.isFocused()
390
371
  ) {
391
- this.executeFetch()
372
+ this.#executeFetch()
392
373
  }
393
- }, this.currentRefetchInterval)
374
+ }, this.#currentRefetchInterval)
394
375
  }
395
376
 
396
- private updateTimers(): void {
397
- this.updateStaleTimeout()
398
- this.updateRefetchInterval(this.computeRefetchInterval())
377
+ #updateTimers(): void {
378
+ this.#updateStaleTimeout()
379
+ this.#updateRefetchInterval(this.#computeRefetchInterval())
399
380
  }
400
381
 
401
- private clearStaleTimeout(): void {
402
- if (this.staleTimeoutId) {
403
- clearTimeout(this.staleTimeoutId)
404
- this.staleTimeoutId = undefined
382
+ #clearStaleTimeout(): void {
383
+ if (this.#staleTimeoutId) {
384
+ clearTimeout(this.#staleTimeoutId)
385
+ this.#staleTimeoutId = undefined
405
386
  }
406
387
  }
407
388
 
408
- private clearRefetchInterval(): void {
409
- if (this.refetchIntervalId) {
410
- clearInterval(this.refetchIntervalId)
411
- this.refetchIntervalId = undefined
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.currentQuery
406
+ const prevQuery = this.#currentQuery
426
407
  const prevOptions = this.options
427
- const prevResult = this.currentResult as
408
+ const prevResult = this.#currentResult as
428
409
  | QueryObserverResult<TData, TError>
429
410
  | undefined
430
- const prevResultState = this.currentResultState
431
- const prevResultOptions = this.currentResultOptions
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.currentQueryInitialState
416
+ : this.#currentQueryInitialState
436
417
  const prevQueryResult = queryChange
437
- ? this.currentResult
438
- : this.previousQueryResult
418
+ ? this.#currentResult
419
+ : this.#previousQueryResult
439
420
 
440
421
  const { state } = query
441
- let { dataUpdatedAt, error, errorUpdatedAt, fetchStatus, status } = state
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 = 'loading'
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
- else if (options.select && typeof state.data !== 'undefined') {
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.selectFn
454
+ options.select === this.#selectFn
487
455
  ) {
488
- data = this.selectResult
456
+ data = this.#selectResult
489
457
  } else {
490
458
  try {
491
- this.selectFn = options.select
459
+ this.#selectFn = options.select
492
460
  data = options.select(state.data)
493
461
  data = replaceData(prevResult?.data, data, options)
494
- this.selectResult = data
495
- this.selectError = null
462
+ this.#selectResult = data
463
+ this.#selectError = null
496
464
  } catch (selectError) {
497
- if (process.env.NODE_ENV !== 'production') {
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 === 'loading'
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
- ? (options.placeholderData as PlaceholderDataFunction<TQueryData>)()
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.selectError = null
498
+ this.#selectError = null
532
499
  } catch (selectError) {
533
- if (process.env.NODE_ENV !== 'production') {
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.selectError) {
549
- error = this.selectError as any
550
- data = this.selectResult
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 isLoading = status === 'loading'
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
- isLoading,
528
+ isPending,
563
529
  isSuccess: status === 'success',
564
530
  isError,
565
- isInitialLoading: isLoading && isFetching,
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 && !isLoading,
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.currentResult as
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.currentQuery, this.options)
598
- this.currentResultState = this.currentQuery.state
599
- this.currentResultOptions = this.options
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.currentResult = nextResult
571
+ this.#currentResult = nextResult
607
572
 
608
573
  // Determine which callbacks to trigger
609
- const defaultNotifyOptions: NotifyOptions = { cache: true }
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.trackedProps.size)
585
+ (!notifyOnChangeProps && !this.#trackedProps.size)
621
586
  ) {
622
587
  return true
623
588
  }
624
589
 
625
- const includedProps = new Set(notifyOnChangeProps ?? this.trackedProps)
590
+ const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps)
626
591
 
627
- if (this.options.useErrorBoundary) {
592
+ if (this.options.throwErrors) {
628
593
  includedProps.add('error')
629
594
  }
630
595
 
631
- return Object.keys(this.currentResult).some((key) => {
596
+ return Object.keys(this.#currentResult).some((key) => {
632
597
  const typedKey = key as keyof QueryObserverResult
633
- const changed = this.currentResult[typedKey] !== prevResult[typedKey]
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.notify({ ...defaultNotifyOptions, ...notifyOptions })
607
+ this.#notify({ ...defaultNotifyOptions, ...notifyOptions })
643
608
  }
644
609
 
645
- private updateQuery(): void {
646
- const query = this.client.getQueryCache().build(this.client, this.options)
610
+ #updateQuery(): void {
611
+ const query = this.#client.getQueryCache().build(this.#client, this.options)
647
612
 
648
- if (query === this.currentQuery) {
613
+ if (query === this.#currentQuery) {
649
614
  return
650
615
  }
651
616
 
652
- const prevQuery = this.currentQuery as
617
+ const prevQuery = this.#currentQuery as
653
618
  | Query<TQueryFnData, TError, TQueryData, TQueryKey>
654
619
  | undefined
655
- this.currentQuery = query
656
- this.currentQueryInitialState = query.state
657
- this.previousQueryResult = this.currentResult
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.updateResult(notifyOptions)
639
+ this.#updateResult(notifyOptions)
675
640
 
676
641
  if (this.hasListeners()) {
677
- this.updateTimers()
642
+ this.#updateTimers()
678
643
  }
679
644
  }
680
645
 
681
- private notify(notifyOptions: NotifyOptions): void {
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.currentResult.data!)
686
- this.options.onSettled?.(this.currentResult.data!, null)
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.currentResult.error!)
689
- this.options.onSettled?.(undefined, this.currentResult.error!)
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.currentResult)
660
+ listener(this.#currentResult)
696
661
  })
697
662
  }
698
663
 
699
664
  // Then the cache listeners
700
- if (notifyOptions.cache) {
701
- this.client.getQueryCache().notify({
702
- query: this.currentQuery,
703
- type: 'observerResultsUpdated',
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