@tanstack/query-core 5.0.0-alpha.49 → 5.0.0-alpha.50
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.js +15 -31
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/hydration.js +4 -7
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +4 -5
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +2 -3
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/mutation.js +102 -127
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutationCache.js +17 -30
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationObserver.js +51 -81
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/onlineManager.js +14 -29
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/queriesObserver.js +79 -125
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/query.js +161 -210
- package/build/lib/query.js.map +1 -1
- package/build/lib/queryCache.js +10 -15
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryClient.js +61 -98
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryObserver.js +197 -297
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/removable.js +6 -13
- package/build/lib/removable.js.map +1 -1
- package/build/lib/retryer.js +14 -15
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/utils.js +1 -1
- package/build/lib/utils.js.map +1 -1
- package/package.json +1 -2
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -16
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/umd/index.development.js +0 -2691
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
|
@@ -1,130 +1,33 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
4
3
|
var utils = require('./utils.js');
|
|
5
4
|
var notifyManager = require('./notifyManager.js');
|
|
6
5
|
var focusManager = require('./focusManager.js');
|
|
7
6
|
var subscribable = require('./subscribable.js');
|
|
8
7
|
var retryer = require('./retryer.js');
|
|
9
8
|
|
|
10
|
-
var _client = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("client");
|
|
11
|
-
var _currentQuery = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentQuery");
|
|
12
|
-
var _currentQueryInitialState = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentQueryInitialState");
|
|
13
|
-
var _currentResult = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentResult");
|
|
14
|
-
var _currentResultState = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentResultState");
|
|
15
|
-
var _currentResultOptions = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentResultOptions");
|
|
16
|
-
var _selectError = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("selectError");
|
|
17
|
-
var _selectFn = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("selectFn");
|
|
18
|
-
var _selectResult = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("selectResult");
|
|
19
|
-
var _lastQueryWithDefinedData = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("lastQueryWithDefinedData");
|
|
20
|
-
var _staleTimeoutId = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("staleTimeoutId");
|
|
21
|
-
var _refetchIntervalId = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("refetchIntervalId");
|
|
22
|
-
var _currentRefetchInterval = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("currentRefetchInterval");
|
|
23
|
-
var _trackedProps = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("trackedProps");
|
|
24
|
-
var _executeFetch = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("executeFetch");
|
|
25
|
-
var _updateStaleTimeout = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("updateStaleTimeout");
|
|
26
|
-
var _computeRefetchInterval = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("computeRefetchInterval");
|
|
27
|
-
var _updateRefetchInterval = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("updateRefetchInterval");
|
|
28
|
-
var _updateTimers = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("updateTimers");
|
|
29
|
-
var _clearStaleTimeout = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("clearStaleTimeout");
|
|
30
|
-
var _clearRefetchInterval = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("clearRefetchInterval");
|
|
31
|
-
var _updateResult = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("updateResult");
|
|
32
|
-
var _updateQuery = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("updateQuery");
|
|
33
|
-
var _notify = /*#__PURE__*/_rollupPluginBabelHelpers.classPrivateFieldLooseKey("notify");
|
|
34
9
|
class QueryObserver extends subscribable.Subscribable {
|
|
10
|
+
#client;
|
|
11
|
+
#currentQuery = undefined;
|
|
12
|
+
#currentQueryInitialState = undefined;
|
|
13
|
+
#currentResult = undefined;
|
|
14
|
+
#currentResultState;
|
|
15
|
+
#currentResultOptions;
|
|
16
|
+
#selectError;
|
|
17
|
+
#selectFn;
|
|
18
|
+
#selectResult;
|
|
19
|
+
// This property keeps track of the last query with defined data.
|
|
20
|
+
// It will be used to pass the previous data and query to the placeholder function between renders.
|
|
21
|
+
#lastQueryWithDefinedData;
|
|
22
|
+
#staleTimeoutId;
|
|
23
|
+
#refetchIntervalId;
|
|
24
|
+
#currentRefetchInterval;
|
|
25
|
+
#trackedProps = new Set();
|
|
35
26
|
constructor(client, options) {
|
|
36
27
|
super();
|
|
37
|
-
|
|
38
|
-
value: _notify2
|
|
39
|
-
});
|
|
40
|
-
Object.defineProperty(this, _updateQuery, {
|
|
41
|
-
value: _updateQuery2
|
|
42
|
-
});
|
|
43
|
-
Object.defineProperty(this, _updateResult, {
|
|
44
|
-
value: _updateResult2
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(this, _clearRefetchInterval, {
|
|
47
|
-
value: _clearRefetchInterval2
|
|
48
|
-
});
|
|
49
|
-
Object.defineProperty(this, _clearStaleTimeout, {
|
|
50
|
-
value: _clearStaleTimeout2
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(this, _updateTimers, {
|
|
53
|
-
value: _updateTimers2
|
|
54
|
-
});
|
|
55
|
-
Object.defineProperty(this, _updateRefetchInterval, {
|
|
56
|
-
value: _updateRefetchInterval2
|
|
57
|
-
});
|
|
58
|
-
Object.defineProperty(this, _computeRefetchInterval, {
|
|
59
|
-
value: _computeRefetchInterval2
|
|
60
|
-
});
|
|
61
|
-
Object.defineProperty(this, _updateStaleTimeout, {
|
|
62
|
-
value: _updateStaleTimeout2
|
|
63
|
-
});
|
|
64
|
-
Object.defineProperty(this, _executeFetch, {
|
|
65
|
-
value: _executeFetch2
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(this, _client, {
|
|
68
|
-
writable: true,
|
|
69
|
-
value: void 0
|
|
70
|
-
});
|
|
71
|
-
Object.defineProperty(this, _currentQuery, {
|
|
72
|
-
writable: true,
|
|
73
|
-
value: undefined
|
|
74
|
-
});
|
|
75
|
-
Object.defineProperty(this, _currentQueryInitialState, {
|
|
76
|
-
writable: true,
|
|
77
|
-
value: undefined
|
|
78
|
-
});
|
|
79
|
-
Object.defineProperty(this, _currentResult, {
|
|
80
|
-
writable: true,
|
|
81
|
-
value: undefined
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(this, _currentResultState, {
|
|
84
|
-
writable: true,
|
|
85
|
-
value: void 0
|
|
86
|
-
});
|
|
87
|
-
Object.defineProperty(this, _currentResultOptions, {
|
|
88
|
-
writable: true,
|
|
89
|
-
value: void 0
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(this, _selectError, {
|
|
92
|
-
writable: true,
|
|
93
|
-
value: void 0
|
|
94
|
-
});
|
|
95
|
-
Object.defineProperty(this, _selectFn, {
|
|
96
|
-
writable: true,
|
|
97
|
-
value: void 0
|
|
98
|
-
});
|
|
99
|
-
Object.defineProperty(this, _selectResult, {
|
|
100
|
-
writable: true,
|
|
101
|
-
value: void 0
|
|
102
|
-
});
|
|
103
|
-
// This property keeps track of the last query with defined data.
|
|
104
|
-
// It will be used to pass the previous data and query to the placeholder function between renders.
|
|
105
|
-
Object.defineProperty(this, _lastQueryWithDefinedData, {
|
|
106
|
-
writable: true,
|
|
107
|
-
value: void 0
|
|
108
|
-
});
|
|
109
|
-
Object.defineProperty(this, _staleTimeoutId, {
|
|
110
|
-
writable: true,
|
|
111
|
-
value: void 0
|
|
112
|
-
});
|
|
113
|
-
Object.defineProperty(this, _refetchIntervalId, {
|
|
114
|
-
writable: true,
|
|
115
|
-
value: void 0
|
|
116
|
-
});
|
|
117
|
-
Object.defineProperty(this, _currentRefetchInterval, {
|
|
118
|
-
writable: true,
|
|
119
|
-
value: void 0
|
|
120
|
-
});
|
|
121
|
-
Object.defineProperty(this, _trackedProps, {
|
|
122
|
-
writable: true,
|
|
123
|
-
value: new Set()
|
|
124
|
-
});
|
|
125
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _client)[_client] = client;
|
|
28
|
+
this.#client = client;
|
|
126
29
|
this.options = options;
|
|
127
|
-
|
|
30
|
+
this.#selectError = null;
|
|
128
31
|
this.bindMethods();
|
|
129
32
|
this.setOptions(options);
|
|
130
33
|
}
|
|
@@ -133,11 +36,11 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
133
36
|
}
|
|
134
37
|
onSubscribe() {
|
|
135
38
|
if (this.listeners.size === 1) {
|
|
136
|
-
|
|
137
|
-
if (shouldFetchOnMount(
|
|
138
|
-
|
|
39
|
+
this.#currentQuery.addObserver(this);
|
|
40
|
+
if (shouldFetchOnMount(this.#currentQuery, this.options)) {
|
|
41
|
+
this.#executeFetch();
|
|
139
42
|
}
|
|
140
|
-
|
|
43
|
+
this.#updateTimers();
|
|
141
44
|
}
|
|
142
45
|
}
|
|
143
46
|
onUnsubscribe() {
|
|
@@ -146,25 +49,25 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
146
49
|
}
|
|
147
50
|
}
|
|
148
51
|
shouldFetchOnReconnect() {
|
|
149
|
-
return shouldFetchOn(
|
|
52
|
+
return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnReconnect);
|
|
150
53
|
}
|
|
151
54
|
shouldFetchOnWindowFocus() {
|
|
152
|
-
return shouldFetchOn(
|
|
55
|
+
return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
153
56
|
}
|
|
154
57
|
destroy() {
|
|
155
58
|
this.listeners = new Set();
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
59
|
+
this.#clearStaleTimeout();
|
|
60
|
+
this.#clearRefetchInterval();
|
|
61
|
+
this.#currentQuery.removeObserver(this);
|
|
159
62
|
}
|
|
160
63
|
setOptions(options, notifyOptions) {
|
|
161
64
|
const prevOptions = this.options;
|
|
162
|
-
const prevQuery =
|
|
163
|
-
this.options =
|
|
65
|
+
const prevQuery = this.#currentQuery;
|
|
66
|
+
this.options = this.#client.defaultQueryOptions(options);
|
|
164
67
|
if (!utils.shallowEqualObjects(prevOptions, this.options)) {
|
|
165
|
-
|
|
68
|
+
this.#client.getQueryCache().notify({
|
|
166
69
|
type: 'observerOptionsUpdated',
|
|
167
|
-
query:
|
|
70
|
+
query: this.#currentQuery,
|
|
168
71
|
observer: this
|
|
169
72
|
});
|
|
170
73
|
}
|
|
@@ -176,34 +79,34 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
176
79
|
if (!this.options.queryKey) {
|
|
177
80
|
this.options.queryKey = prevOptions.queryKey;
|
|
178
81
|
}
|
|
179
|
-
|
|
82
|
+
this.#updateQuery();
|
|
180
83
|
const mounted = this.hasListeners();
|
|
181
84
|
|
|
182
85
|
// Fetch if there are subscribers
|
|
183
|
-
if (mounted && shouldFetchOptionally(
|
|
184
|
-
|
|
86
|
+
if (mounted && shouldFetchOptionally(this.#currentQuery, prevQuery, this.options, prevOptions)) {
|
|
87
|
+
this.#executeFetch();
|
|
185
88
|
}
|
|
186
89
|
|
|
187
90
|
// Update result
|
|
188
|
-
|
|
91
|
+
this.#updateResult(notifyOptions);
|
|
189
92
|
|
|
190
93
|
// Update stale interval if needed
|
|
191
|
-
if (mounted && (
|
|
192
|
-
|
|
94
|
+
if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
|
|
95
|
+
this.#updateStaleTimeout();
|
|
193
96
|
}
|
|
194
|
-
const nextRefetchInterval =
|
|
97
|
+
const nextRefetchInterval = this.#computeRefetchInterval();
|
|
195
98
|
|
|
196
99
|
// Update refetch interval if needed
|
|
197
|
-
if (mounted && (
|
|
198
|
-
|
|
100
|
+
if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.#currentRefetchInterval)) {
|
|
101
|
+
this.#updateRefetchInterval(nextRefetchInterval);
|
|
199
102
|
}
|
|
200
103
|
}
|
|
201
104
|
getOptimisticResult(options) {
|
|
202
|
-
const query =
|
|
105
|
+
const query = this.#client.getQueryCache().build(this.#client, options);
|
|
203
106
|
return this.createResult(query, options);
|
|
204
107
|
}
|
|
205
108
|
getCurrentResult() {
|
|
206
|
-
return
|
|
109
|
+
return this.#currentResult;
|
|
207
110
|
}
|
|
208
111
|
trackResult(result) {
|
|
209
112
|
const trackedResult = {};
|
|
@@ -212,7 +115,7 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
212
115
|
configurable: false,
|
|
213
116
|
enumerable: true,
|
|
214
117
|
get: () => {
|
|
215
|
-
|
|
118
|
+
this.#trackedProps.add(key);
|
|
216
119
|
return result[key];
|
|
217
120
|
}
|
|
218
121
|
});
|
|
@@ -220,7 +123,7 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
220
123
|
return trackedResult;
|
|
221
124
|
}
|
|
222
125
|
getCurrentQuery() {
|
|
223
|
-
return
|
|
126
|
+
return this.#currentQuery;
|
|
224
127
|
}
|
|
225
128
|
refetch({
|
|
226
129
|
...options
|
|
@@ -230,29 +133,86 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
230
133
|
});
|
|
231
134
|
}
|
|
232
135
|
fetchOptimistic(options) {
|
|
233
|
-
const defaultedOptions =
|
|
234
|
-
const query =
|
|
136
|
+
const defaultedOptions = this.#client.defaultQueryOptions(options);
|
|
137
|
+
const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
|
|
235
138
|
query.isFetchingOptimistic = true;
|
|
236
139
|
return query.fetch().then(() => this.createResult(query, defaultedOptions));
|
|
237
140
|
}
|
|
238
141
|
fetch(fetchOptions) {
|
|
239
|
-
|
|
240
|
-
return _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _executeFetch)[_executeFetch]({
|
|
142
|
+
return this.#executeFetch({
|
|
241
143
|
...fetchOptions,
|
|
242
|
-
cancelRefetch:
|
|
144
|
+
cancelRefetch: fetchOptions.cancelRefetch ?? true
|
|
243
145
|
}).then(() => {
|
|
244
|
-
|
|
245
|
-
return
|
|
146
|
+
this.#updateResult();
|
|
147
|
+
return this.#currentResult;
|
|
246
148
|
});
|
|
247
149
|
}
|
|
150
|
+
#executeFetch(fetchOptions) {
|
|
151
|
+
// Make sure we reference the latest query as the current one might have been removed
|
|
152
|
+
this.#updateQuery();
|
|
153
|
+
|
|
154
|
+
// Fetch
|
|
155
|
+
let promise = this.#currentQuery.fetch(this.options, fetchOptions);
|
|
156
|
+
if (!fetchOptions?.throwOnError) {
|
|
157
|
+
promise = promise.catch(utils.noop);
|
|
158
|
+
}
|
|
159
|
+
return promise;
|
|
160
|
+
}
|
|
161
|
+
#updateStaleTimeout() {
|
|
162
|
+
this.#clearStaleTimeout();
|
|
163
|
+
if (utils.isServer || this.#currentResult.isStale || !utils.isValidTimeout(this.options.staleTime)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const time = utils.timeUntilStale(this.#currentResult.dataUpdatedAt, this.options.staleTime);
|
|
167
|
+
|
|
168
|
+
// The timeout is sometimes triggered 1 ms before the stale time expiration.
|
|
169
|
+
// To mitigate this issue we always add 1 ms to the timeout.
|
|
170
|
+
const timeout = time + 1;
|
|
171
|
+
this.#staleTimeoutId = setTimeout(() => {
|
|
172
|
+
if (!this.#currentResult.isStale) {
|
|
173
|
+
this.#updateResult();
|
|
174
|
+
}
|
|
175
|
+
}, timeout);
|
|
176
|
+
}
|
|
177
|
+
#computeRefetchInterval() {
|
|
178
|
+
return (typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval) ?? false;
|
|
179
|
+
}
|
|
180
|
+
#updateRefetchInterval(nextInterval) {
|
|
181
|
+
this.#clearRefetchInterval();
|
|
182
|
+
this.#currentRefetchInterval = nextInterval;
|
|
183
|
+
if (utils.isServer || this.options.enabled === false || !utils.isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.#refetchIntervalId = setInterval(() => {
|
|
187
|
+
if (this.options.refetchIntervalInBackground || focusManager.focusManager.isFocused()) {
|
|
188
|
+
this.#executeFetch();
|
|
189
|
+
}
|
|
190
|
+
}, this.#currentRefetchInterval);
|
|
191
|
+
}
|
|
192
|
+
#updateTimers() {
|
|
193
|
+
this.#updateStaleTimeout();
|
|
194
|
+
this.#updateRefetchInterval(this.#computeRefetchInterval());
|
|
195
|
+
}
|
|
196
|
+
#clearStaleTimeout() {
|
|
197
|
+
if (this.#staleTimeoutId) {
|
|
198
|
+
clearTimeout(this.#staleTimeoutId);
|
|
199
|
+
this.#staleTimeoutId = undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
#clearRefetchInterval() {
|
|
203
|
+
if (this.#refetchIntervalId) {
|
|
204
|
+
clearInterval(this.#refetchIntervalId);
|
|
205
|
+
this.#refetchIntervalId = undefined;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
248
208
|
createResult(query, options) {
|
|
249
|
-
const prevQuery =
|
|
209
|
+
const prevQuery = this.#currentQuery;
|
|
250
210
|
const prevOptions = this.options;
|
|
251
|
-
const prevResult =
|
|
252
|
-
const prevResultState =
|
|
253
|
-
const prevResultOptions =
|
|
211
|
+
const prevResult = this.#currentResult;
|
|
212
|
+
const prevResultState = this.#currentResultState;
|
|
213
|
+
const prevResultOptions = this.#currentResultOptions;
|
|
254
214
|
const queryChange = query !== prevQuery;
|
|
255
|
-
const queryInitialState = queryChange ? query.state :
|
|
215
|
+
const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
|
|
256
216
|
const {
|
|
257
217
|
state
|
|
258
218
|
} = query;
|
|
@@ -284,17 +244,17 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
284
244
|
// Select data if needed
|
|
285
245
|
if (options.select && typeof state.data !== 'undefined') {
|
|
286
246
|
// Memoize select result
|
|
287
|
-
if (prevResult && state.data ===
|
|
288
|
-
data =
|
|
247
|
+
if (prevResult && state.data === prevResultState?.data && options.select === this.#selectFn) {
|
|
248
|
+
data = this.#selectResult;
|
|
289
249
|
} else {
|
|
290
250
|
try {
|
|
291
|
-
|
|
251
|
+
this.#selectFn = options.select;
|
|
292
252
|
data = options.select(state.data);
|
|
293
|
-
data = utils.replaceData(prevResult
|
|
294
|
-
|
|
295
|
-
|
|
253
|
+
data = utils.replaceData(prevResult?.data, data, options);
|
|
254
|
+
this.#selectResult = data;
|
|
255
|
+
this.#selectError = null;
|
|
296
256
|
} catch (selectError) {
|
|
297
|
-
|
|
257
|
+
this.#selectError = selectError;
|
|
298
258
|
}
|
|
299
259
|
}
|
|
300
260
|
}
|
|
@@ -308,29 +268,28 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
308
268
|
let placeholderData;
|
|
309
269
|
|
|
310
270
|
// Memoize placeholder data
|
|
311
|
-
if (prevResult
|
|
271
|
+
if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
|
|
312
272
|
placeholderData = prevResult.data;
|
|
313
273
|
} else {
|
|
314
|
-
|
|
315
|
-
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData((_classPrivateFieldLoo = _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _lastQueryWithDefinedData)[_lastQueryWithDefinedData]) == null ? void 0 : _classPrivateFieldLoo.state.data, _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _lastQueryWithDefinedData)[_lastQueryWithDefinedData]) : options.placeholderData;
|
|
274
|
+
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
|
|
316
275
|
if (options.select && typeof placeholderData !== 'undefined') {
|
|
317
276
|
try {
|
|
318
277
|
placeholderData = options.select(placeholderData);
|
|
319
|
-
|
|
278
|
+
this.#selectError = null;
|
|
320
279
|
} catch (selectError) {
|
|
321
|
-
|
|
280
|
+
this.#selectError = selectError;
|
|
322
281
|
}
|
|
323
282
|
}
|
|
324
283
|
}
|
|
325
284
|
if (typeof placeholderData !== 'undefined') {
|
|
326
285
|
status = 'success';
|
|
327
|
-
data = utils.replaceData(prevResult
|
|
286
|
+
data = utils.replaceData(prevResult?.data, placeholderData, options);
|
|
328
287
|
isPlaceholderData = true;
|
|
329
288
|
}
|
|
330
289
|
}
|
|
331
|
-
if (
|
|
332
|
-
error =
|
|
333
|
-
data =
|
|
290
|
+
if (this.#selectError) {
|
|
291
|
+
error = this.#selectError;
|
|
292
|
+
data = this.#selectResult;
|
|
334
293
|
errorUpdatedAt = Date.now();
|
|
335
294
|
status = 'error';
|
|
336
295
|
}
|
|
@@ -366,145 +325,86 @@ class QueryObserver extends subscribable.Subscribable {
|
|
|
366
325
|
};
|
|
367
326
|
return result;
|
|
368
327
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
function _executeFetch2(fetchOptions) {
|
|
377
|
-
// Make sure we reference the latest query as the current one might have been removed
|
|
378
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _updateQuery)[_updateQuery]();
|
|
328
|
+
#updateResult(notifyOptions) {
|
|
329
|
+
const prevResult = this.#currentResult;
|
|
330
|
+
const nextResult = this.createResult(this.#currentQuery, this.options);
|
|
331
|
+
this.#currentResultState = this.#currentQuery.state;
|
|
332
|
+
this.#currentResultOptions = this.options;
|
|
379
333
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
promise = promise.catch(utils.noop);
|
|
384
|
-
}
|
|
385
|
-
return promise;
|
|
386
|
-
}
|
|
387
|
-
function _updateStaleTimeout2() {
|
|
388
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _clearStaleTimeout)[_clearStaleTimeout]();
|
|
389
|
-
if (utils.isServer || _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult].isStale || !utils.isValidTimeout(this.options.staleTime)) {
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
const time = utils.timeUntilStale(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult].dataUpdatedAt, this.options.staleTime);
|
|
393
|
-
|
|
394
|
-
// The timeout is sometimes triggered 1 ms before the stale time expiration.
|
|
395
|
-
// To mitigate this issue we always add 1 ms to the timeout.
|
|
396
|
-
const timeout = time + 1;
|
|
397
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _staleTimeoutId)[_staleTimeoutId] = setTimeout(() => {
|
|
398
|
-
if (!_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult].isStale) {
|
|
399
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _updateResult)[_updateResult]();
|
|
334
|
+
// Only notify and update result if something has changed
|
|
335
|
+
if (utils.shallowEqualObjects(nextResult, prevResult)) {
|
|
336
|
+
return;
|
|
400
337
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
function _computeRefetchInterval2() {
|
|
404
|
-
var _ref;
|
|
405
|
-
return (_ref = typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult].data, _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentQuery)[_currentQuery]) : this.options.refetchInterval) != null ? _ref : false;
|
|
406
|
-
}
|
|
407
|
-
function _updateRefetchInterval2(nextInterval) {
|
|
408
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _clearRefetchInterval)[_clearRefetchInterval]();
|
|
409
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentRefetchInterval)[_currentRefetchInterval] = nextInterval;
|
|
410
|
-
if (utils.isServer || this.options.enabled === false || !utils.isValidTimeout(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentRefetchInterval)[_currentRefetchInterval]) || _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentRefetchInterval)[_currentRefetchInterval] === 0) {
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _refetchIntervalId)[_refetchIntervalId] = setInterval(() => {
|
|
414
|
-
if (this.options.refetchIntervalInBackground || focusManager.focusManager.isFocused()) {
|
|
415
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _executeFetch)[_executeFetch]();
|
|
338
|
+
if (this.#currentResultState.data !== undefined) {
|
|
339
|
+
this.#lastQueryWithDefinedData = this.#currentQuery;
|
|
416
340
|
}
|
|
417
|
-
|
|
418
|
-
}
|
|
419
|
-
function _updateTimers2() {
|
|
420
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _updateStaleTimeout)[_updateStaleTimeout]();
|
|
421
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _updateRefetchInterval)[_updateRefetchInterval](_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _computeRefetchInterval)[_computeRefetchInterval]());
|
|
422
|
-
}
|
|
423
|
-
function _clearStaleTimeout2() {
|
|
424
|
-
if (_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _staleTimeoutId)[_staleTimeoutId]) {
|
|
425
|
-
clearTimeout(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _staleTimeoutId)[_staleTimeoutId]);
|
|
426
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _staleTimeoutId)[_staleTimeoutId] = undefined;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
function _clearRefetchInterval2() {
|
|
430
|
-
if (_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _refetchIntervalId)[_refetchIntervalId]) {
|
|
431
|
-
clearInterval(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _refetchIntervalId)[_refetchIntervalId]);
|
|
432
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _refetchIntervalId)[_refetchIntervalId] = undefined;
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
function _updateResult2(notifyOptions) {
|
|
436
|
-
const prevResult = _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult];
|
|
437
|
-
const nextResult = this.createResult(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentQuery)[_currentQuery], this.options);
|
|
438
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResultState)[_currentResultState] = _rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentQuery)[_currentQuery].state;
|
|
439
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResultOptions)[_currentResultOptions] = this.options;
|
|
341
|
+
this.#currentResult = nextResult;
|
|
440
342
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
if (
|
|
464
|
-
|
|
343
|
+
// Determine which callbacks to trigger
|
|
344
|
+
const defaultNotifyOptions = {};
|
|
345
|
+
const shouldNotifyListeners = () => {
|
|
346
|
+
if (!prevResult) {
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
const {
|
|
350
|
+
notifyOnChangeProps
|
|
351
|
+
} = this.options;
|
|
352
|
+
if (notifyOnChangeProps === 'all' || !notifyOnChangeProps && !this.#trackedProps.size) {
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
|
|
356
|
+
if (this.options.throwOnError) {
|
|
357
|
+
includedProps.add('error');
|
|
358
|
+
}
|
|
359
|
+
return Object.keys(this.#currentResult).some(key => {
|
|
360
|
+
const typedKey = key;
|
|
361
|
+
const changed = this.#currentResult[typedKey] !== prevResult[typedKey];
|
|
362
|
+
return changed && includedProps.has(typedKey);
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {
|
|
366
|
+
defaultNotifyOptions.listeners = true;
|
|
465
367
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
return changed && includedProps.has(typedKey);
|
|
368
|
+
this.#notify({
|
|
369
|
+
...defaultNotifyOptions,
|
|
370
|
+
...notifyOptions
|
|
470
371
|
});
|
|
471
|
-
};
|
|
472
|
-
if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
|
|
473
|
-
defaultNotifyOptions.listeners = true;
|
|
474
372
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentQueryInitialState)[_currentQueryInitialState] = query.state;
|
|
488
|
-
if (this.hasListeners()) {
|
|
489
|
-
prevQuery == null ? void 0 : prevQuery.removeObserver(this);
|
|
490
|
-
query.addObserver(this);
|
|
373
|
+
#updateQuery() {
|
|
374
|
+
const query = this.#client.getQueryCache().build(this.#client, this.options);
|
|
375
|
+
if (query === this.#currentQuery) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const prevQuery = this.#currentQuery;
|
|
379
|
+
this.#currentQuery = query;
|
|
380
|
+
this.#currentQueryInitialState = query.state;
|
|
381
|
+
if (this.hasListeners()) {
|
|
382
|
+
prevQuery?.removeObserver(this);
|
|
383
|
+
query.addObserver(this);
|
|
384
|
+
}
|
|
491
385
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
if (notifyOptions.listeners) {
|
|
497
|
-
this.listeners.forEach(listener => {
|
|
498
|
-
listener(_rollupPluginBabelHelpers.classPrivateFieldLooseBase(this, _currentResult)[_currentResult]);
|
|
499
|
-
});
|
|
386
|
+
onQueryUpdate() {
|
|
387
|
+
this.#updateResult();
|
|
388
|
+
if (this.hasListeners()) {
|
|
389
|
+
this.#updateTimers();
|
|
500
390
|
}
|
|
391
|
+
}
|
|
392
|
+
#notify(notifyOptions) {
|
|
393
|
+
notifyManager.notifyManager.batch(() => {
|
|
394
|
+
// First, trigger the listeners
|
|
395
|
+
if (notifyOptions.listeners) {
|
|
396
|
+
this.listeners.forEach(listener => {
|
|
397
|
+
listener(this.#currentResult);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
501
400
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
401
|
+
// Then the cache listeners
|
|
402
|
+
this.#client.getQueryCache().notify({
|
|
403
|
+
query: this.#currentQuery,
|
|
404
|
+
type: 'observerResultsUpdated'
|
|
405
|
+
});
|
|
506
406
|
});
|
|
507
|
-
}
|
|
407
|
+
}
|
|
508
408
|
}
|
|
509
409
|
function shouldLoadOnMount(query, options) {
|
|
510
410
|
return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
|