@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.
Files changed (184) 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 +42 -73
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +42 -75
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +42 -73
  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 +867 -1399
  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 +58 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +107 -111
  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/subscribable.ts +1 -1
  152. package/src/tests/focusManager.test.tsx +25 -25
  153. package/src/tests/hydration.test.tsx +167 -81
  154. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  155. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  156. package/src/tests/mutationCache.test.tsx +127 -127
  157. package/src/tests/mutationObserver.test.tsx +1 -31
  158. package/src/tests/mutations.test.tsx +62 -43
  159. package/src/tests/onlineManager.test.tsx +12 -4
  160. package/src/tests/queriesObserver.test.tsx +41 -77
  161. package/src/tests/query.test.tsx +175 -243
  162. package/src/tests/queryCache.test.tsx +170 -93
  163. package/src/tests/queryClient.test.tsx +229 -378
  164. package/src/tests/queryObserver.test.tsx +23 -147
  165. package/src/tests/utils.test.tsx +84 -29
  166. package/src/tests/utils.ts +9 -18
  167. package/src/types.ts +187 -140
  168. package/src/utils.ts +31 -124
  169. package/build/lib/logger.d.ts +0 -8
  170. package/build/lib/logger.esm.js +0 -4
  171. package/build/lib/logger.esm.js.map +0 -1
  172. package/build/lib/logger.js +0 -8
  173. package/build/lib/logger.js.map +0 -1
  174. package/build/lib/logger.mjs +0 -4
  175. package/build/lib/logger.mjs.map +0 -1
  176. package/build/lib/logger.native.d.ts +0 -6
  177. package/build/lib/logger.native.esm.js +0 -12
  178. package/build/lib/logger.native.esm.js.map +0 -1
  179. package/build/lib/logger.native.js +0 -16
  180. package/build/lib/logger.native.js.map +0 -1
  181. package/build/lib/logger.native.mjs +0 -12
  182. package/build/lib/logger.native.mjs.map +0 -1
  183. package/src/logger.native.ts +0 -11
  184. package/src/logger.ts +0 -9
@@ -5,110 +5,105 @@ import { Subscribable } from './subscribable.mjs';
5
5
  import { canFetch, isCancelledError } from './retryer.mjs';
6
6
 
