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