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