7
7
  class QueryObserver extends Subscribable {
8
+ #client;
9
+ #currentQuery = undefined;
10
+ #currentQueryInitialState = undefined;
11
+ #currentResult = undefined;
12
+ #currentResultState;
13
+ #currentResultOptions;
14
+ #previousQueryResult;
15
+ #selectError;
16
+ #selectFn;
17
+ #selectResult;
18
+ #staleTimeoutId;
19
+ #refetchIntervalId;
20
+ #currentRefetchInterval;
21
+ #trackedProps = new Set();
8
22
  constructor(client, options) {
9
23
  super();
10
- this.client = client;
24
+ this.#client = client;
11
25
  this.options = options;
12
- this.trackedProps = new Set();
13
- this.selectError = null;
26
+ this.#selectError = null;
14
27
  this.bindMethods();
15
28
  this.setOptions(options);
16
29
  }
17
-
18
30
  bindMethods() {
19
- this.remove = this.remove.bind(this);
20
31
  this.refetch = this.refetch.bind(this);
21
32
  }
22
-
23
33
  onSubscribe() {
24
34
  if (this.listeners.length === 1) {
25
- this.currentQuery.addObserver(this);
26
-
27
- if (shouldFetchOnMount(this.currentQuery, this.options)) {
28
- this.executeFetch();
35
+ this.#currentQuery.addObserver(this);
36
+ if (shouldFetchOnMount(this.#currentQuery, this.options)) {
37
+ this.#executeFetch();
29
38
  }
30
-
31
- this.updateTimers();
39
+ this.#updateTimers();
32
40
  }
33
41
  }
34
-
35
42
  onUnsubscribe() {
36
43
  if (!this.listeners.length) {
37
44
  this.destroy();
38
45
  }
39
46
  }
40
-
41
47
  shouldFetchOnReconnect() {
42
- return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
48
+ return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnReconnect);
43
49
  }
44
-
45
50
  shouldFetchOnWindowFocus() {
46
- return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
51
+ return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
47
52
  }
48
-
49
53
  destroy() {
50
54
  this.listeners = [];
51
- this.clearStaleTimeout();
52
- this.clearRefetchInterval();
53
- this.currentQuery.removeObserver(this);
55
+ this.#clearStaleTimeout();
56
+ this.#clearRefetchInterval();
57
+ this.#currentQuery.removeObserver(this);
54
58
  }
55
-
56
59
  setOptions(options, notifyOptions) {
57
60
  const prevOptions = this.options;
58
- const prevQuery = this.currentQuery;
59
- this.options = this.client.defaultQueryOptions(options);
60
-
61
- if (process.env.NODE_ENV !== 'production' && typeof (options == null ? void 0 : options.isDataEqual) !== 'undefined') {
62
- this.client.getLogger().error("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");
63
- }
64
-
61
+ const prevQuery = this.#currentQuery;
62
+ this.options = this.#client.defaultQueryOptions(options);
65
63
  if (!shallowEqualObjects(prevOptions, this.options)) {
66
- this.client.getQueryCache().notify({
64
+ this.#client.getQueryCache().notify({
67
65
  type: 'observerOptionsUpdated',
68
- query: this.currentQuery,
66
+ query: this.#currentQuery,
69
67
  observer: this
70
68
  });
71
69
  }
72
-
73
70
  if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
74
71
  throw new Error('Expected enabled to be a boolean');
75
- } // Keep previous query key if the user does not supply one
76
-
72
+ }
77
73
 
74
+ // Keep previous query key if the user does not supply one
78
75
  if (!this.options.queryKey) {
79
76
  this.options.queryKey = prevOptions.queryKey;
80
77
  }
78
+ this.#updateQuery();
79
+ const mounted = this.hasListeners();
81
80
 
82
- this.updateQuery();
83
- const mounted = this.hasListeners(); // Fetch if there are subscribers
84
-
85
- if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
86
- this.executeFetch();
87
- } // Update result
88
-
81
+ // Fetch if there are subscribers
82
+ if (mounted && shouldFetchOptionally(this.#currentQuery, prevQuery, this.options, prevOptions)) {
83
+ this.#executeFetch();
84
+ }
89
85
 
90
- this.updateResult(notifyOptions); // Update stale interval if needed
86
+ // Update result
87
+ this.#updateResult(notifyOptions);
91
88
 
92
- if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
93
- this.updateStaleTimeout();
89
+ // Update stale interval if needed
90
+ if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
91
+ this.#updateStaleTimeout();
94
92
  }
93
+ const nextRefetchInterval = this.#computeRefetchInterval();
95
94
 
96
- const nextRefetchInterval = this.computeRefetchInterval(); // Update refetch interval if needed
97
-
98
- if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
99
- this.updateRefetchInterval(nextRefetchInterval);
95
+ // Update refetch interval if needed
96
+ if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.#currentRefetchInterval)) {
97
+ this.#updateRefetchInterval(nextRefetchInterval);
100
98
  }
101
99
  }
102
-
103
100
  getOptimisticResult(options) {
104
- const query = this.client.getQueryCache().build(this.client, options);
101
+ const query = this.#client.getQueryCache().build(this.#client, options);
105
102
  return this.createResult(query, options);
106
103
  }
107
-
108
104
  getCurrentResult() {
109
- return this.currentResult;
105
+ return this.#currentResult;
110
106
  }
111
-
112
107
  trackResult(result) {
113
108
  const trackedResult = {};
114
109
  Object.keys(result).forEach(key => {
@@ -116,243 +111,199 @@ class QueryObserver extends Subscribable {
116
111
  configurable: false,
117
112
  enumerable: true,
118
113
  get: () => {
119
- this.trackedProps.add(key);
114
+ this.#trackedProps.add(key);
120
115
  return result[key];
121
116
  }
122
117
  });
123
118
  });
