@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.
Files changed (184) hide show
  1. package/build/lib/focusManager.d.ts +1 -3
  2. package/build/lib/focusManager.esm.js +19 -36
  3. package/build/lib/focusManager.esm.js.map +1 -1
  4. package/build/lib/focusManager.js +19 -38
  5. package/build/lib/focusManager.js.map +1 -1
  6. package/build/lib/focusManager.mjs +19 -36
  7. package/build/lib/focusManager.mjs.map +1 -1
  8. package/build/lib/hydration.esm.js +21 -23
  9. package/build/lib/hydration.esm.js.map +1 -1
  10. package/build/lib/hydration.js +21 -25
  11. package/build/lib/hydration.js.map +1 -1
  12. package/build/lib/hydration.mjs +21 -23
  13. package/build/lib/hydration.mjs.map +1 -1
  14. package/build/lib/index.d.ts +1 -2
  15. package/build/lib/index.esm.js +1 -1
  16. package/build/lib/index.js +2 -8
  17. package/build/lib/index.js.map +1 -1
  18. package/build/lib/index.mjs +1 -1
  19. package/build/lib/infiniteQueryBehavior.d.ts +3 -7
  20. package/build/lib/infiniteQueryBehavior.esm.js +52 -75
  21. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  22. package/build/lib/infiniteQueryBehavior.js +50 -77
  23. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.mjs +52 -75
  25. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  26. package/build/lib/infiniteQueryObserver.d.ts +4 -4
  27. package/build/lib/infiniteQueryObserver.esm.js +18 -26
  28. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  29. package/build/lib/infiniteQueryObserver.js +18 -28
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.mjs +18 -26
  32. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  33. package/build/lib/mutation.d.ts +11 -22
  34. package/build/lib/mutation.esm.js +73 -105
  35. package/build/lib/mutation.esm.js.map +1 -1
  36. package/build/lib/mutation.js +73 -107
  37. package/build/lib/mutation.js.map +1 -1
  38. package/build/lib/mutation.mjs +73 -105
  39. package/build/lib/mutation.mjs.map +1 -1
  40. package/build/lib/mutationCache.d.ts +4 -6
  41. package/build/lib/mutationCache.esm.js +23 -32
  42. package/build/lib/mutationCache.esm.js.map +1 -1
  43. package/build/lib/mutationCache.js +23 -34
  44. package/build/lib/mutationCache.js.map +1 -1
  45. package/build/lib/mutationCache.mjs +23 -32
  46. package/build/lib/mutationCache.mjs.map +1 -1
  47. package/build/lib/mutationObserver.d.ts +4 -9
  48. package/build/lib/mutationObserver.esm.js +42 -73
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +42 -75
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +42 -73
  53. package/build/lib/mutationObserver.mjs.map +1 -1
  54. package/build/lib/notifyManager.esm.js +7 -17
  55. package/build/lib/notifyManager.esm.js.map +1 -1
  56. package/build/lib/notifyManager.js +7 -19
  57. package/build/lib/notifyManager.js.map +1 -1
  58. package/build/lib/notifyManager.mjs +7 -17
  59. package/build/lib/notifyManager.mjs.map +1 -1
  60. package/build/lib/onlineManager.d.ts +1 -3
  61. package/build/lib/onlineManager.esm.js +16 -30
  62. package/build/lib/onlineManager.esm.js.map +1 -1
  63. package/build/lib/onlineManager.js +16 -32
  64. package/build/lib/onlineManager.js.map +1 -1
  65. package/build/lib/onlineManager.mjs +16 -30
  66. package/build/lib/onlineManager.mjs.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +3 -10
  68. package/build/lib/queriesObserver.esm.js +47 -71
  69. package/build/lib/queriesObserver.esm.js.map +1 -1
  70. package/build/lib/queriesObserver.js +49 -75
  71. package/build/lib/queriesObserver.js.map +1 -1
  72. package/build/lib/queriesObserver.mjs +47 -71
  73. package/build/lib/queriesObserver.mjs.map +1 -1
  74. package/build/lib/query.d.ts +14 -21
  75. package/build/lib/query.esm.js +140 -194
  76. package/build/lib/query.esm.js.map +1 -1
  77. package/build/lib/query.js +139 -195
  78. package/build/lib/query.js.map +1 -1
  79. package/build/lib/query.mjs +140 -194
  80. package/build/lib/query.mjs.map +1 -1
  81. package/build/lib/queryCache.d.ts +12 -7
  82. package/build/lib/queryCache.esm.js +21 -45
  83. package/build/lib/queryCache.esm.js.map +1 -1
  84. package/build/lib/queryCache.js +20 -46
  85. package/build/lib/queryCache.js.map +1 -1
  86. package/build/lib/queryCache.mjs +21 -45
  87. package/build/lib/queryCache.mjs.map +1 -1
  88. package/build/lib/queryClient.d.ts +18 -46
  89. package/build/lib/queryClient.esm.js +137 -216
  90. package/build/lib/queryClient.esm.js.map +1 -1
  91. package/build/lib/queryClient.js +136 -217
  92. package/build/lib/queryClient.js.map +1 -1
  93. package/build/lib/queryClient.mjs +137 -216
  94. package/build/lib/queryClient.mjs.map +1 -1
  95. package/build/lib/queryObserver.d.ts +4 -29
  96. package/build/lib/queryObserver.esm.js +176 -258
  97. package/build/lib/queryObserver.esm.js.map +1 -1
  98. package/build/lib/queryObserver.js +176 -260
  99. package/build/lib/queryObserver.js.map +1 -1
  100. package/build/lib/queryObserver.mjs +176 -258
  101. package/build/lib/queryObserver.mjs.map +1 -1
  102. package/build/lib/removable.d.ts +3 -3
  103. package/build/lib/removable.esm.js +10 -14
  104. package/build/lib/removable.esm.js.map +1 -1
  105. package/build/lib/removable.js +10 -16
  106. package/build/lib/removable.js.map +1 -1
  107. package/build/lib/removable.mjs +10 -14
  108. package/build/lib/removable.mjs.map +1 -1
  109. package/build/lib/retryer.d.ts +5 -5
  110. package/build/lib/retryer.esm.js +27 -44
  111. package/build/lib/retryer.esm.js.map +1 -1
  112. package/build/lib/retryer.js +27 -46
  113. package/build/lib/retryer.js.map +1 -1
  114. package/build/lib/retryer.mjs +27 -44
  115. package/build/lib/retryer.mjs.map +1 -1
  116. package/build/lib/subscribable.esm.js +4 -7
  117. package/build/lib/subscribable.esm.js.map +1 -1
  118. package/build/lib/subscribable.js +4 -9
  119. package/build/lib/subscribable.js.map +1 -1
  120. package/build/lib/subscribable.mjs +4 -7
  121. package/build/lib/subscribable.mjs.map +1 -1
  122. package/build/lib/tests/utils.d.ts +3 -12
  123. package/build/lib/types.d.ts +111 -99
  124. package/build/lib/utils.d.ts +8 -18
  125. package/build/lib/utils.esm.js +39 -132
  126. package/build/lib/utils.esm.js.map +1 -1
  127. package/build/lib/utils.js +42 -144
  128. package/build/lib/utils.js.map +1 -1
  129. package/build/lib/utils.mjs +39 -132
  130. package/build/lib/utils.mjs.map +1 -1
  131. package/build/umd/index.development.js +867 -1399
  132. package/build/umd/index.development.js.map +1 -1
  133. package/build/umd/index.production.js +1 -1
  134. package/build/umd/index.production.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/focusManager.ts +17 -24
  137. package/src/index.ts +1 -11
  138. package/src/infiniteQueryBehavior.ts +54 -94
  139. package/src/infiniteQueryObserver.ts +10 -12
  140. package/src/mutation.ts +68 -92
  141. package/src/mutationCache.ts +27 -27
  142. package/src/mutationObserver.ts +58 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +107 -111
  146. package/src/queryCache.ts +42 -41
  147. package/src/queryClient.ts +155 -434
  148. package/src/queryObserver.ts +155 -192
  149. package/src/removable.ts +13 -13
  150. package/src/retryer.ts +5 -5
  151. package/src/subscribable.ts +1 -1
  152. package/src/tests/focusManager.test.tsx +25 -25
  153. package/src/tests/hydration.test.tsx +167 -81
  154. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  155. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  156. package/src/tests/mutationCache.test.tsx +127 -127
  157. package/src/tests/mutationObserver.test.tsx +1 -31
  158. package/src/tests/mutations.test.tsx +62 -43
  159. package/src/tests/onlineManager.test.tsx +12 -4
  160. package/src/tests/queriesObserver.test.tsx +41 -77
  161. package/src/tests/query.test.tsx +175 -243
  162. package/src/tests/queryCache.test.tsx +170 -93
  163. package/src/tests/queryClient.test.tsx +229 -378
  164. package/src/tests/queryObserver.test.tsx +23 -147
  165. package/src/tests/utils.test.tsx +84 -29
  166. package/src/tests/utils.ts +9 -18
  167. package/src/types.ts +187 -140
  168. package/src/utils.ts +31 -124
  169. package/build/lib/logger.d.ts +0 -8
  170. package/build/lib/logger.esm.js +0 -4
  171. package/build/lib/logger.esm.js.map +0 -1
  172. package/build/lib/logger.js +0 -8
  173. package/build/lib/logger.js.map +0 -1
  174. package/build/lib/logger.mjs +0 -4
  175. package/build/lib/logger.mjs.map +0 -1
  176. package/build/lib/logger.native.d.ts +0 -6
  177. package/build/lib/logger.native.esm.js +0 -12
  178. package/build/lib/logger.native.esm.js.map +0 -1
  179. package/build/lib/logger.native.js +0 -16
  180. package/build/lib/logger.native.js.map +0 -1
  181. package/build/lib/logger.native.mjs +0 -12
  182. package/build/lib/logger.native.mjs.map +0 -1
  183. package/src/logger.native.ts +0 -11
  184. package/src/logger.ts +0 -9
