@tanstack/query-core 4.24.10 → 5.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +43 -72
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +43 -74
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +43 -72
- 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 +868 -1398
- 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 +60 -97
- package/src/onlineManager.ts +14 -14
- package/src/queriesObserver.ts +50 -54
- package/src/query.ts +106 -110
- 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/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
package/build/lib/query.js
CHANGED
|
@@ -1,262 +1,227 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var utils = require('./utils.js');
|
|
6
|
-
var logger = require('./logger');
|
|
7
4
|
var notifyManager = require('./notifyManager.js');
|
|
8
5
|
var retryer = require('./retryer.js');
|
|
9
6
|
var removable = require('./removable.js');
|
|
10
7
|
|
|
8
|
+
// TYPES
|
|
9
|
+
|
|
11
10
|
// CLASS
|
|
11
|
+
|
|
12
12
|
class Query extends removable.Removable {
|
|
13
|
+
#initialState;
|
|
14
|
+
#revertState;
|
|
15
|
+
#cache;
|
|
16
|
+
#promise;
|
|
17
|
+
#retryer;
|
|
18
|
+
#observers;
|
|
19
|
+
#defaultOptions;
|
|
20
|
+
#abortSignalConsumed;
|
|
13
21
|
constructor(config) {
|
|
14
22
|
super();
|
|
15
|
-
this
|
|
16
|
-
this
|
|
17
|
-
this
|
|
18
|
-
this
|
|
19
|
-
this
|
|
20
|
-
this.logger = config.logger || logger.defaultLogger;
|
|
23
|
+
this.#abortSignalConsumed = false;
|
|
24
|
+
this.#defaultOptions = config.defaultOptions;
|
|
25
|
+
this.#setOptions(config.options);
|
|
26
|
+
this.#observers = [];
|
|
27
|
+
this.#cache = config.cache;
|
|
21
28
|
this.queryKey = config.queryKey;
|
|
22
29
|
this.queryHash = config.queryHash;
|
|
23
|
-
this
|
|
24
|
-
this.state = this
|
|
30
|
+
this.#initialState = config.state || getDefaultState(this.options);
|
|
31
|
+
this.state = this.#initialState;
|
|
25
32
|
this.scheduleGc();
|
|
26
33
|
}
|
|
27
|
-
|
|
28
34
|
get meta() {
|
|
29
35
|
return this.options.meta;
|
|
30
36
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
#setOptions(options) {
|
|
38
|
+
this.options = {
|
|
39
|
+
...this.#defaultOptions,
|
|
34
40
|
...options
|
|
35
41
|
};
|
|
36
|
-
this.
|
|
42
|
+
this.updateGcTime(this.options.gcTime);
|
|
37
43
|
}
|
|
38
|
-
|
|
39
44
|
optionalRemove() {
|
|
40
|
-
if (!this
|
|
41
|
-
this
|
|
45
|
+
if (!this.#observers.length && this.state.fetchStatus === 'idle') {
|
|
46
|
+
this.#cache.remove(this);
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
|
-
|
|
45
49
|
setData(newData, options) {
|
|
46
|
-
const data = utils.replaceData(this.state.data, newData, this.options);
|
|
50
|
+
const data = utils.replaceData(this.state.data, newData, this.options);
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
// Set data and mark it as cached
|
|
53
|
+
this.#dispatch({
|
|
49
54
|
data,
|
|
50
55
|
type: 'success',
|
|
51
|
-
dataUpdatedAt: options
|
|
52
|
-
manual: options
|
|
56
|
+
dataUpdatedAt: options?.updatedAt,
|
|
57
|
+
manual: options?.manual
|
|
53
58
|
});
|
|
54
59
|
return data;
|
|
55
60
|
}
|
|
56
|
-
|
|
57
61
|
setState(state, setStateOptions) {
|
|
58
|
-
this
|
|
62
|
+
this.#dispatch({
|
|
59
63
|
type: 'setState',
|
|
60
64
|
state,
|
|
61
65
|
setStateOptions
|
|
62
66
|
});
|
|
63
67
|
}
|
|
64
|
-
|
|
65
68
|
cancel(options) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const promise = this.promise;
|
|
69
|
-
(_this$retryer = this.retryer) == null ? void 0 : _this$retryer.cancel(options);
|
|
69
|
+
const promise = this.#promise;
|
|
70
|
+
this.#retryer?.cancel(options);
|
|
70
71
|
return promise ? promise.then(utils.noop).catch(utils.noop) : Promise.resolve();
|
|
71
72
|
}
|
|
72
|
-
|
|
73
73
|
destroy() {
|
|
74
74
|
super.destroy();
|
|
75
75
|
this.cancel({
|
|
76
76
|
silent: true
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
|
-
|
|
80
79
|
reset() {
|
|
81
80
|
this.destroy();
|
|
82
|
-
this.setState(this
|
|
81
|
+
this.setState(this.#initialState);
|
|
83
82
|
}
|
|
84
|
-
|
|
85
83
|
isActive() {
|
|
86
|
-
return this
|
|
84
|
+
return this.#observers.some(observer => observer.options.enabled !== false);
|
|
87
85
|
}
|
|
88
|
-
|
|
89
86
|
isDisabled() {
|
|
90
87
|
return this.getObserversCount() > 0 && !this.isActive();
|
|
91
88
|
}
|
|
92
|
-
|
|
93
89
|
isStale() {
|
|
94
|
-
return this.state.isInvalidated || !this.state.dataUpdatedAt || this
|
|
90
|
+
return this.state.isInvalidated || !this.state.dataUpdatedAt || this.#observers.some(observer => observer.getCurrentResult().isStale);
|
|
95
91
|
}
|
|
96
|
-
|
|
97
92
|
isStaleByTime(staleTime = 0) {
|
|
98
93
|
return this.state.isInvalidated || !this.state.dataUpdatedAt || !utils.timeUntilStale(this.state.dataUpdatedAt, staleTime);
|
|
99
94
|
}
|
|
100
|
-
|
|
101
95
|
onFocus() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (observer) {
|
|
107
|
-
observer.refetch({
|
|
108
|
-
cancelRefetch: false
|
|
109
|
-
});
|
|
110
|
-
} // Continue fetch if currently paused
|
|
111
|
-
|
|
96
|
+
const observer = this.#observers.find(x => x.shouldFetchOnWindowFocus());
|
|
97
|
+
observer?.refetch({
|
|
98
|
+
cancelRefetch: false
|
|
99
|
+
});
|
|
112
100
|
|
|
113
|
-
|
|
101
|
+
// Continue fetch if currently paused
|
|
102
|
+
this.#retryer?.continue();
|
|
114
103
|
}
|
|
115
|
-
|
|
116
104
|
onOnline() {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (observer) {
|
|
122
|
-
observer.refetch({
|
|
123
|
-
cancelRefetch: false
|
|
124
|
-
});
|
|
125
|
-
} // Continue fetch if currently paused
|
|
126
|
-
|
|
105
|
+
const observer = this.#observers.find(x => x.shouldFetchOnReconnect());
|
|
106
|
+
observer?.refetch({
|
|
107
|
+
cancelRefetch: false
|
|
108
|
+
});
|
|
127
109
|
|
|
128
|
-
|
|
110
|
+
// Continue fetch if currently paused
|
|
111
|
+
this.#retryer?.continue();
|
|
129
112
|
}
|
|
130
|
-
|
|
131
113
|
addObserver(observer) {
|
|
132
|
-
if (this
|
|
133
|
-
this
|
|
114
|
+
if (this.#observers.indexOf(observer) === -1) {
|
|
115
|
+
this.#observers.push(observer);
|
|
134
116
|
|
|
117
|
+
// Stop the query from being garbage collected
|
|
135
118
|
this.clearGcTimeout();
|
|
136
|
-
this
|
|
119
|
+
this.#cache.notify({
|
|
137
120
|
type: 'observerAdded',
|
|
138
121
|
query: this,
|
|
139
122
|
observer
|
|
140
123
|
});
|
|
141
124
|
}
|
|
142
125
|
}
|
|
143
|
-
|
|
144
126
|
removeObserver(observer) {
|
|
145
|
-
if (this
|
|
146
|
-
this
|
|
147
|
-
|
|
148
|
-
if (!this.observers.length) {
|
|
127
|
+
if (this.#observers.indexOf(observer) !== -1) {
|
|
128
|
+
this.#observers = this.#observers.filter(x => x !== observer);
|
|
129
|
+
if (!this.#observers.length) {
|
|
149
130
|
// If the transport layer does not support cancellation
|
|
150
131
|
// we'll let the query continue so the result can be cached
|
|
151
|
-
if (this
|
|
152
|
-
if (this
|
|
153
|
-
this
|
|
132
|
+
if (this.#retryer) {
|
|
133
|
+
if (this.#abortSignalConsumed) {
|
|
134
|
+
this.#retryer.cancel({
|
|
154
135
|
revert: true
|
|
155
136
|
});
|
|
156
137
|
} else {
|
|
157
|
-
this
|
|
138
|
+
this.#retryer.cancelRetry();
|
|
158
139
|
}
|
|
159
140
|
}
|
|
160
|
-
|
|
161
141
|
this.scheduleGc();
|
|
162
142
|
}
|
|
163
|
-
|
|
164
|
-
this.cache.notify({
|
|
143
|
+
this.#cache.notify({
|
|
165
144
|
type: 'observerRemoved',
|
|
166
145
|
query: this,
|
|
167
146
|
observer
|
|
168
147
|
});
|
|
169
148
|
}
|
|
170
149
|
}
|
|
171
|
-
|
|
172
150
|
getObserversCount() {
|
|
173
|
-
return this
|
|
151
|
+
return this.#observers.length;
|
|
174
152
|
}
|
|
175
|
-
|
|
176
153
|
invalidate() {
|
|
177
154
|
if (!this.state.isInvalidated) {
|
|
178
|
-
this
|
|
155
|
+
this.#dispatch({
|
|
179
156
|
type: 'invalidate'
|
|
180
157
|
});
|
|
181
158
|
}
|
|
182
159
|
}
|
|
183
|
-
|
|
184
160
|
fetch(options, fetchOptions) {
|
|
185
|
-
var _this$options$behavio, _context$fetchOptions;
|
|
186
|
-
|
|
187
161
|
if (this.state.fetchStatus !== 'idle') {
|
|
188
|
-
if (this.state.dataUpdatedAt && fetchOptions
|
|
162
|
+
if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
|
|
189
163
|
// Silently cancel current fetch if the user wants to cancel refetches
|
|
190
164
|
this.cancel({
|
|
191
165
|
silent: true
|
|
192
166
|
});
|
|
193
|
-
} else if (this
|
|
194
|
-
var _this$retryer4;
|
|
195
|
-
|
|
167
|
+
} else if (this.#promise) {
|
|
196
168
|
// make sure that retries that were potentially cancelled due to unmounts can continue
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return this
|
|
169
|
+
this.#retryer?.continueRetry();
|
|
170
|
+
// Return current promise if we are already fetching
|
|
171
|
+
return this.#promise;
|
|
200
172
|
}
|
|
201
|
-
}
|
|
202
|
-
|
|
173
|
+
}
|
|
203
174
|
|
|
175
|
+
// Update config if passed, otherwise the config from the last execution is used
|
|
204
176
|
if (options) {
|
|
205
|
-
this
|
|
206
|
-
}
|
|
207
|
-
// This can happen when the query is hydrated or created with setQueryData.
|
|
208
|
-
|
|
177
|
+
this.#setOptions(options);
|
|
178
|
+
}
|
|
209
179
|
|
|
180
|
+
// Use the options from the first observer with a query function if no function is found.
|
|
181
|
+
// This can happen when the query is hydrated or created with setQueryData.
|
|
210
182
|
if (!this.options.queryFn) {
|
|
211
|
-
const observer = this
|
|
212
|
-
|
|
183
|
+
const observer = this.#observers.find(x => x.options.queryFn);
|
|
213
184
|
if (observer) {
|
|
214
|
-
this
|
|
185
|
+
this.#setOptions(observer.options);
|
|
215
186
|
}
|
|
216
187
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
this.logger.error("As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']");
|
|
188
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
189
|
+
if (!Array.isArray(this.options.queryKey)) {
|
|
190
|
+
console.error(`As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`);
|
|
221
191
|
}
|
|
222
192
|
}
|
|
193
|
+
const abortController = new AbortController();
|
|
223
194
|
|
|
224
|
-
|
|
225
|
-
|
|
195
|
+
// Create query function context
|
|
226
196
|
const queryFnContext = {
|
|
227
197
|
queryKey: this.queryKey,
|
|
228
|
-
pageParam: undefined,
|
|
229
198
|
meta: this.meta
|
|
230
|
-
};
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// Adds an enumerable signal property to the object that
|
|
231
202
|
// which sets abortSignalConsumed to true when the signal
|
|
232
203
|
// is read.
|
|
233
|
-
|
|
234
204
|
const addSignalProperty = object => {
|
|
235
205
|
Object.defineProperty(object, 'signal', {
|
|
236
206
|
enumerable: true,
|
|
237
207
|
get: () => {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return abortController.signal;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return undefined;
|
|
208
|
+
this.#abortSignalConsumed = true;
|
|
209
|
+
return abortController.signal;
|
|
244
210
|
}
|
|
245
211
|
});
|
|
246
212
|
};
|
|
213
|
+
addSignalProperty(queryFnContext);
|
|
247
214
|
|
|
248
|
-
|
|
249
|
-
|
|
215
|
+
// Create fetch function
|
|
250
216
|
const fetchFn = () => {
|
|
251
217
|
if (!this.options.queryFn) {
|
|
252
|
-
return Promise.reject('Missing queryFn');
|
|
218
|
+
return Promise.reject(new Error('Missing queryFn'));
|
|
253
219
|
}
|
|
254
|
-
|
|
255
|
-
this.abortSignalConsumed = false;
|
|
220
|
+
this.#abortSignalConsumed = false;
|
|
256
221
|
return this.options.queryFn(queryFnContext);
|
|
257
|
-
};
|
|
258
|
-
|
|
222
|
+
};
|
|
259
223
|
|
|
224
|
+
// Trigger behavior hook
|
|
260
225
|
const context = {
|
|
261
226
|
fetchOptions,
|
|
262
227
|
options: this.options,
|
|
@@ -265,89 +230,74 @@ class Query extends removable.Removable {
|
|
|
265
230
|
fetchFn
|
|
266
231
|
};
|
|
267
232
|
addSignalProperty(context);
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
this.revertState = this.state; // Set to fetching state if not already in it
|
|
233
|
+
this.options.behavior?.onFetch(context);
|
|
271
234
|
|
|
272
|
-
|
|
273
|
-
|
|
235
|
+
// Store state in case the current fetch needs to be reverted
|
|
236
|
+
this.#revertState = this.state;
|
|
274
237
|
|
|
275
|
-
|
|
238
|
+
// Set to fetching state if not already in it
|
|
239
|
+
if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== context.fetchOptions?.meta) {
|
|
240
|
+
this.#dispatch({
|
|
276
241
|
type: 'fetch',
|
|
277
|
-
meta:
|
|
242
|
+
meta: context.fetchOptions?.meta
|
|
278
243
|
});
|
|
279
244
|
}
|
|
280
|
-
|
|
281
245
|
const onError = error => {
|
|
282
246
|
// Optimistically update state if needed
|
|
283
247
|
if (!(retryer.isCancelledError(error) && error.silent)) {
|
|
284
|
-
this
|
|
248
|
+
this.#dispatch({
|
|
285
249
|
type: 'error',
|
|
286
250
|
error: error
|
|
287
251
|
});
|
|
288
252
|
}
|
|
289
|
-
|
|
290
253
|
if (!retryer.isCancelledError(error)) {
|
|
291
|
-
var _this$cache$config$on, _this$cache$config;
|
|
292
|
-
|
|
293
254
|
// Notify cache callback
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
297
|
-
this.logger.error(error);
|
|
298
|
-
}
|
|
255
|
+
this.#cache.config.onError?.(error, this);
|
|
299
256
|
}
|
|
300
|
-
|
|
301
257
|
if (!this.isFetchingOptimistic) {
|
|
302
258
|
// Schedule query gc after fetching
|
|
303
259
|
this.scheduleGc();
|
|
304
260
|
}
|
|
305
|
-
|
|
306
261
|
this.isFetchingOptimistic = false;
|
|
307
|
-
};
|
|
308
|
-
|
|
262
|
+
};
|
|
309
263
|
|
|
310
|
-
|
|
264
|
+
// Try to fetch the data
|
|
265
|
+
this.#retryer = retryer.createRetryer({
|
|
311
266
|
fn: context.fetchFn,
|
|
312
|
-
abort: abortController
|
|
267
|
+
abort: abortController.abort.bind(abortController),
|
|
313
268
|
onSuccess: data => {
|
|
314
|
-
var _this$cache$config$on2, _this$cache$config2;
|
|
315
|
-
|
|
316
269
|
if (typeof data === 'undefined') {
|
|
317
270
|
if (process.env.NODE_ENV !== 'production') {
|
|
318
|
-
|
|
271
|
+
console.error(`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`);
|
|
319
272
|
}
|
|
320
|
-
|
|
321
273
|
onError(new Error('undefined'));
|
|
322
274
|
return;
|
|
323
275
|
}
|
|
276
|
+
this.setData(data);
|
|
324
277
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
(_this$cache$config$on2 = (_this$cache$config2 = this.cache.config).onSuccess) == null ? void 0 : _this$cache$config$on2.call(_this$cache$config2, data, this);
|
|
328
|
-
|
|
278
|
+
// Notify cache callback
|
|
279
|
+
this.#cache.config.onSuccess?.(data, this);
|
|
329
280
|
if (!this.isFetchingOptimistic) {
|
|
330
281
|
// Schedule query gc after fetching
|
|
331
282
|
this.scheduleGc();
|
|
332
283
|
}
|
|
333
|
-
|
|
334
284
|
this.isFetchingOptimistic = false;
|
|
335
285
|
},
|
|
336
286
|
onError,
|
|
337
287
|
onFail: (failureCount, error) => {
|
|
338
|
-
this
|
|
288
|
+
this.#dispatch({
|
|
339
289
|
type: 'failed',
|
|
340
290
|
failureCount,
|
|
341
291
|
error
|
|
342
292
|
});
|
|
343
293
|
},
|
|
344
294
|
onPause: () => {
|
|
345
|
-
this
|
|
295
|
+
this.#dispatch({
|
|
346
296
|
type: 'pause'
|
|
347
297
|
});
|
|
348
298
|
},
|
|
349
299
|
onContinue: () => {
|
|
350
|
-
this
|
|
300
|
+
this.#dispatch({
|
|
351
301
|
type: 'continue'
|
|
352
302
|
});
|
|
353
303
|
},
|
|
@@ -355,48 +305,46 @@ class Query extends removable.Removable {
|
|
|
355
305
|
retryDelay: context.options.retryDelay,
|
|
356
306
|
networkMode: context.options.networkMode
|
|
357
307
|
});
|
|
358
|
-
this
|
|
359
|
-
return this
|
|
308
|
+
this.#promise = this.#retryer.promise;
|
|
309
|
+
return this.#promise;
|
|
360
310
|
}
|
|
361
|
-
|
|
362
|
-
dispatch(action) {
|
|
311
|
+
#dispatch(action) {
|
|
363
312
|
const reducer = state => {
|
|
364
|
-
var _action$meta, _action$dataUpdatedAt;
|
|
365
|
-
|
|
366
313
|
switch (action.type) {
|
|
367
314
|
case 'failed':
|
|
368
|
-
return {
|
|
315
|
+
return {
|
|
316
|
+
...state,
|
|
369
317
|
fetchFailureCount: action.failureCount,
|
|
370
318
|
fetchFailureReason: action.error
|
|
371
319
|
};
|
|
372
|
-
|
|
373
320
|
case 'pause':
|
|
374
|
-
return {
|
|
321
|
+
return {
|
|
322
|
+
...state,
|
|
375
323
|
fetchStatus: 'paused'
|
|
376
324
|
};
|
|
377
|
-
|
|
378
325
|
case 'continue':
|
|
379
|
-
return {
|
|
326
|
+
return {
|
|
327
|
+
...state,
|
|
380
328
|
fetchStatus: 'fetching'
|
|
381
329
|
};
|
|
382
|
-
|
|
383
330
|
case 'fetch':
|
|
384
|
-
return {
|
|
331
|
+
return {
|
|
332
|
+
...state,
|
|
385
333
|
fetchFailureCount: 0,
|
|
386
334
|
fetchFailureReason: null,
|
|
387
|
-
fetchMeta:
|
|
335
|
+
fetchMeta: action.meta ?? null,
|
|
388
336
|
fetchStatus: retryer.canFetch(this.options.networkMode) ? 'fetching' : 'paused',
|
|
389
337
|
...(!state.dataUpdatedAt && {
|
|
390
338
|
error: null,
|
|
391
|
-
status: '
|
|
339
|
+
status: 'pending'
|
|
392
340
|
})
|
|
393
341
|
};
|
|
394
|
-
|
|
395
342
|
case 'success':
|
|
396
|
-
return {
|
|
343
|
+
return {
|
|
344
|
+
...state,
|
|
397
345
|
data: action.data,
|
|
398
346
|
dataUpdateCount: state.dataUpdateCount + 1,
|
|
399
|
-
dataUpdatedAt:
|
|
347
|
+
dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
|
|
400
348
|
error: null,
|
|
401
349
|
isInvalidated: false,
|
|
402
350
|
status: 'success',
|
|
@@ -406,16 +354,15 @@ class Query extends removable.Removable {
|
|
|
406
354
|
fetchFailureReason: null
|
|
407
355
|
})
|
|
408
356
|
};
|
|
409
|
-
|
|
410
357
|
case 'error':
|
|
411
358
|
const error = action.error;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
359
|
+
if (retryer.isCancelledError(error) && error.revert && this.#revertState) {
|
|
360
|
+
return {
|
|
361
|
+
...this.#revertState
|
|
415
362
|
};
|
|
416
363
|
}
|
|
417
|
-
|
|
418
|
-
|
|
364
|
+
return {
|
|
365
|
+
...state,
|
|
419
366
|
error: error,
|
|
420
367
|
errorUpdateCount: state.errorUpdateCount + 1,
|
|
421
368
|
errorUpdatedAt: Date.now(),
|
|
@@ -424,34 +371,31 @@ class Query extends removable.Removable {
|
|
|
424
371
|
fetchStatus: 'idle',
|
|
425
372
|
status: 'error'
|
|
426
373
|
};
|
|
427
|
-
|
|
428
374
|
case 'invalidate':
|
|
429
|
-
return {
|
|
375
|
+
return {
|
|
376
|
+
...state,
|
|
430
377
|
isInvalidated: true
|
|
431
378
|
};
|
|
432
|
-
|
|
433
379
|
case 'setState':
|
|
434
|
-
return {
|
|
380
|
+
return {
|
|
381
|
+
...state,
|
|
435
382
|
...action.state
|
|
436
383
|
};
|
|
437
384
|
}
|
|
438
385
|
};
|
|
439
|
-
|
|
440
386
|
this.state = reducer(this.state);
|
|
441
387
|
notifyManager.notifyManager.batch(() => {
|
|
442
|
-
this
|
|
388
|
+
this.#observers.forEach(observer => {
|
|
443
389
|
observer.onQueryUpdate(action);
|
|
444
390
|
});
|
|
445
|
-
this
|
|
391
|
+
this.#cache.notify({
|
|
446
392
|
query: this,
|
|
447
393
|
type: 'updated',
|
|
448
394
|
action
|
|
449
395
|
});
|
|
450
396
|
});
|
|
451
397
|
}
|
|
452
|
-
|
|
453
398
|
}
|
|
454
|
-
|
|
455
399
|
function getDefaultState(options) {
|
|
456
400
|
const data = typeof options.initialData === 'function' ? options.initialData() : options.initialData;
|
|
457
401
|
const hasData = typeof data !== 'undefined';
|
|
@@ -459,7 +403,7 @@ function getDefaultState(options) {
|
|
|
459
403
|
return {
|
|
460
404
|
data,
|
|
461
405
|
dataUpdateCount: 0,
|
|
462
|
-
dataUpdatedAt: hasData ? initialDataUpdatedAt
|
|
406
|
+
dataUpdatedAt: hasData ? initialDataUpdatedAt ?? Date.now() : 0,
|
|
463
407
|
error: null,
|
|
464
408
|
errorUpdateCount: 0,
|
|
465
409
|
errorUpdatedAt: 0,
|
|
@@ -467,7 +411,7 @@ function getDefaultState(options) {
|
|
|
467
411
|
fetchFailureReason: null,
|
|
468
412
|
fetchMeta: null,
|
|
469
413
|
isInvalidated: false,
|
|
470
|
-
status: hasData ? 'success' : '
|
|
414
|
+
status: hasData ? 'success' : 'pending',
|
|
471
415
|
fetchStatus: 'idle'
|
|
472
416
|
};
|
|
473
417
|
}
|