@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.
Files changed (183) 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 +43 -72
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +43 -74
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +43 -72
  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 +868 -1398
  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 +60 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +106 -110
  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/tests/focusManager.test.tsx +25 -25
  152. package/src/tests/hydration.test.tsx +167 -81
  153. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  154. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  155. package/src/tests/mutationCache.test.tsx +127 -127
  156. package/src/tests/mutationObserver.test.tsx +1 -31
  157. package/src/tests/mutations.test.tsx +62 -43
  158. package/src/tests/onlineManager.test.tsx +12 -4
  159. package/src/tests/queriesObserver.test.tsx +41 -77
  160. package/src/tests/query.test.tsx +175 -243
  161. package/src/tests/queryCache.test.tsx +170 -93
  162. package/src/tests/queryClient.test.tsx +229 -378
  163. package/src/tests/queryObserver.test.tsx +23 -147
  164. package/src/tests/utils.test.tsx +84 -29
  165. package/src/tests/utils.ts +9 -18
  166. package/src/types.ts +187 -140
  167. package/src/utils.ts +31 -124
  168. package/build/lib/logger.d.ts +0 -8
  169. package/build/lib/logger.esm.js +0 -4
  170. package/build/lib/logger.esm.js.map +0 -1
  171. package/build/lib/logger.js +0 -8
  172. package/build/lib/logger.js.map +0 -1
  173. package/build/lib/logger.mjs +0 -4
  174. package/build/lib/logger.mjs.map +0 -1
  175. package/build/lib/logger.native.d.ts +0 -6
  176. package/build/lib/logger.native.esm.js +0 -12
  177. package/build/lib/logger.native.esm.js.map +0 -1
  178. package/build/lib/logger.native.js +0 -16
  179. package/build/lib/logger.native.js.map +0 -1
  180. package/build/lib/logger.native.mjs +0 -12
  181. package/build/lib/logger.native.mjs.map +0 -1
  182. package/src/logger.native.ts +0 -11
  183. package/src/logger.ts +0 -9
@@ -1,258 +1,225 @@
1
- import { replaceData, noop, timeUntilStale, getAbortController } from './utils.mjs';
2
- import { defaultLogger } from './logger.mjs';
1
+ import { replaceData, noop, timeUntilStale } from './utils.mjs';
3
2
  import { notifyManager } from './notifyManager.mjs';
4
3
  import { createRetryer, isCancelledError, canFetch } from './retryer.mjs';