124
119
  return trackedResult;
125
120
  }
126
-
127
121
  getCurrentQuery() {
128
- return this.currentQuery;
129
- }
130
-
131
- remove() {
132
- this.client.getQueryCache().remove(this.currentQuery);
122
+ return this.#currentQuery;
133
123
  }
134
-
135
124
  refetch({
136
- refetchPage,
137
125
  ...options
138
126
  } = {}) {
139
- return this.fetch({ ...options,
140
- meta: {
141
- refetchPage
142
- }
127
+ return this.fetch({
128
+ ...options
143
129
  });
144
130
  }
145
-
146
131
  fetchOptimistic(options) {
147
- const defaultedOptions = this.client.defaultQueryOptions(options);
148
- const query = this.client.getQueryCache().build(this.client, defaultedOptions);
132
+ const defaultedOptions = this.#client.defaultQueryOptions(options);
133
+ const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
149
134
  query.isFetchingOptimistic = true;
150
135
  return query.fetch().then(() => this.createResult(query, defaultedOptions));
151
136
  }
152
-
153
137
  fetch(fetchOptions) {
154
- var _fetchOptions$cancelR;
155
-
156
- return this.executeFetch({ ...fetchOptions,
157
- cancelRefetch: (_fetchOptions$cancelR = fetchOptions.cancelRefetch) != null ? _fetchOptions$cancelR : true
138
+ return this.#executeFetch({
139
+ ...fetchOptions,
140
+ cancelRefetch: fetchOptions.cancelRefetch ?? true
158
141
  }).then(() => {
159
- this.updateResult();
160
- return this.currentResult;
142
+ this.#updateResult();
143
+ return this.#currentResult;
161
144
  });
162
145
  }
163
-
164
- executeFetch(fetchOptions) {
146
+ #executeFetch(fetchOptions) {
165
147
  // Make sure we reference the latest query as the current one might have been removed
166
- this.updateQuery(); // Fetch
148
+ this.#updateQuery();
167
149
 
168
- let promise = this.currentQuery.fetch(this.options, fetchOptions);
169
-
170
- if (!(fetchOptions != null && fetchOptions.throwOnError)) {
150
+ // Fetch
151
+ let promise = this.#currentQuery.fetch(this.options, fetchOptions);
152
+ if (!fetchOptions?.throwOnError) {
171
153
  promise = promise.catch(noop);
172
154
  }
173
-
174
155
  return promise;
175
156
  }
176
-
177
- updateStaleTimeout() {
178
- this.clearStaleTimeout();
179
-
180
- if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
157
+ #updateStaleTimeout() {
158
+ this.#clearStaleTimeout();
159
+ if (isServer || this.#currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
181
160
  return;
182
161
  }
162
+ const time = timeUntilStale(this.#currentResult.dataUpdatedAt, this.options.staleTime);
183
163
 
184
- const time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime); // The timeout is sometimes triggered 1 ms before the stale time expiration.
164
+ // The timeout is sometimes triggered 1 ms before the stale time expiration.
185
165
  // To mitigate this issue we always add 1 ms to the timeout.
186
-
187
166
  const timeout = time + 1;
188
- this.staleTimeoutId = setTimeout(() => {
189
- if (!this.currentResult.isStale) {
190
- this.updateResult();
167
+ this.#staleTimeoutId = setTimeout(() => {
168
+ if (!this.#currentResult.isStale) {
169
+ this.#updateResult();
191
170
  }
192
171
  }, timeout);
193
172
  }
194
-
195
- computeRefetchInterval() {
196
- var _this$options$refetch;
197
-
198
- return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.currentResult.data, this.currentQuery) : (_this$options$refetch = this.options.refetchInterval) != null ? _this$options$refetch : false;
173
+ #computeRefetchInterval() {
174
+ return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval ?? false;
199
175
  }
200
-
201
- updateRefetchInterval(nextInterval) {
202
- this.clearRefetchInterval();
203
- this.currentRefetchInterval = nextInterval;
204
-
205
- if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
176
+ #updateRefetchInterval(nextInterval) {
177
+ this.#clearRefetchInterval();
178
+ this.#currentRefetchInterval = nextInterval;
179
+ if (isServer || this.options.enabled === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
206
180
  return;
207
181
  }
208
-
209
- this.refetchIntervalId = setInterval(() => {
182
+ this.#refetchIntervalId = setInterval(() => {
210
183
  if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
211
- this.executeFetch();
184
+ this.#executeFetch();
212
185
  }
213
- }, this.currentRefetchInterval);
186
+ }, this.#currentRefetchInterval);
214
187
  }
215
-
216
- updateTimers() {
217
- this.updateStaleTimeout();
218
- this.updateRefetchInterval(this.computeRefetchInterval());
188
+ #updateTimers() {
189
+ this.#updateStaleTimeout();
190
+ this.#updateRefetchInterval(this.#computeRefetchInterval());
219
191
  }
220
-
221
- clearStaleTimeout() {
222
- if (this.staleTimeoutId) {
223
- clearTimeout(this.staleTimeoutId);
224
- this.staleTimeoutId = undefined;
192
+ #clearStaleTimeout() {
193
+ if (this.#staleTimeoutId) {
194
+ clearTimeout(this.#staleTimeoutId);
195
+ this.#staleTimeoutId = undefined;
225
196
  }
226
197
  }
227
-
228
- clearRefetchInterval() {
229
- if (this.refetchIntervalId) {
230
- clearInterval(this.refetchIntervalId);
231
- this.refetchIntervalId = undefined;
198
+ #clearRefetchInterval() {
199
+ if (this.#refetchIntervalId) {
200
+ clearInterval(this.#refetchIntervalId);
201
+ this.#refetchIntervalId = undefined;
232
202
  }
233
203
  }
234
-
235
204
  createResult(query, options) {
236
- const prevQuery = this.currentQuery;
205
+ const prevQuery = this.#currentQuery;
237
206
  const prevOptions = this.options;
238
- const prevResult = this.currentResult;
239
- const prevResultState = this.currentResultState;
240
- const prevResultOptions = this.currentResultOptions;
207
+ const prevResult = this.#currentResult;
208
+ const prevResultState = this.#currentResultState;
209
+ const prevResultOptions = this.#currentResultOptions;
241
210
  const queryChange = query !== prevQuery;
242
- const queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
243
- const prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
211
+ const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
212
+ const prevQueryResult = queryChange ? this.#currentResult : this.#previousQueryResult;
244
213
  const {
245
214
  state
246
215
  } = query;
247
216
  let {
248
- dataUpdatedAt,
249
217
  error,
250
218
  errorUpdatedAt,
251
219
  fetchStatus,
252
220
  status
253
221
  } = state;
254
- let isPreviousData = false;
255
222
  let isPlaceholderData = false;
256
- let data; // Optimistically set result in fetching state if needed
223
+ let data;
257
224
 
225
+ // Optimistically set result in fetching state if needed
258
226
  if (options._optimisticResults) {
259
227
  const mounted = this.hasListeners();
260
228
  const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
261
229
  const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
262
-
263
230
  if (fetchOnMount || fetchOptionally) {
264
231
  fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
265
-
266
- if (!dataUpdatedAt) {
267
- status = 'loading';
232
+ if (!state.dataUpdatedAt) {
233
+ status = 'pending';
268
234
  }
269
235
  }
270
-
271
236
  if (options._optimisticResults === 'isRestoring') {
272
237
  fetchStatus = 'idle';
273
238
  }
274
- } // Keep previous data if needed
275
-
239
+ }
276
240
 
277
- if (options.keepPreviousData && !state.dataUpdatedAt && prevQueryResult != null && prevQueryResult.isSuccess && status !== 'error') {
278
- data = prevQueryResult.data;
279
- dataUpdatedAt = prevQueryResult.dataUpdatedAt;
280
- status = prevQueryResult.status;
281
- isPreviousData = true;
282
- } // Select data if needed
283
- else if (options.select && typeof state.data !== 'undefined') {
241
+ // Select data if needed
242
+ if (options.select && typeof state.data !== 'undefined') {
284
243
  // Memoize select result
285
- if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
286
- data = this.selectResult;
244
+ if (prevResult && state.data === prevResultState?.data && options.select === this.#selectFn) {
245
+ data = this.#selectResult;
287
246
  } else {
288
247
  try {
289
- this.selectFn = options.select;
248
+ this.#selectFn = options.select;
290
249
  data = options.select(state.data);
291
- data = replaceData(prevResult == null ? void 0 : prevResult.data, data, options);
292
- this.selectResult = data;
293
- this.selectError = null;
250
+ data = replaceData(prevResult?.data, data, options);
251
+ this.#selectResult = data;
252
+ this.#selectError = null;
294
253
  } catch (selectError) {
295
- if (process.env.NODE_ENV !== 'production') {
296
- this.client.getLogger().error(selectError);
297
- }
298
-
299
- this.selectError = selectError;
254
+ this.#selectError = selectError;
300
255
  }
301
256
  }
302
- } // Use query data
257
+ }
258
+ // Use query data
303
259
  else {
304
260
  data = state.data;
305
- } // Show placeholder data if needed
306
-
261
+ }
307
262
 
308
- if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'loading') {
309
- let placeholderData; // Memoize placeholder data
263
+ // Show placeholder data if needed
264
+ if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'pending') {
265
+ let placeholderData;
310
266
 
311
- if (prevResult != null && prevResult.isPlaceholderData && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
267
+ // Memoize placeholder data
268
+ if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
312
269
  placeholderData = prevResult.data;
313
270
  } else {
314
- placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData() : options.placeholderData;
315
-
271
+ placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(prevQueryResult?.data) : options.placeholderData;
316
272
  if (options.select && typeof placeholderData !== 'undefined') {
317
273
  try {
318
274
  placeholderData = options.select(placeholderData);
319
- this.selectError = null;
275
+ this.#selectError = null;
320
276
  } catch (selectError) {
321
- if (process.env.NODE_ENV !== 'production') {
322
- this.client.getLogger().error(selectError);
323
- }
324
-
325
- this.selectError = selectError;
277
+ this.#selectError = selectError;
326
278
  }
327
279
  }
328
280
  }
329
-
330
281
  if (typeof placeholderData !== 'undefined') {
331
282
  status = 'success';
332
- data = replaceData(prevResult == null ? void 0 : prevResult.data, placeholderData, options);
283
+ data = replaceData(prevResult?.data, placeholderData, options);
333
284
  isPlaceholderData = true;
334
285
  }
335
286
  }
336
-
337
- if (this.selectError) {
338
- error = this.selectError;
339
- data = this.selectResult;
287
+ if (this.#selectError) {
288
+ error = this.#selectError;
289
+ data = this.#selectResult;
340
290
  errorUpdatedAt = Date.now();
341
291
  status = 'error';
342
292
  }
343
-
344
293
  const isFetching = fetchStatus === 'fetching';
345
- const isLoading = status === 'loading';
294
+ const isPending = status === 'pending';
346
295
  const isError = status === 'error';
296
+ const isLoading = isPending && isFetching;
347
297
  const result = {
348
298
  status,
349
299
  fetchStatus,
350
- isLoading,
300
+ isPending,
351
301
  isSuccess: status === 'success',
352
302
  isError,
353
- isInitialLoading: isLoading && isFetching,
303
+ isInitialLoading: isLoading,
304
+ isLoading,
354
305
  data,
355
- dataUpdatedAt,
306
+ dataUpdatedAt: state.dataUpdatedAt,
356
307
  error,
357
308
  errorUpdatedAt,
358
309
  failureCount: state.fetchFailureCount,
@@ -361,159 +312,126 @@ class QueryObserver extends Subscribable {
361
312
  isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
362
313
  isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
363
314
  isFetching,
364
- isRefetching: isFetching && !isLoading,
315
+ isRefetching: isFetching && !isPending,
365
316
  isLoadingError: isError && state.dataUpdatedAt === 0,
366
317
  isPaused: fetchStatus === 'paused',
367
318
  isPlaceholderData,
368
- isPreviousData,
369
319
  isRefetchError: isError && state.dataUpdatedAt !== 0,
370
320
  isStale: isStale(query, options),
371
- refetch: this.refetch,
372
- remove: this.remove
321
+ refetch: this.refetch
373
322
  };
374
323
  return result;
375
324
  }
325
+ #updateResult(notifyOptions) {
326
+ const prevResult = this.#currentResult;
327
+ const nextResult = this.createResult(this.#currentQuery, this.options);
328
+ this.#currentResultState = this.#currentQuery.state;
329
+ this.#currentResultOptions = this.options;
376
330
 
377
- updateResult(notifyOptions) {
378
- const prevResult = this.currentResult;
379
- const nextResult = this.createResult(this.currentQuery, this.options);
380
- this.currentResultState = this.currentQuery.state;
381
- this.currentResultOptions = this.options; // Only notify and update result if something has changed
382
-
331
+ // Only notify and update result if something has changed
383
332
  if (shallowEqualObjects(nextResult, prevResult)) {
384
333
  return;
385
334
  }
335
+ this.#currentResult = nextResult;
386
336
 
387
- this.currentResult = nextResult; // Determine which callbacks to trigger
388
-
389
- const defaultNotifyOptions = {
390
- cache: true
391
- };
392
-
337
+ // Determine which callbacks to trigger
338
+ const defaultNotifyOptions = {};
393
339
  const shouldNotifyListeners = () => {
394
340
  if (!prevResult) {
395
341
  return true;
396
342
  }
397
-
398
343
  const {
399
344
  notifyOnChangeProps
400
345
  } = this.options;
401
-
402
- if (notifyOnChangeProps === 'all' || !notifyOnChangeProps && !this.trackedProps.size) {
346
+ if (notifyOnChangeProps === 'all' || !notifyOnChangeProps && !this.#trackedProps.size) {
403
347
  return true;
404
348
  }
405
-
406
- const includedProps = new Set(notifyOnChangeProps != null ? notifyOnChangeProps : this.trackedProps);
407
-
408
- if (this.options.useErrorBoundary) {
349
+ const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
350
+ if (this.options.throwErrors) {
409
351
  includedProps.add('error');
410
352
  }
411
-
412
- return Object.keys(this.currentResult).some(key => {
353
+ return Object.keys(this.#currentResult).some(key => {
413
354
  const typedKey = key;
414
- const changed = this.currentResult[typedKey] !== prevResult[typedKey];
355
+ const changed = this.#currentResult[typedKey] !== prevResult[typedKey];
415
356
  return changed && includedProps.has(typedKey);
416
357
  });
417
358
  };
418
-
419
- if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
359
+ if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {
420
360
  defaultNotifyOptions.listeners = true;
421
361
  }
422
-
423
- this.notify({ ...defaultNotifyOptions,
362
+ this.#notify({
363
+ ...defaultNotifyOptions,
424
364
  ...notifyOptions
425
365
  });
426
366
  }
427
-
428
- updateQuery() {
429
- const query = this.client.getQueryCache().build(this.client, this.options);
430
-
431
- if (query === this.currentQuery) {
367
+ #updateQuery() {
368
+ const query = this.#client.getQueryCache().build(this.#client, this.options);
369
+ if (query === this.#currentQuery) {
432
370
  return;
433
371
  }
434
-
435
- const prevQuery = this.currentQuery;
436
- this.currentQuery = query;
437
- this.currentQueryInitialState = query.state;
438
- this.previousQueryResult = this.currentResult;
439
-
372
+ const prevQuery = this.#currentQuery;
373
+ this.#currentQuery = query;
374
+ this.#currentQueryInitialState = query.state;
375
+ this.#previousQueryResult = this.#currentResult;
440
376
  if (this.hasListeners()) {
441
- prevQuery == null ? void 0 : prevQuery.removeObserver(this);
377
+ prevQuery?.removeObserver(this);
442
378
  query.addObserver(this);
443
379
  }
444
380
  }
445
-
446
381
  onQueryUpdate(action) {
447
382
  const notifyOptions = {};
448
-
449
383
  if (action.type === 'success') {
450
384
  notifyOptions.onSuccess = !action.manual;
451
385
  } else if (action.type === 'error' && !isCancelledError(action.error)) {
452
386
  notifyOptions.onError = true;
453
387
  }
454
-
455
- this.updateResult(notifyOptions);
456
-
388
+ this.#updateResult(notifyOptions);
457
389
  if (this.hasListeners()) {
458
- this.updateTimers();
390
+ this.#updateTimers();
459
391
  }
460
392
  }
461
-
462
- notify(notifyOptions) {
393
+ #notify(notifyOptions) {
463
394
  notifyManager.batch(() => {
464
395
  // First trigger the configuration callbacks
465
396
  if (notifyOptions.onSuccess) {
466
- var _this$options$onSucce, _this$options, _this$options$onSettl, _this$options2;
467
-
468
- (_this$options$onSucce = (_this$options = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options, this.currentResult.data);
469
- (_this$options$onSettl = (_this$options2 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options2, this.currentResult.data, null);
397
+ this.options.onSuccess?.(this.#currentResult.data);
398
+ this.options.onSettled?.(this.#currentResult.data, null);
470
399
  } else if (notifyOptions.onError) {
471
- var _this$options$onError, _this$options3, _this$options$onSettl2, _this$options4;
472
-
473
- (_this$options$onError = (_this$options3 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options3, this.currentResult.error);
474
- (_this$options$onSettl2 = (_this$options4 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options4, undefined, this.currentResult.error);
475
- } // Then trigger the listeners
476
-
400
+ this.options.onError?.(this.#currentResult.error);
401
+ this.options.onSettled?.(undefined, this.#currentResult.error);
402
+ }
477
403
 
404
+ // Then trigger the listeners
478
405
  if (notifyOptions.listeners) {
479
406
  this.listeners.forEach(listener => {
480
- listener(this.currentResult);
481
- });
482
- } // Then the cache listeners
483
-
484
-
485
- if (notifyOptions.cache) {
486
- this.client.getQueryCache().notify({
487
- query: this.currentQuery,
488
- type: 'observerResultsUpdated'
407
+ listener(this.#currentResult);
489
408
  });
490
409
  }
410
+
411
+ // Then the cache listeners
412
+ this.#client.getQueryCache().notify({
413
+ query: this.#currentQuery,
414
+ type: 'observerResultsUpdated'
415
+ });
491
416
  });
492
417
  }
493
-
494
418
  }
495
-
496
419
  function shouldLoadOnMount(query, options) {
497
420
  return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
498
421
  }
499
-
500
422
  function shouldFetchOnMount(query, options) {
501
423
  return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
502
424
  }
503
-
504
425
  function shouldFetchOn(query, options, field) {
505
426
  if (options.enabled !== false) {
506
427
  const value = typeof field === 'function' ? field(query) : field;
507
428
  return value === 'always' || value !== false && isStale(query, options);
508
429
  }
509
-
510
430
  return false;
511
431
  }
512
-
513
432
  function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
514
433
  return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
515
434
  }
516
-
517
435
  function isStale(query, options) {
518
436
  return query.isStaleByTime(options.staleTime);
519
437
  }