@@ -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.abortSignalConsumed = false;
16
- this.defaultOptions = config.defaultOptions;
17
- this.setOptions(config.options);
18
- this.observers = [];
19
- this.cache = config.cache;
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.initialState = config.state || getDefaultState(this.options);
24
- this.state = this.initialState;
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
- setOptions(options) {
33
- this.options = { ...this.defaultOptions,
37
+ #setOptions(options) {
38
+ this.options = {
39
+ ...this.#defaultOptions,
34
40
  ...options
35
41
  };
36
- this.updateCacheTime(this.options.cacheTime);
42
+ this.updateGcTime(this.options.gcTime);
37
43
  }
38
-
39
44
  optionalRemove() {
40
- if (!this.observers.length && this.state.fetchStatus === 'idle') {
41
- this.cache.remove(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); // Set data and mark it as cached
50
+ const data = utils.replaceData(this.state.data, newData, this.options);
47
51
 
48
- this.dispatch({
52
+ // Set data and mark it as cached
53
+ this.#dispatch({
49
54
  data,
50
55
  type: 'success',
51
- dataUpdatedAt: options == null ? void 0 : options.updatedAt,
52
- manual: options == null ? void 0 : options.manual
56
+ dataUpdatedAt: options?.updatedAt,
57
+ manual: options?.manual
53
58
  });
54
59
  return data;
55
60
  }
56
-
57
61
  setState(state, setStateOptions) {
58
- this.dispatch({
62
+ this.#dispatch({
59
63
  type: 'setState',
60
64
  state,
61
65
  setStateOptions
62
66
  });
63
67
  }
64
-
65
68
  cancel(options) {
66
- var _this$retryer;
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.initialState);
81
+ this.setState(this.#initialState);
83
82
  }
84
-
85
83
  isActive() {
86
- return this.observers.some(observer => observer.options.enabled !== false);
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.observers.some(observer => observer.getCurrentResult().isStale);
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
- var _this$retryer2;
103
-
104
- const observer = this.observers.find(x => x.shouldFetchOnWindowFocus());
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
- (_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
101
+ // Continue fetch if currently paused
102
+ this.#retryer?.continue();
114
103
  }
115
-
116
104
  onOnline() {
117
- var _this$retryer3;
118
-
119
- const observer = this.observers.find(x => x.shouldFetchOnReconnect());
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
- (_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
110
+ // Continue fetch if currently paused
111
+ this.#retryer?.continue();
129
112
  }
130
-
131
113
  addObserver(observer) {
132
- if (this.observers.indexOf(observer) === -1) {
133
- this.observers.push(observer); // Stop the query from being garbage collected
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.cache.notify({
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.observers.indexOf(observer) !== -1) {
146
- this.observers = this.observers.filter(x => x !== observer);
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.retryer) {
152
- if (this.abortSignalConsumed) {
153
- this.retryer.cancel({
132
+ if (this.#retryer) {
133
+ if (this.#abortSignalConsumed) {
134
+ this.#retryer.cancel({
154
135
  revert: true
155
136
  });
156
137
  } else {
157
- this.retryer.cancelRetry();
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.observers.length;
151
+ return this.#observers.length;
174
152
  }
175
-
176
153
  invalidate() {
177
154
  if (!this.state.isInvalidated) {
178
- this.dispatch({
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 != null && fetchOptions.cancelRefetch) {
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.promise) {
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
- (_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry(); // Return current promise if we are already fetching
198
-
199
- return this.promise;
169
+ this.#retryer?.continueRetry();
170
+ // Return current promise if we are already fetching
171
+ return this.#promise;
200
172
  }
201
- } // Update config if passed, otherwise the config from the last execution is used
202
-
173
+ }
203
174
 
175
+ // Update config if passed, otherwise the config from the last execution is used
204
176
  if (options) {
205
- this.setOptions(options);
206
- } // Use the options from the first observer with a query function if no function is found.
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.observers.find(x => x.options.queryFn);
212
-
183
+ const observer = this.#observers.find(x => x.options.queryFn);
213
184
  if (observer) {
214
- this.setOptions(observer.options);
185
+ this.#setOptions(observer.options);
215
186
  }
216
187
  }
217
-
218
- if (!Array.isArray(this.options.queryKey)) {
219
- if (process.env.NODE_ENV !== 'production') {
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
- const abortController = utils.getAbortController(); // Create query function context
225
-
195
+ // Create query function context
226
196
  const queryFnContext = {
227
197
  queryKey: this.queryKey,
228
- pageParam: undefined,
229
198
  meta: this.meta
230
- }; // Adds an enumerable signal property to the object that
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
- if (abortController) {
239
- this.abortSignalConsumed = true;
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
- addSignalProperty(queryFnContext); // Create fetch function
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
- }; // Trigger behavior hook
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
- (_this$options$behavio = this.options.behavior) == null ? void 0 : _this$options$behavio.onFetch(context); // Store state in case the current fetch needs to be reverted
269
-
270
- this.revertState = this.state; // Set to fetching state if not already in it
233
+ this.options.behavior?.onFetch(context);
271
234
 
272
- if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
273
- var _context$fetchOptions2;
235
+ // Store state in case the current fetch needs to be reverted
236
+ this.#revertState = this.state;
274
237
 
275
- this.dispatch({
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: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.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.dispatch({
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
- (_this$cache$config$on = (_this$cache$config = this.cache.config).onError) == null ? void 0 : _this$cache$config$on.call(_this$cache$config, error, this);
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
- }; // Try to fetch the data
308
-
262
+ };
309
263
 
310
- this.retryer = retryer.createRetryer({
264
+ // Try to fetch the data
265
+ this.#retryer = retryer.createRetryer({
311
266
  fn: context.fetchFn,
312
- abort: abortController == null ? void 0 : abortController.abort.bind(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
- this.logger.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);
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
- this.setData(data); // Notify cache callback
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.dispatch({
288
+ this.#dispatch({
339
289
  type: 'failed',
340
290
  failureCount,
341
291
  error
342
292
  });
343
293
  },
344
294
  onPause: () => {
345
- this.dispatch({
295
+ this.#dispatch({
346
296
  type: 'pause'
347
297
  });
348
298
  },
349
299
  onContinue: () => {
350
- this.dispatch({
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.promise = this.retryer.promise;
359
- return this.promise;
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 { ...state,
315
+ return {
316
+ ...state,
369
317
  fetchFailureCount: action.failureCount,
370
318
  fetchFailureReason: action.error
371
319
  };
372
-
373
320
  case 'pause':
374
- return { ...state,
321
+ return {
322
+ ...state,
375
323
  fetchStatus: 'paused'
376
324
  };
377
-
378
325
  case 'continue':
379
- return { ...state,
326
+ return {
327
+ ...state,
380
328
  fetchStatus: 'fetching'
381
329
  };
382
-
383
330
  case 'fetch':
384
- return { ...state,
331
+ return {
332
+ ...state,
385
333
  fetchFailureCount: 0,
386
334
  fetchFailureReason: null,
387
- fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
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: 'loading'
339
+ status: 'pending'
392
340
  })
393
341
  };
394
-
395
342
  case 'success':
396
- return { ...state,
343
+ return {
344
+ ...state,
397
345
  data: action.data,
398
346
  dataUpdateCount: state.dataUpdateCount + 1,
399
- dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
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
- if (retryer.isCancelledError(error) && error.revert && this.revertState) {
414
- return { ...this.revertState
359
+ if (retryer.isCancelledError(error) && error.revert && this.#revertState) {
360
+ return {
361
+ ...this.#revertState
415
362
  };
416
363
  }
417
-
418
- return { ...state,
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 { ...state,
375
+ return {
376
+ ...state,
430
377
  isInvalidated: true
431
378
  };
432
-
433
379
  case 'setState':
434
- return { ...state,
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.observers.forEach(observer => {
388
+ this.#observers.forEach(observer => {
443
389
  observer.onQueryUpdate(action);
444
390
  });
445
- this.cache.notify({
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 != null ? initialDataUpdatedAt : Date.now() : 0,
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' : 'loading',
414
+ status: hasData ? 'success' : 'pending',
471
415
  fetchStatus: 'idle'
472
416
  };
473
417
  }