5
4
  import { Removable } from './removable.mjs';
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.abortSignalConsumed = false;
12
- this.defaultOptions = config.defaultOptions;
13
- this.setOptions(config.options);
14
- this.observers = [];
15
- this.cache = config.cache;
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.initialState = config.state || getDefaultState(this.options);
20
- this.state = this.initialState;
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
- setOptions(options) {
29
- this.options = { ...this.defaultOptions,
35
+ #setOptions(options) {
36
+ this.options = {
37
+ ...this.#defaultOptions,
30
38
  ...options
31
39
  };
32
- this.updateCacheTime(this.options.cacheTime);
40
+ this.updateGcTime(this.options.gcTime);
33
41
  }
34
-
35
42
  optionalRemove() {
36
- if (!this.observers.length && this.state.fetchStatus === 'idle') {
37
- this.cache.remove(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); // Set data and mark it as cached
48
+ const data = replaceData(this.state.data, newData, this.options);
43
49
 
44
- this.dispatch({
50
+ // Set data and mark it as cached
51
+ this.#dispatch({
45
52
  data,
46
53
  type: 'success',
47
- dataUpdatedAt: options == null ? void 0 : options.updatedAt,
48
- manual: options == null ? void 0 : options.manual
54
+ dataUpdatedAt: options?.updatedAt,
55
+ manual: options?.manual
49
56
  });
50
57
  return data;
51
58
  }
52
-
53
59
  setState(state, setStateOptions) {
54
- this.dispatch({
60
+ this.#dispatch({
55
61
  type: 'setState',
56
62
  state,
57
63
  setStateOptions
58
64
  });
59
65
  }
60
-
61
66
  cancel(options) {
62
- var _this$retryer;
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.initialState);
79
+ this.setState(this.#initialState);
79
80
  }
80
-
81
81
  isActive() {
82
- return this.observers.some(observer => observer.options.enabled !== false);
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.observers.some(observer => observer.getCurrentResult().isStale);
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
- var _this$retryer2;
99
-
100
- const observer = this.observers.find(x => x.shouldFetchOnWindowFocus());
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
- (_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
99
+ // Continue fetch if currently paused
100
+ this.#retryer?.continue();
110
101
  }
111
-
112
102
  onOnline() {
113
- var _this$retryer3;
114
-
115
- const observer = this.observers.find(x => x.shouldFetchOnReconnect());
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
- (_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
108
+ // Continue fetch if currently paused
109
+ this.#retryer?.continue();
125
110
  }
126
-
127
111
  addObserver(observer) {
128
- if (this.observers.indexOf(observer) === -1) {
129
- this.observers.push(observer); // Stop the query from being garbage collected
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.cache.notify({
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.observers.indexOf(observer) !== -1) {
142
- this.observers = this.observers.filter(x => x !== observer);
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.retryer) {
148
- if (this.abortSignalConsumed) {
149
- this.retryer.cancel({
130
+ if (this.#retryer) {
131
+ if (this.#abortSignalConsumed) {
132
+ this.#retryer.cancel({
150
133
  revert: true
151
134
  });
152
135
  } else {
153
- this.retryer.cancelRetry();
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.observers.length;
149
+ return this.#observers.length;
170
150
  }
171
-
172
151
  invalidate() {
173
152
  if (!this.state.isInvalidated) {
174
- this.dispatch({
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 != null && fetchOptions.cancelRefetch) {
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.promise) {
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
- (_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry(); // Return current promise if we are already fetching
194
-
195
- return this.promise;
167
+ this.#retryer?.continueRetry();
168
+ // Return current promise if we are already fetching
169
+ return this.#promise;
196
170
  }
197
- } // Update config if passed, otherwise the config from the last execution is used
198
-
171
+ }
199
172
 
173
+ // Update config if passed, otherwise the config from the last execution is used
200
174
  if (options) {
201
- this.setOptions(options);
202
- } // Use the options from the first observer with a query function if no function is found.
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.observers.find(x => x.options.queryFn);
208
-
181
+ const observer = this.#observers.find(x => x.options.queryFn);
209
182
  if (observer) {
210
- this.setOptions(observer.options);
183
+ this.#setOptions(observer.options);
211
184
  }
212
185
  }
213
-
214
- if (!Array.isArray(this.options.queryKey)) {
215
- if (process.env.NODE_ENV !== 'production') {
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
- const abortController = getAbortController(); // Create query function context
221
-
193
+ // Create query function context
222
194
  const queryFnContext = {
223
195
  queryKey: this.queryKey,
224
- pageParam: undefined,
225
196
  meta: this.meta
226
- }; // Adds an enumerable signal property to the object that
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
- if (abortController) {
235
- this.abortSignalConsumed = true;
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
- addSignalProperty(queryFnContext); // Create fetch function
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
- }; // Trigger behavior hook
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
- (_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
265
-
266
- this.revertState = this.state; // Set to fetching state if not already in it
231
+ this.options.behavior?.onFetch(context);
267
232
 
268
- if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
269
- var _context$fetchOptions2;
233
+ // Store state in case the current fetch needs to be reverted
234
+ this.#revertState = this.state;
270
235
 
271
- this.dispatch({
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: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.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.dispatch({
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
- (_this$cache$config$on = (_this$cache$config = this.cache.config).onError) == null ? void 0 : _this$cache$config$on.call(_this$cache$config, error, this);
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
- }; // Try to fetch the data
304
-
260
+ };
305
261
 
306
- this.retryer = createRetryer({
262
+ // Try to fetch the data
263
+ this.#retryer = createRetryer({
307
264
  fn: context.fetchFn,
308
- abort: abortController == null ? void 0 : abortController.abort.bind(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
- 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);
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
- this.setData(data); // Notify cache callback
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.dispatch({
286
+ this.#dispatch({
335
287
  type: 'failed',
336
288
  failureCount,
337
289
  error
338
290
  });
339
291
  },
340
292
  onPause: () => {
341
- this.dispatch({
293
+ this.#dispatch({
342
294
  type: 'pause'
343
295
  });
344
296
  },
345
297
  onContinue: () => {
346
- this.dispatch({
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.promise = this.retryer.promise;
355
- return this.promise;
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 { ...state,
313
+ return {
314
+ ...state,
365
315
  fetchFailureCount: action.failureCount,
366
316
  fetchFailureReason: action.error
367
317
  };
368
-
369
318
  case 'pause':
370
- return { ...state,
319
+ return {
320
+ ...state,
371
321
  fetchStatus: 'paused'
372
322
  };
373
-
374
323
  case 'continue':
375
- return { ...state,
324
+ return {
325
+ ...state,
376
326
  fetchStatus: 'fetching'
377
327
  };
378
-
379
328
  case 'fetch':
380
- return { ...state,
329
+ return {
330
+ ...state,
381
331
  fetchFailureCount: 0,
382
332
  fetchFailureReason: null,
383
- fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
333
+ fetchMeta: action.meta ?? null,
384
334
  fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused',
385
335
  ...(!state.dataUpdatedAt && {
386
336
  error: null,
387
- status: 'loading'
337
+ status: 'pending'
388
338
  })
389
339
  };
390
-
391
340
  case 'success':
392
- return { ...state,
341
+ return {
342
+ ...state,
393
343
  data: action.data,
394
344
  dataUpdateCount: state.dataUpdateCount + 1,
395
- dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
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
- if (isCancelledError(error) && error.revert && this.revertState) {
410
- return { ...this.revertState
357
+ if (isCancelledError(error) && error.revert && this.#revertState) {
358
+ return {
359
+ ...this.#revertState
411
360
  };
412
361
  }
413
-
414
- return { ...state,
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 { ...state,
373
+ return {
374
+ ...state,
426
375
  isInvalidated: true
427
376
  };
428
-
429
377
  case 'setState':
430
- return { ...state,
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.observers.forEach(observer => {
386
+ this.#observers.forEach(observer => {
439
387
  observer.onQueryUpdate(action);
440
388
  });
441
- this.cache.notify({
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 != null ? initialDataUpdatedAt : Date.now() : 0,
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' : 'loading',
412
+ status: hasData ? 'success' : 'pending',
467
413
  fetchStatus: 'idle'
468
414
  };
469
415
  }