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