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