@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
|
@@ -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
|
|
24
|
+
this.#client = client;
|
|
11
25
|
this.options = options;
|
|
12
|
-
this
|
|
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
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
48
|
+
return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnReconnect);
|
|
43
49
|
}
|
|
44
|
-
|
|
45
50
|
shouldFetchOnWindowFocus() {
|
|
46
|
-
return shouldFetchOn(this
|
|
51
|
+
return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
47
52
|
}
|
|
48
|
-
|
|
49
53
|
destroy() {
|
|
50
54
|
this.listeners = [];
|
|
51
|
-
this
|
|
52
|
-
this
|
|
53
|
-
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
|
|
59
|
-
this.options = this
|
|
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
|
|
64
|
+
this.#client.getQueryCache().notify({
|
|
67
65
|
type: 'observerOptionsUpdated',
|
|
68
|
-
query: this
|
|
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
|
-
}
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
86
|
+
// Update result
|
|
87
|
+
this.#updateResult(notifyOptions);
|
|
91
88
|
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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({
|
|
140
|
-
|
|
141
|
-
refetchPage
|
|
142
|
-
}
|
|
127
|
+
return this.fetch({
|
|
128
|
+
...options
|
|
143
129
|
});
|
|
144
130
|
}
|
|
145
|
-
|
|
146
131
|
fetchOptimistic(options) {
|
|
147
|
-
const defaultedOptions = this
|
|
148
|
-
const query = this
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
|
160
|
-
return this
|
|
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
|
|
148
|
+
this.#updateQuery();
|
|
167
149
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (!
|
|
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
|
-
|
|
178
|
-
this.
|
|
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
|
-
|
|
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
|
|
189
|
-
if (!this
|
|
190
|
-
this
|
|
167
|
+
this.#staleTimeoutId = setTimeout(() => {
|
|
168
|
+
if (!this.#currentResult.isStale) {
|
|
169
|
+
this.#updateResult();
|
|
191
170
|
}
|
|
192
171
|
}, timeout);
|
|
193
172
|
}
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
202
|
-
this
|
|
203
|
-
this.currentRefetchInterval
|
|
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
|
|
184
|
+
this.#executeFetch();
|
|
212
185
|
}
|
|
213
|
-
}, this
|
|
186
|
+
}, this.#currentRefetchInterval);
|
|
214
187
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
this
|
|
218
|
-
this.updateRefetchInterval(this.computeRefetchInterval());
|
|
188
|
+
#updateTimers() {
|
|
189
|
+
this.#updateStaleTimeout();
|
|
190
|
+
this.#updateRefetchInterval(this.#computeRefetchInterval());
|
|
219
191
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
this.staleTimeoutId = undefined;
|
|
192
|
+
#clearStaleTimeout() {
|
|
193
|
+
if (this.#staleTimeoutId) {
|
|
194
|
+
clearTimeout(this.#staleTimeoutId);
|
|
195
|
+
this.#staleTimeoutId = undefined;
|
|
225
196
|
}
|
|
226
197
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
|
205
|
+
const prevQuery = this.#currentQuery;
|
|
237
206
|
const prevOptions = this.options;
|
|
238
|
-
const prevResult = this
|
|
239
|
-
const prevResultState = this
|
|
240
|
-
const prevResultOptions = this
|
|
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
|
|
243
|
-
const prevQueryResult = queryChange ? this
|
|
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;
|
|
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
|
-
|
|
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
|
-
}
|
|
275
|
-
|
|
239
|
+
}
|
|
276
240
|
|
|
277
|
-
|
|
278
|
-
|
|
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 ===
|
|
286
|
-
data = this
|
|
244
|
+
if (prevResult && state.data === prevResultState?.data && options.select === this.#selectFn) {
|
|
245
|
+
data = this.#selectResult;
|
|
287
246
|
} else {
|
|
288
247
|
try {
|
|
289
|
-
this
|
|
248
|
+
this.#selectFn = options.select;
|
|
290
249
|
data = options.select(state.data);
|
|
291
|
-
data = replaceData(prevResult
|
|
292
|
-
this
|
|
293
|
-
this
|
|
250
|
+
data = replaceData(prevResult?.data, data, options);
|
|
251
|
+
this.#selectResult = data;
|
|
252
|
+
this.#selectError = null;
|
|
294
253
|
} catch (selectError) {
|
|
295
|
-
|
|
296
|
-
this.client.getLogger().error(selectError);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
this.selectError = selectError;
|
|
254
|
+
this.#selectError = selectError;
|
|
300
255
|
}
|
|
301
256
|
}
|
|
302
|
-
}
|
|
257
|
+
}
|
|
258
|
+
// Use query data
|
|
303
259
|
else {
|
|
304
260
|
data = state.data;
|
|
305
|
-
}
|
|
306
|
-
|
|
261
|
+
}
|
|
307
262
|
|
|
308
|
-
|
|
309
|
-
|
|
263
|
+
// Show placeholder data if needed
|
|
264
|
+
if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'pending') {
|
|
265
|
+
let placeholderData;
|
|
310
266
|
|
|
311
|
-
|
|
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
|
|
275
|
+
this.#selectError = null;
|
|
320
276
|
} catch (selectError) {
|
|
321
|
-
|
|
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
|
|
283
|
+
data = replaceData(prevResult?.data, placeholderData, options);
|
|
333
284
|
isPlaceholderData = true;
|
|
334
285
|
}
|
|
335
286
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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
|
|
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
|
-
|
|
300
|
+
isPending,
|
|
351
301
|
isSuccess: status === 'success',
|
|
352
302
|
isError,
|
|
353
|
-
isInitialLoading: isLoading
|
|
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 && !
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
362
|
+
this.#notify({
|
|
363
|
+
...defaultNotifyOptions,
|
|
424
364
|
...notifyOptions
|
|
425
365
|
});
|
|
426
366
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
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
|
-
|
|
436
|
-
this
|
|
437
|
-
this
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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
|
|
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
|
}
|