@frkntmbs/strapi-plugin-video-optimizer 1.0.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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +286 -0
  3. package/admin/custom.d.ts +8 -0
  4. package/admin/src/buildVersion.ts +3 -0
  5. package/admin/src/components/AssetOptimizationLabel.tsx +61 -0
  6. package/admin/src/components/BridgeProviders.tsx +123 -0
  7. package/admin/src/components/MediaLibraryCacheBridge.tsx +24 -0
  8. package/admin/src/components/MediaLibraryCardActionsBridge.tsx +249 -0
  9. package/admin/src/components/MediaLibraryJobWatcher.tsx +136 -0
  10. package/admin/src/components/MediaLibraryProgressBridge.tsx +97 -0
  11. package/admin/src/components/OptimizationChoicePicker.tsx +65 -0
  12. package/admin/src/components/OptimizationResizeFields.tsx +120 -0
  13. package/admin/src/components/OptimizationVideoFields.tsx +217 -0
  14. package/admin/src/components/UploadEnhancerBridge.tsx +205 -0
  15. package/admin/src/components/upload/PendingAssetStep.tsx +97 -0
  16. package/admin/src/defaultGlobalSettings.ts +32 -0
  17. package/admin/src/hooks/useDefaultOptimizationMode.ts +24 -0
  18. package/admin/src/hooks/useUploadWithOptimizer.ts +45 -0
  19. package/admin/src/index.ts +84 -0
  20. package/admin/src/pages/SettingsPage.tsx +208 -0
  21. package/admin/src/pluginId.ts +79 -0
  22. package/admin/src/translations/en.json +74 -0
  23. package/admin/src/translations/tr.json +74 -0
  24. package/admin/src/utils/adminFetch.ts +57 -0
  25. package/admin/src/utils/captureQueryClient.ts +34 -0
  26. package/admin/src/utils/debugMediaLibraryProgress.ts +70 -0
  27. package/admin/src/utils/extractAssetDimensions.ts +22 -0
  28. package/admin/src/utils/initJobPoller.ts +173 -0
  29. package/admin/src/utils/initMediaLibraryCardActions.ts +308 -0
  30. package/admin/src/utils/initMediaLibraryProgress.ts +219 -0
  31. package/admin/src/utils/initUploadEnhancer.ts +447 -0
  32. package/admin/src/utils/invalidateMediaLibrary.ts +203 -0
  33. package/admin/src/utils/jobProgressStore.ts +113 -0
  34. package/admin/src/utils/mediaLibraryCardMatch.ts +414 -0
  35. package/admin/src/utils/mediaLibraryCardStore.ts +223 -0
  36. package/admin/src/utils/mediaLibraryQueryBridge.ts +113 -0
  37. package/admin/src/utils/mediaLibraryRoute.ts +9 -0
  38. package/admin/src/utils/optimizationFields.ts +17 -0
  39. package/admin/src/utils/probeVideoDimensions.ts +94 -0
  40. package/admin/src/utils/uploadAssetStore.ts +670 -0
  41. package/admin/tsconfig.json +8 -0
  42. package/dist/admin/SettingsPage-CN2fR83m.js +150 -0
  43. package/dist/admin/SettingsPage-D6e536P0.mjs +150 -0
  44. package/dist/admin/en-CqM903j3.js +77 -0
  45. package/dist/admin/en-CsHicGzL.mjs +77 -0
  46. package/dist/admin/index-BjWoS0YU.js +2542 -0
  47. package/dist/admin/index-Cs_uiChW.mjs +2541 -0
  48. package/dist/admin/index-DOuHOS2G.js +8799 -0
  49. package/dist/admin/index-rAmxCQz6.mjs +8781 -0
  50. package/dist/admin/index.js +4 -0
  51. package/dist/admin/index.mjs +4 -0
  52. package/dist/admin/tr-Y0-ANilh.mjs +77 -0
  53. package/dist/admin/tr-muzHkdC4.js +77 -0
  54. package/dist/server/index.js +1538 -0
  55. package/dist/server/index.mjs +1533 -0
  56. package/package.json +100 -0
  57. package/server/index.js +1 -0
  58. package/server/src/bootstrap.ts +377 -0
  59. package/server/src/buildVersion.ts +1 -0
  60. package/server/src/config/defaults.ts +91 -0
  61. package/server/src/config/index.ts +51 -0
  62. package/server/src/constants.ts +83 -0
  63. package/server/src/controllers/index.ts +7 -0
  64. package/server/src/controllers/job.ts +102 -0
  65. package/server/src/controllers/preference.ts +206 -0
  66. package/server/src/index.ts +15 -0
  67. package/server/src/register.ts +19 -0
  68. package/server/src/routes/index.ts +103 -0
  69. package/server/src/services/index.ts +9 -0
  70. package/server/src/services/job-queue.ts +663 -0
  71. package/server/src/services/optimizer.ts +284 -0
  72. package/server/src/services/preference.ts +172 -0
  73. package/server/src/utils/request-context.ts +7 -0
  74. package/server/src/utils/upload-preferences-context.ts +202 -0
  75. package/server/tsconfig.json +8 -0
  76. package/strapi-admin.js +7 -0
  77. package/strapi-server.js +7 -0
@@ -0,0 +1,2541 @@
1
+ import { i as isServer, s as sleep, _ as _extends, e as isValidTimeout, f as functionalUpdate, r as replaceEqualDeep, n as noop, t as timeUntilStale, h as ensureQueryKeyArray, j as getAbortController, k as notifyManager, l as getLogger, o as hashQueryKeyByOptions, p as parseFilterArgs, q as matchQuery, v as matchMutation, w as parseQueryArgs, x as hashQueryKey, y as partialMatchKey, z as shallowEqualObjects, A as difference, B as replaceAt, C as useQueryClient, E as parseMutationFilterArgs, F as parseMutationArgs } from "./index-rAmxCQz6.mjs";
2
+ import { Q, G, H } from "./index-rAmxCQz6.mjs";
3
+ import React__default, { useMemo } from "react";
4
+ function _setPrototypeOf(t, e) {
5
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(t2, e2) {
6
+ return t2.__proto__ = e2, t2;
7
+ }, _setPrototypeOf(t, e);
8
+ }
9
+ function _inheritsLoose(t, o) {
10
+ t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
11
+ }
12
+ var Subscribable = /* @__PURE__ */ (function() {
13
+ function Subscribable2() {
14
+ this.listeners = [];
15
+ }
16
+ var _proto = Subscribable2.prototype;
17
+ _proto.subscribe = function subscribe(listener) {
18
+ var _this = this;
19
+ var callback = listener || function() {
20
+ return void 0;
21
+ };
22
+ this.listeners.push(callback);
23
+ this.onSubscribe();
24
+ return function() {
25
+ _this.listeners = _this.listeners.filter(function(x) {
26
+ return x !== callback;
27
+ });
28
+ _this.onUnsubscribe();
29
+ };
30
+ };
31
+ _proto.hasListeners = function hasListeners() {
32
+ return this.listeners.length > 0;
33
+ };
34
+ _proto.onSubscribe = function onSubscribe() {
35
+ };
36
+ _proto.onUnsubscribe = function onUnsubscribe() {
37
+ };
38
+ return Subscribable2;
39
+ })();
40
+ var FocusManager = /* @__PURE__ */ (function(_Subscribable) {
41
+ _inheritsLoose(FocusManager2, _Subscribable);
42
+ function FocusManager2() {
43
+ var _this;
44
+ _this = _Subscribable.call(this) || this;
45
+ _this.setup = function(onFocus) {
46
+ var _window;
47
+ if (!isServer && ((_window = window) == null ? void 0 : _window.addEventListener)) {
48
+ var listener = function listener2() {
49
+ return onFocus();
50
+ };
51
+ window.addEventListener("visibilitychange", listener, false);
52
+ window.addEventListener("focus", listener, false);
53
+ return function() {
54
+ window.removeEventListener("visibilitychange", listener);
55
+ window.removeEventListener("focus", listener);
56
+ };
57
+ }
58
+ };
59
+ return _this;
60
+ }
61
+ var _proto = FocusManager2.prototype;
62
+ _proto.onSubscribe = function onSubscribe() {
63
+ if (!this.cleanup) {
64
+ this.setEventListener(this.setup);
65
+ }
66
+ };
67
+ _proto.onUnsubscribe = function onUnsubscribe() {
68
+ if (!this.hasListeners()) {
69
+ var _this$cleanup;
70
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
71
+ this.cleanup = void 0;
72
+ }
73
+ };
74
+ _proto.setEventListener = function setEventListener(setup) {
75
+ var _this$cleanup2, _this2 = this;
76
+ this.setup = setup;
77
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
78
+ this.cleanup = setup(function(focused) {
79
+ if (typeof focused === "boolean") {
80
+ _this2.setFocused(focused);
81
+ } else {
82
+ _this2.onFocus();
83
+ }
84
+ });
85
+ };
86
+ _proto.setFocused = function setFocused(focused) {
87
+ this.focused = focused;
88
+ if (focused) {
89
+ this.onFocus();
90
+ }
91
+ };
92
+ _proto.onFocus = function onFocus() {
93
+ this.listeners.forEach(function(listener) {
94
+ listener();
95
+ });
96
+ };
97
+ _proto.isFocused = function isFocused() {
98
+ if (typeof this.focused === "boolean") {
99
+ return this.focused;
100
+ }
101
+ if (typeof document === "undefined") {
102
+ return true;
103
+ }
104
+ return [void 0, "visible", "prerender"].includes(document.visibilityState);
105
+ };
106
+ return FocusManager2;
107
+ })(Subscribable);
108
+ var focusManager = new FocusManager();
109
+ var OnlineManager = /* @__PURE__ */ (function(_Subscribable) {
110
+ _inheritsLoose(OnlineManager2, _Subscribable);
111
+ function OnlineManager2() {
112
+ var _this;
113
+ _this = _Subscribable.call(this) || this;
114
+ _this.setup = function(onOnline) {
115
+ var _window;
116
+ if (!isServer && ((_window = window) == null ? void 0 : _window.addEventListener)) {
117
+ var listener = function listener2() {
118
+ return onOnline();
119
+ };
120
+ window.addEventListener("online", listener, false);
121
+ window.addEventListener("offline", listener, false);
122
+ return function() {
123
+ window.removeEventListener("online", listener);
124
+ window.removeEventListener("offline", listener);
125
+ };
126
+ }
127
+ };
128
+ return _this;
129
+ }
130
+ var _proto = OnlineManager2.prototype;
131
+ _proto.onSubscribe = function onSubscribe() {
132
+ if (!this.cleanup) {
133
+ this.setEventListener(this.setup);
134
+ }
135
+ };
136
+ _proto.onUnsubscribe = function onUnsubscribe() {
137
+ if (!this.hasListeners()) {
138
+ var _this$cleanup;
139
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
140
+ this.cleanup = void 0;
141
+ }
142
+ };
143
+ _proto.setEventListener = function setEventListener(setup) {
144
+ var _this$cleanup2, _this2 = this;
145
+ this.setup = setup;
146
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
147
+ this.cleanup = setup(function(online) {
148
+ if (typeof online === "boolean") {
149
+ _this2.setOnline(online);
150
+ } else {
151
+ _this2.onOnline();
152
+ }
153
+ });
154
+ };
155
+ _proto.setOnline = function setOnline(online) {
156
+ this.online = online;
157
+ if (online) {
158
+ this.onOnline();
159
+ }
160
+ };
161
+ _proto.onOnline = function onOnline() {
162
+ this.listeners.forEach(function(listener) {
163
+ listener();
164
+ });
165
+ };
166
+ _proto.isOnline = function isOnline() {
167
+ if (typeof this.online === "boolean") {
168
+ return this.online;
169
+ }
170
+ if (typeof navigator === "undefined" || typeof navigator.onLine === "undefined") {
171
+ return true;
172
+ }
173
+ return navigator.onLine;
174
+ };
175
+ return OnlineManager2;
176
+ })(Subscribable);
177
+ var onlineManager = new OnlineManager();
178
+ function defaultRetryDelay(failureCount) {
179
+ return Math.min(1e3 * Math.pow(2, failureCount), 3e4);
180
+ }
181
+ function isCancelable(value) {
182
+ return typeof (value == null ? void 0 : value.cancel) === "function";
183
+ }
184
+ var CancelledError = function CancelledError2(options) {
185
+ this.revert = options == null ? void 0 : options.revert;
186
+ this.silent = options == null ? void 0 : options.silent;
187
+ };
188
+ function isCancelledError(value) {
189
+ return value instanceof CancelledError;
190
+ }
191
+ var Retryer = function Retryer2(config) {
192
+ var _this = this;
193
+ var cancelRetry = false;
194
+ var cancelFn;
195
+ var continueFn;
196
+ var promiseResolve;
197
+ var promiseReject;
198
+ this.abort = config.abort;
199
+ this.cancel = function(cancelOptions) {
200
+ return cancelFn == null ? void 0 : cancelFn(cancelOptions);
201
+ };
202
+ this.cancelRetry = function() {
203
+ cancelRetry = true;
204
+ };
205
+ this.continueRetry = function() {
206
+ cancelRetry = false;
207
+ };
208
+ this.continue = function() {
209
+ return continueFn == null ? void 0 : continueFn();
210
+ };
211
+ this.failureCount = 0;
212
+ this.isPaused = false;
213
+ this.isResolved = false;
214
+ this.isTransportCancelable = false;
215
+ this.promise = new Promise(function(outerResolve, outerReject) {
216
+ promiseResolve = outerResolve;
217
+ promiseReject = outerReject;
218
+ });
219
+ var resolve = function resolve2(value) {
220
+ if (!_this.isResolved) {
221
+ _this.isResolved = true;
222
+ config.onSuccess == null ? void 0 : config.onSuccess(value);
223
+ continueFn == null ? void 0 : continueFn();
224
+ promiseResolve(value);
225
+ }
226
+ };
227
+ var reject = function reject2(value) {
228
+ if (!_this.isResolved) {
229
+ _this.isResolved = true;
230
+ config.onError == null ? void 0 : config.onError(value);
231
+ continueFn == null ? void 0 : continueFn();
232
+ promiseReject(value);
233
+ }
234
+ };
235
+ var pause = function pause2() {
236
+ return new Promise(function(continueResolve) {
237
+ continueFn = continueResolve;
238
+ _this.isPaused = true;
239
+ config.onPause == null ? void 0 : config.onPause();
240
+ }).then(function() {
241
+ continueFn = void 0;
242
+ _this.isPaused = false;
243
+ config.onContinue == null ? void 0 : config.onContinue();
244
+ });
245
+ };
246
+ var run = function run2() {
247
+ if (_this.isResolved) {
248
+ return;
249
+ }
250
+ var promiseOrValue;
251
+ try {
252
+ promiseOrValue = config.fn();
253
+ } catch (error) {
254
+ promiseOrValue = Promise.reject(error);
255
+ }
256
+ cancelFn = function cancelFn2(cancelOptions) {
257
+ if (!_this.isResolved) {
258
+ reject(new CancelledError(cancelOptions));
259
+ _this.abort == null ? void 0 : _this.abort();
260
+ if (isCancelable(promiseOrValue)) {
261
+ try {
262
+ promiseOrValue.cancel();
263
+ } catch (_unused) {
264
+ }
265
+ }
266
+ }
267
+ };
268
+ _this.isTransportCancelable = isCancelable(promiseOrValue);
269
+ Promise.resolve(promiseOrValue).then(resolve).catch(function(error) {
270
+ var _config$retry, _config$retryDelay;
271
+ if (_this.isResolved) {
272
+ return;
273
+ }
274
+ var retry = (_config$retry = config.retry) != null ? _config$retry : 3;
275
+ var retryDelay = (_config$retryDelay = config.retryDelay) != null ? _config$retryDelay : defaultRetryDelay;
276
+ var delay = typeof retryDelay === "function" ? retryDelay(_this.failureCount, error) : retryDelay;
277
+ var shouldRetry = retry === true || typeof retry === "number" && _this.failureCount < retry || typeof retry === "function" && retry(_this.failureCount, error);
278
+ if (cancelRetry || !shouldRetry) {
279
+ reject(error);
280
+ return;
281
+ }
282
+ _this.failureCount++;
283
+ config.onFail == null ? void 0 : config.onFail(_this.failureCount, error);
284
+ sleep(delay).then(function() {
285
+ if (!focusManager.isFocused() || !onlineManager.isOnline()) {
286
+ return pause();
287
+ }
288
+ }).then(function() {
289
+ if (cancelRetry) {
290
+ reject(error);
291
+ } else {
292
+ run2();
293
+ }
294
+ });
295
+ });
296
+ };
297
+ run();
298
+ };
299
+ var Query = /* @__PURE__ */ (function() {
300
+ function Query2(config) {
301
+ this.abortSignalConsumed = false;
302
+ this.hadObservers = false;
303
+ this.defaultOptions = config.defaultOptions;
304
+ this.setOptions(config.options);
305
+ this.observers = [];
306
+ this.cache = config.cache;
307
+ this.queryKey = config.queryKey;
308
+ this.queryHash = config.queryHash;
309
+ this.initialState = config.state || this.getDefaultState(this.options);
310
+ this.state = this.initialState;
311
+ this.meta = config.meta;
312
+ this.scheduleGc();
313
+ }
314
+ var _proto = Query2.prototype;
315
+ _proto.setOptions = function setOptions(options) {
316
+ var _this$options$cacheTi;
317
+ this.options = _extends({}, this.defaultOptions, options);
318
+ this.meta = options == null ? void 0 : options.meta;
319
+ this.cacheTime = Math.max(this.cacheTime || 0, (_this$options$cacheTi = this.options.cacheTime) != null ? _this$options$cacheTi : 5 * 60 * 1e3);
320
+ };
321
+ _proto.setDefaultOptions = function setDefaultOptions(options) {
322
+ this.defaultOptions = options;
323
+ };
324
+ _proto.scheduleGc = function scheduleGc() {
325
+ var _this = this;
326
+ this.clearGcTimeout();
327
+ if (isValidTimeout(this.cacheTime)) {
328
+ this.gcTimeout = setTimeout(function() {
329
+ _this.optionalRemove();
330
+ }, this.cacheTime);
331
+ }
332
+ };
333
+ _proto.clearGcTimeout = function clearGcTimeout() {
334
+ if (this.gcTimeout) {
335
+ clearTimeout(this.gcTimeout);
336
+ this.gcTimeout = void 0;
337
+ }
338
+ };
339
+ _proto.optionalRemove = function optionalRemove() {
340
+ if (!this.observers.length) {
341
+ if (this.state.isFetching) {
342
+ if (this.hadObservers) {
343
+ this.scheduleGc();
344
+ }
345
+ } else {
346
+ this.cache.remove(this);
347
+ }
348
+ }
349
+ };
350
+ _proto.setData = function setData(updater, options) {
351
+ var _this$options$isDataE, _this$options;
352
+ var prevData = this.state.data;
353
+ var data = functionalUpdate(updater, prevData);
354
+ if ((_this$options$isDataE = (_this$options = this.options).isDataEqual) == null ? void 0 : _this$options$isDataE.call(_this$options, prevData, data)) {
355
+ data = prevData;
356
+ } else if (this.options.structuralSharing !== false) {
357
+ data = replaceEqualDeep(prevData, data);
358
+ }
359
+ this.dispatch({
360
+ data,
361
+ type: "success",
362
+ dataUpdatedAt: options == null ? void 0 : options.updatedAt
363
+ });
364
+ return data;
365
+ };
366
+ _proto.setState = function setState(state, setStateOptions) {
367
+ this.dispatch({
368
+ type: "setState",
369
+ state,
370
+ setStateOptions
371
+ });
372
+ };
373
+ _proto.cancel = function cancel(options) {
374
+ var _this$retryer;
375
+ var promise = this.promise;
376
+ (_this$retryer = this.retryer) == null ? void 0 : _this$retryer.cancel(options);
377
+ return promise ? promise.then(noop).catch(noop) : Promise.resolve();
378
+ };
379
+ _proto.destroy = function destroy() {
380
+ this.clearGcTimeout();
381
+ this.cancel({
382
+ silent: true
383
+ });
384
+ };
385
+ _proto.reset = function reset() {
386
+ this.destroy();
387
+ this.setState(this.initialState);
388
+ };
389
+ _proto.isActive = function isActive() {
390
+ return this.observers.some(function(observer) {
391
+ return observer.options.enabled !== false;
392
+ });
393
+ };
394
+ _proto.isFetching = function isFetching() {
395
+ return this.state.isFetching;
396
+ };
397
+ _proto.isStale = function isStale2() {
398
+ return this.state.isInvalidated || !this.state.dataUpdatedAt || this.observers.some(function(observer) {
399
+ return observer.getCurrentResult().isStale;
400
+ });
401
+ };
402
+ _proto.isStaleByTime = function isStaleByTime(staleTime) {
403
+ if (staleTime === void 0) {
404
+ staleTime = 0;
405
+ }
406
+ return this.state.isInvalidated || !this.state.dataUpdatedAt || !timeUntilStale(this.state.dataUpdatedAt, staleTime);
407
+ };
408
+ _proto.onFocus = function onFocus() {
409
+ var _this$retryer2;
410
+ var observer = this.observers.find(function(x) {
411
+ return x.shouldFetchOnWindowFocus();
412
+ });
413
+ if (observer) {
414
+ observer.refetch();
415
+ }
416
+ (_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
417
+ };
418
+ _proto.onOnline = function onOnline() {
419
+ var _this$retryer3;
420
+ var observer = this.observers.find(function(x) {
421
+ return x.shouldFetchOnReconnect();
422
+ });
423
+ if (observer) {
424
+ observer.refetch();
425
+ }
426
+ (_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
427
+ };
428
+ _proto.addObserver = function addObserver(observer) {
429
+ if (this.observers.indexOf(observer) === -1) {
430
+ this.observers.push(observer);
431
+ this.hadObservers = true;
432
+ this.clearGcTimeout();
433
+ this.cache.notify({
434
+ type: "observerAdded",
435
+ query: this,
436
+ observer
437
+ });
438
+ }
439
+ };
440
+ _proto.removeObserver = function removeObserver(observer) {
441
+ if (this.observers.indexOf(observer) !== -1) {
442
+ this.observers = this.observers.filter(function(x) {
443
+ return x !== observer;
444
+ });
445
+ if (!this.observers.length) {
446
+ if (this.retryer) {
447
+ if (this.retryer.isTransportCancelable || this.abortSignalConsumed) {
448
+ this.retryer.cancel({
449
+ revert: true
450
+ });
451
+ } else {
452
+ this.retryer.cancelRetry();
453
+ }
454
+ }
455
+ if (this.cacheTime) {
456
+ this.scheduleGc();
457
+ } else {
458
+ this.cache.remove(this);
459
+ }
460
+ }
461
+ this.cache.notify({
462
+ type: "observerRemoved",
463
+ query: this,
464
+ observer
465
+ });
466
+ }
467
+ };
468
+ _proto.getObserversCount = function getObserversCount() {
469
+ return this.observers.length;
470
+ };
471
+ _proto.invalidate = function invalidate() {
472
+ if (!this.state.isInvalidated) {
473
+ this.dispatch({
474
+ type: "invalidate"
475
+ });
476
+ }
477
+ };
478
+ _proto.fetch = function fetch(options, fetchOptions) {
479
+ var _this2 = this, _this$options$behavio, _context$fetchOptions, _abortController$abor;
480
+ if (this.state.isFetching) {
481
+ if (this.state.dataUpdatedAt && (fetchOptions == null ? void 0 : fetchOptions.cancelRefetch)) {
482
+ this.cancel({
483
+ silent: true
484
+ });
485
+ } else if (this.promise) {
486
+ var _this$retryer4;
487
+ (_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry();
488
+ return this.promise;
489
+ }
490
+ }
491
+ if (options) {
492
+ this.setOptions(options);
493
+ }
494
+ if (!this.options.queryFn) {
495
+ var observer = this.observers.find(function(x) {
496
+ return x.options.queryFn;
497
+ });
498
+ if (observer) {
499
+ this.setOptions(observer.options);
500
+ }
501
+ }
502
+ var queryKey = ensureQueryKeyArray(this.queryKey);
503
+ var abortController = getAbortController();
504
+ var queryFnContext = {
505
+ queryKey,
506
+ pageParam: void 0,
507
+ meta: this.meta
508
+ };
509
+ Object.defineProperty(queryFnContext, "signal", {
510
+ enumerable: true,
511
+ get: function get() {
512
+ if (abortController) {
513
+ _this2.abortSignalConsumed = true;
514
+ return abortController.signal;
515
+ }
516
+ return void 0;
517
+ }
518
+ });
519
+ var fetchFn = function fetchFn2() {
520
+ if (!_this2.options.queryFn) {
521
+ return Promise.reject("Missing queryFn");
522
+ }
523
+ _this2.abortSignalConsumed = false;
524
+ return _this2.options.queryFn(queryFnContext);
525
+ };
526
+ var context = {
527
+ fetchOptions,
528
+ options: this.options,
529
+ queryKey,
530
+ state: this.state,
531
+ fetchFn,
532
+ meta: this.meta
533
+ };
534
+ if ((_this$options$behavio = this.options.behavior) == null ? void 0 : _this$options$behavio.onFetch) {
535
+ var _this$options$behavio2;
536
+ (_this$options$behavio2 = this.options.behavior) == null ? void 0 : _this$options$behavio2.onFetch(context);
537
+ }
538
+ this.revertState = this.state;
539
+ if (!this.state.isFetching || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
540
+ var _context$fetchOptions2;
541
+ this.dispatch({
542
+ type: "fetch",
543
+ meta: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.meta
544
+ });
545
+ }
546
+ this.retryer = new Retryer({
547
+ fn: context.fetchFn,
548
+ abort: abortController == null ? void 0 : (_abortController$abor = abortController.abort) == null ? void 0 : _abortController$abor.bind(abortController),
549
+ onSuccess: function onSuccess(data) {
550
+ _this2.setData(data);
551
+ _this2.cache.config.onSuccess == null ? void 0 : _this2.cache.config.onSuccess(data, _this2);
552
+ if (_this2.cacheTime === 0) {
553
+ _this2.optionalRemove();
554
+ }
555
+ },
556
+ onError: function onError(error) {
557
+ if (!(isCancelledError(error) && error.silent)) {
558
+ _this2.dispatch({
559
+ type: "error",
560
+ error
561
+ });
562
+ }
563
+ if (!isCancelledError(error)) {
564
+ _this2.cache.config.onError == null ? void 0 : _this2.cache.config.onError(error, _this2);
565
+ getLogger().error(error);
566
+ }
567
+ if (_this2.cacheTime === 0) {
568
+ _this2.optionalRemove();
569
+ }
570
+ },
571
+ onFail: function onFail() {
572
+ _this2.dispatch({
573
+ type: "failed"
574
+ });
575
+ },
576
+ onPause: function onPause() {
577
+ _this2.dispatch({
578
+ type: "pause"
579
+ });
580
+ },
581
+ onContinue: function onContinue() {
582
+ _this2.dispatch({
583
+ type: "continue"
584
+ });
585
+ },
586
+ retry: context.options.retry,
587
+ retryDelay: context.options.retryDelay
588
+ });
589
+ this.promise = this.retryer.promise;
590
+ return this.promise;
591
+ };
592
+ _proto.dispatch = function dispatch(action) {
593
+ var _this3 = this;
594
+ this.state = this.reducer(this.state, action);
595
+ notifyManager.batch(function() {
596
+ _this3.observers.forEach(function(observer) {
597
+ observer.onQueryUpdate(action);
598
+ });
599
+ _this3.cache.notify({
600
+ query: _this3,
601
+ type: "queryUpdated",
602
+ action
603
+ });
604
+ });
605
+ };
606
+ _proto.getDefaultState = function getDefaultState2(options) {
607
+ var data = typeof options.initialData === "function" ? options.initialData() : options.initialData;
608
+ var hasInitialData = typeof options.initialData !== "undefined";
609
+ var initialDataUpdatedAt = hasInitialData ? typeof options.initialDataUpdatedAt === "function" ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
610
+ var hasData = typeof data !== "undefined";
611
+ return {
612
+ data,
613
+ dataUpdateCount: 0,
614
+ dataUpdatedAt: hasData ? initialDataUpdatedAt != null ? initialDataUpdatedAt : Date.now() : 0,
615
+ error: null,
616
+ errorUpdateCount: 0,
617
+ errorUpdatedAt: 0,
618
+ fetchFailureCount: 0,
619
+ fetchMeta: null,
620
+ isFetching: false,
621
+ isInvalidated: false,
622
+ isPaused: false,
623
+ status: hasData ? "success" : "idle"
624
+ };
625
+ };
626
+ _proto.reducer = function reducer2(state, action) {
627
+ var _action$meta, _action$dataUpdatedAt;
628
+ switch (action.type) {
629
+ case "failed":
630
+ return _extends({}, state, {
631
+ fetchFailureCount: state.fetchFailureCount + 1
632
+ });
633
+ case "pause":
634
+ return _extends({}, state, {
635
+ isPaused: true
636
+ });
637
+ case "continue":
638
+ return _extends({}, state, {
639
+ isPaused: false
640
+ });
641
+ case "fetch":
642
+ return _extends({}, state, {
643
+ fetchFailureCount: 0,
644
+ fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
645
+ isFetching: true,
646
+ isPaused: false
647
+ }, !state.dataUpdatedAt && {
648
+ error: null,
649
+ status: "loading"
650
+ });
651
+ case "success":
652
+ return _extends({}, state, {
653
+ data: action.data,
654
+ dataUpdateCount: state.dataUpdateCount + 1,
655
+ dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
656
+ error: null,
657
+ fetchFailureCount: 0,
658
+ isFetching: false,
659
+ isInvalidated: false,
660
+ isPaused: false,
661
+ status: "success"
662
+ });
663
+ case "error":
664
+ var error = action.error;
665
+ if (isCancelledError(error) && error.revert && this.revertState) {
666
+ return _extends({}, this.revertState);
667
+ }
668
+ return _extends({}, state, {
669
+ error,
670
+ errorUpdateCount: state.errorUpdateCount + 1,
671
+ errorUpdatedAt: Date.now(),
672
+ fetchFailureCount: state.fetchFailureCount + 1,
673
+ isFetching: false,
674
+ isPaused: false,
675
+ status: "error"
676
+ });
677
+ case "invalidate":
678
+ return _extends({}, state, {
679
+ isInvalidated: true
680
+ });
681
+ case "setState":
682
+ return _extends({}, state, action.state);
683
+ default:
684
+ return state;
685
+ }
686
+ };
687
+ return Query2;
688
+ })();
689
+ var QueryCache = /* @__PURE__ */ (function(_Subscribable) {
690
+ _inheritsLoose(QueryCache2, _Subscribable);
691
+ function QueryCache2(config) {
692
+ var _this;
693
+ _this = _Subscribable.call(this) || this;
694
+ _this.config = config || {};
695
+ _this.queries = [];
696
+ _this.queriesMap = {};
697
+ return _this;
698
+ }
699
+ var _proto = QueryCache2.prototype;
700
+ _proto.build = function build(client, options, state) {
701
+ var _options$queryHash;
702
+ var queryKey = options.queryKey;
703
+ var queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : hashQueryKeyByOptions(queryKey, options);
704
+ var query = this.get(queryHash);
705
+ if (!query) {
706
+ query = new Query({
707
+ cache: this,
708
+ queryKey,
709
+ queryHash,
710
+ options: client.defaultQueryOptions(options),
711
+ state,
712
+ defaultOptions: client.getQueryDefaults(queryKey),
713
+ meta: options.meta
714
+ });
715
+ this.add(query);
716
+ }
717
+ return query;
718
+ };
719
+ _proto.add = function add(query) {
720
+ if (!this.queriesMap[query.queryHash]) {
721
+ this.queriesMap[query.queryHash] = query;
722
+ this.queries.push(query);
723
+ this.notify({
724
+ type: "queryAdded",
725
+ query
726
+ });
727
+ }
728
+ };
729
+ _proto.remove = function remove(query) {
730
+ var queryInMap = this.queriesMap[query.queryHash];
731
+ if (queryInMap) {
732
+ query.destroy();
733
+ this.queries = this.queries.filter(function(x) {
734
+ return x !== query;
735
+ });
736
+ if (queryInMap === query) {
737
+ delete this.queriesMap[query.queryHash];
738
+ }
739
+ this.notify({
740
+ type: "queryRemoved",
741
+ query
742
+ });
743
+ }
744
+ };
745
+ _proto.clear = function clear() {
746
+ var _this2 = this;
747
+ notifyManager.batch(function() {
748
+ _this2.queries.forEach(function(query) {
749
+ _this2.remove(query);
750
+ });
751
+ });
752
+ };
753
+ _proto.get = function get(queryHash) {
754
+ return this.queriesMap[queryHash];
755
+ };
756
+ _proto.getAll = function getAll() {
757
+ return this.queries;
758
+ };
759
+ _proto.find = function find(arg1, arg2) {
760
+ var _parseFilterArgs = parseFilterArgs(arg1, arg2), filters = _parseFilterArgs[0];
761
+ if (typeof filters.exact === "undefined") {
762
+ filters.exact = true;
763
+ }
764
+ return this.queries.find(function(query) {
765
+ return matchQuery(filters, query);
766
+ });
767
+ };
768
+ _proto.findAll = function findAll(arg1, arg2) {
769
+ var _parseFilterArgs2 = parseFilterArgs(arg1, arg2), filters = _parseFilterArgs2[0];
770
+ return Object.keys(filters).length > 0 ? this.queries.filter(function(query) {
771
+ return matchQuery(filters, query);
772
+ }) : this.queries;
773
+ };
774
+ _proto.notify = function notify(event) {
775
+ var _this3 = this;
776
+ notifyManager.batch(function() {
777
+ _this3.listeners.forEach(function(listener) {
778
+ listener(event);
779
+ });
780
+ });
781
+ };
782
+ _proto.onFocus = function onFocus() {
783
+ var _this4 = this;
784
+ notifyManager.batch(function() {
785
+ _this4.queries.forEach(function(query) {
786
+ query.onFocus();
787
+ });
788
+ });
789
+ };
790
+ _proto.onOnline = function onOnline() {
791
+ var _this5 = this;
792
+ notifyManager.batch(function() {
793
+ _this5.queries.forEach(function(query) {
794
+ query.onOnline();
795
+ });
796
+ });
797
+ };
798
+ return QueryCache2;
799
+ })(Subscribable);
800
+ var Mutation = /* @__PURE__ */ (function() {
801
+ function Mutation2(config) {
802
+ this.options = _extends({}, config.defaultOptions, config.options);
803
+ this.mutationId = config.mutationId;
804
+ this.mutationCache = config.mutationCache;
805
+ this.observers = [];
806
+ this.state = config.state || getDefaultState();
807
+ this.meta = config.meta;
808
+ }
809
+ var _proto = Mutation2.prototype;
810
+ _proto.setState = function setState(state) {
811
+ this.dispatch({
812
+ type: "setState",
813
+ state
814
+ });
815
+ };
816
+ _proto.addObserver = function addObserver(observer) {
817
+ if (this.observers.indexOf(observer) === -1) {
818
+ this.observers.push(observer);
819
+ }
820
+ };
821
+ _proto.removeObserver = function removeObserver(observer) {
822
+ this.observers = this.observers.filter(function(x) {
823
+ return x !== observer;
824
+ });
825
+ };
826
+ _proto.cancel = function cancel() {
827
+ if (this.retryer) {
828
+ this.retryer.cancel();
829
+ return this.retryer.promise.then(noop).catch(noop);
830
+ }
831
+ return Promise.resolve();
832
+ };
833
+ _proto.continue = function _continue() {
834
+ if (this.retryer) {
835
+ this.retryer.continue();
836
+ return this.retryer.promise;
837
+ }
838
+ return this.execute();
839
+ };
840
+ _proto.execute = function execute() {
841
+ var _this = this;
842
+ var data;
843
+ var restored = this.state.status === "loading";
844
+ var promise = Promise.resolve();
845
+ if (!restored) {
846
+ this.dispatch({
847
+ type: "loading",
848
+ variables: this.options.variables
849
+ });
850
+ promise = promise.then(function() {
851
+ _this.mutationCache.config.onMutate == null ? void 0 : _this.mutationCache.config.onMutate(_this.state.variables, _this);
852
+ }).then(function() {
853
+ return _this.options.onMutate == null ? void 0 : _this.options.onMutate(_this.state.variables);
854
+ }).then(function(context) {
855
+ if (context !== _this.state.context) {
856
+ _this.dispatch({
857
+ type: "loading",
858
+ context,
859
+ variables: _this.state.variables
860
+ });
861
+ }
862
+ });
863
+ }
864
+ return promise.then(function() {
865
+ return _this.executeMutation();
866
+ }).then(function(result) {
867
+ data = result;
868
+ _this.mutationCache.config.onSuccess == null ? void 0 : _this.mutationCache.config.onSuccess(data, _this.state.variables, _this.state.context, _this);
869
+ }).then(function() {
870
+ return _this.options.onSuccess == null ? void 0 : _this.options.onSuccess(data, _this.state.variables, _this.state.context);
871
+ }).then(function() {
872
+ return _this.options.onSettled == null ? void 0 : _this.options.onSettled(data, null, _this.state.variables, _this.state.context);
873
+ }).then(function() {
874
+ _this.dispatch({
875
+ type: "success",
876
+ data
877
+ });
878
+ return data;
879
+ }).catch(function(error) {
880
+ _this.mutationCache.config.onError == null ? void 0 : _this.mutationCache.config.onError(error, _this.state.variables, _this.state.context, _this);
881
+ getLogger().error(error);
882
+ return Promise.resolve().then(function() {
883
+ return _this.options.onError == null ? void 0 : _this.options.onError(error, _this.state.variables, _this.state.context);
884
+ }).then(function() {
885
+ return _this.options.onSettled == null ? void 0 : _this.options.onSettled(void 0, error, _this.state.variables, _this.state.context);
886
+ }).then(function() {
887
+ _this.dispatch({
888
+ type: "error",
889
+ error
890
+ });
891
+ throw error;
892
+ });
893
+ });
894
+ };
895
+ _proto.executeMutation = function executeMutation() {
896
+ var _this2 = this, _this$options$retry;
897
+ this.retryer = new Retryer({
898
+ fn: function fn() {
899
+ if (!_this2.options.mutationFn) {
900
+ return Promise.reject("No mutationFn found");
901
+ }
902
+ return _this2.options.mutationFn(_this2.state.variables);
903
+ },
904
+ onFail: function onFail() {
905
+ _this2.dispatch({
906
+ type: "failed"
907
+ });
908
+ },
909
+ onPause: function onPause() {
910
+ _this2.dispatch({
911
+ type: "pause"
912
+ });
913
+ },
914
+ onContinue: function onContinue() {
915
+ _this2.dispatch({
916
+ type: "continue"
917
+ });
918
+ },
919
+ retry: (_this$options$retry = this.options.retry) != null ? _this$options$retry : 0,
920
+ retryDelay: this.options.retryDelay
921
+ });
922
+ return this.retryer.promise;
923
+ };
924
+ _proto.dispatch = function dispatch(action) {
925
+ var _this3 = this;
926
+ this.state = reducer(this.state, action);
927
+ notifyManager.batch(function() {
928
+ _this3.observers.forEach(function(observer) {
929
+ observer.onMutationUpdate(action);
930
+ });
931
+ _this3.mutationCache.notify(_this3);
932
+ });
933
+ };
934
+ return Mutation2;
935
+ })();
936
+ function getDefaultState() {
937
+ return {
938
+ context: void 0,
939
+ data: void 0,
940
+ error: null,
941
+ failureCount: 0,
942
+ isPaused: false,
943
+ status: "idle",
944
+ variables: void 0
945
+ };
946
+ }
947
+ function reducer(state, action) {
948
+ switch (action.type) {
949
+ case "failed":
950
+ return _extends({}, state, {
951
+ failureCount: state.failureCount + 1
952
+ });
953
+ case "pause":
954
+ return _extends({}, state, {
955
+ isPaused: true
956
+ });
957
+ case "continue":
958
+ return _extends({}, state, {
959
+ isPaused: false
960
+ });
961
+ case "loading":
962
+ return _extends({}, state, {
963
+ context: action.context,
964
+ data: void 0,
965
+ error: null,
966
+ isPaused: false,
967
+ status: "loading",
968
+ variables: action.variables
969
+ });
970
+ case "success":
971
+ return _extends({}, state, {
972
+ data: action.data,
973
+ error: null,
974
+ status: "success",
975
+ isPaused: false
976
+ });
977
+ case "error":
978
+ return _extends({}, state, {
979
+ data: void 0,
980
+ error: action.error,
981
+ failureCount: state.failureCount + 1,
982
+ isPaused: false,
983
+ status: "error"
984
+ });
985
+ case "setState":
986
+ return _extends({}, state, action.state);
987
+ default:
988
+ return state;
989
+ }
990
+ }
991
+ var MutationCache = /* @__PURE__ */ (function(_Subscribable) {
992
+ _inheritsLoose(MutationCache2, _Subscribable);
993
+ function MutationCache2(config) {
994
+ var _this;
995
+ _this = _Subscribable.call(this) || this;
996
+ _this.config = config || {};
997
+ _this.mutations = [];
998
+ _this.mutationId = 0;
999
+ return _this;
1000
+ }
1001
+ var _proto = MutationCache2.prototype;
1002
+ _proto.build = function build(client, options, state) {
1003
+ var mutation = new Mutation({
1004
+ mutationCache: this,
1005
+ mutationId: ++this.mutationId,
1006
+ options: client.defaultMutationOptions(options),
1007
+ state,
1008
+ defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : void 0,
1009
+ meta: options.meta
1010
+ });
1011
+ this.add(mutation);
1012
+ return mutation;
1013
+ };
1014
+ _proto.add = function add(mutation) {
1015
+ this.mutations.push(mutation);
1016
+ this.notify(mutation);
1017
+ };
1018
+ _proto.remove = function remove(mutation) {
1019
+ this.mutations = this.mutations.filter(function(x) {
1020
+ return x !== mutation;
1021
+ });
1022
+ mutation.cancel();
1023
+ this.notify(mutation);
1024
+ };
1025
+ _proto.clear = function clear() {
1026
+ var _this2 = this;
1027
+ notifyManager.batch(function() {
1028
+ _this2.mutations.forEach(function(mutation) {
1029
+ _this2.remove(mutation);
1030
+ });
1031
+ });
1032
+ };
1033
+ _proto.getAll = function getAll() {
1034
+ return this.mutations;
1035
+ };
1036
+ _proto.find = function find(filters) {
1037
+ if (typeof filters.exact === "undefined") {
1038
+ filters.exact = true;
1039
+ }
1040
+ return this.mutations.find(function(mutation) {
1041
+ return matchMutation(filters, mutation);
1042
+ });
1043
+ };
1044
+ _proto.findAll = function findAll(filters) {
1045
+ return this.mutations.filter(function(mutation) {
1046
+ return matchMutation(filters, mutation);
1047
+ });
1048
+ };
1049
+ _proto.notify = function notify(mutation) {
1050
+ var _this3 = this;
1051
+ notifyManager.batch(function() {
1052
+ _this3.listeners.forEach(function(listener) {
1053
+ listener(mutation);
1054
+ });
1055
+ });
1056
+ };
1057
+ _proto.onFocus = function onFocus() {
1058
+ this.resumePausedMutations();
1059
+ };
1060
+ _proto.onOnline = function onOnline() {
1061
+ this.resumePausedMutations();
1062
+ };
1063
+ _proto.resumePausedMutations = function resumePausedMutations() {
1064
+ var pausedMutations = this.mutations.filter(function(x) {
1065
+ return x.state.isPaused;
1066
+ });
1067
+ return notifyManager.batch(function() {
1068
+ return pausedMutations.reduce(function(promise, mutation) {
1069
+ return promise.then(function() {
1070
+ return mutation.continue().catch(noop);
1071
+ });
1072
+ }, Promise.resolve());
1073
+ });
1074
+ };
1075
+ return MutationCache2;
1076
+ })(Subscribable);
1077
+ function infiniteQueryBehavior() {
1078
+ return {
1079
+ onFetch: function onFetch(context) {
1080
+ context.fetchFn = function() {
1081
+ var _context$fetchOptions, _context$fetchOptions2, _context$fetchOptions3, _context$fetchOptions4, _context$state$data, _context$state$data2;
1082
+ var refetchPage = (_context$fetchOptions = context.fetchOptions) == null ? void 0 : (_context$fetchOptions2 = _context$fetchOptions.meta) == null ? void 0 : _context$fetchOptions2.refetchPage;
1083
+ var fetchMore = (_context$fetchOptions3 = context.fetchOptions) == null ? void 0 : (_context$fetchOptions4 = _context$fetchOptions3.meta) == null ? void 0 : _context$fetchOptions4.fetchMore;
1084
+ var pageParam = fetchMore == null ? void 0 : fetchMore.pageParam;
1085
+ var isFetchingNextPage = (fetchMore == null ? void 0 : fetchMore.direction) === "forward";
1086
+ var isFetchingPreviousPage = (fetchMore == null ? void 0 : fetchMore.direction) === "backward";
1087
+ var oldPages = ((_context$state$data = context.state.data) == null ? void 0 : _context$state$data.pages) || [];
1088
+ var oldPageParams = ((_context$state$data2 = context.state.data) == null ? void 0 : _context$state$data2.pageParams) || [];
1089
+ var abortController = getAbortController();
1090
+ var abortSignal = abortController == null ? void 0 : abortController.signal;
1091
+ var newPageParams = oldPageParams;
1092
+ var cancelled = false;
1093
+ var queryFn = context.options.queryFn || function() {
1094
+ return Promise.reject("Missing queryFn");
1095
+ };
1096
+ var buildNewPages = function buildNewPages2(pages, param2, page, previous) {
1097
+ newPageParams = previous ? [param2].concat(newPageParams) : [].concat(newPageParams, [param2]);
1098
+ return previous ? [page].concat(pages) : [].concat(pages, [page]);
1099
+ };
1100
+ var fetchPage = function fetchPage2(pages, manual2, param2, previous) {
1101
+ if (cancelled) {
1102
+ return Promise.reject("Cancelled");
1103
+ }
1104
+ if (typeof param2 === "undefined" && !manual2 && pages.length) {
1105
+ return Promise.resolve(pages);
1106
+ }
1107
+ var queryFnContext = {
1108
+ queryKey: context.queryKey,
1109
+ signal: abortSignal,
1110
+ pageParam: param2,
1111
+ meta: context.meta
1112
+ };
1113
+ var queryFnResult = queryFn(queryFnContext);
1114
+ var promise2 = Promise.resolve(queryFnResult).then(function(page) {
1115
+ return buildNewPages(pages, param2, page, previous);
1116
+ });
1117
+ if (isCancelable(queryFnResult)) {
1118
+ var promiseAsAny = promise2;
1119
+ promiseAsAny.cancel = queryFnResult.cancel;
1120
+ }
1121
+ return promise2;
1122
+ };
1123
+ var promise;
1124
+ if (!oldPages.length) {
1125
+ promise = fetchPage([]);
1126
+ } else if (isFetchingNextPage) {
1127
+ var manual = typeof pageParam !== "undefined";
1128
+ var param = manual ? pageParam : getNextPageParam(context.options, oldPages);
1129
+ promise = fetchPage(oldPages, manual, param);
1130
+ } else if (isFetchingPreviousPage) {
1131
+ var _manual = typeof pageParam !== "undefined";
1132
+ var _param = _manual ? pageParam : getPreviousPageParam(context.options, oldPages);
1133
+ promise = fetchPage(oldPages, _manual, _param, true);
1134
+ } else {
1135
+ (function() {
1136
+ newPageParams = [];
1137
+ var manual2 = typeof context.options.getNextPageParam === "undefined";
1138
+ var shouldFetchFirstPage = refetchPage && oldPages[0] ? refetchPage(oldPages[0], 0, oldPages) : true;
1139
+ promise = shouldFetchFirstPage ? fetchPage([], manual2, oldPageParams[0]) : Promise.resolve(buildNewPages([], oldPageParams[0], oldPages[0]));
1140
+ var _loop = function _loop2(i2) {
1141
+ promise = promise.then(function(pages) {
1142
+ var shouldFetchNextPage = refetchPage && oldPages[i2] ? refetchPage(oldPages[i2], i2, oldPages) : true;
1143
+ if (shouldFetchNextPage) {
1144
+ var _param2 = manual2 ? oldPageParams[i2] : getNextPageParam(context.options, pages);
1145
+ return fetchPage(pages, manual2, _param2);
1146
+ }
1147
+ return Promise.resolve(buildNewPages(pages, oldPageParams[i2], oldPages[i2]));
1148
+ });
1149
+ };
1150
+ for (var i = 1; i < oldPages.length; i++) {
1151
+ _loop(i);
1152
+ }
1153
+ })();
1154
+ }
1155
+ var finalPromise = promise.then(function(pages) {
1156
+ return {
1157
+ pages,
1158
+ pageParams: newPageParams
1159
+ };
1160
+ });
1161
+ var finalPromiseAsAny = finalPromise;
1162
+ finalPromiseAsAny.cancel = function() {
1163
+ cancelled = true;
1164
+ abortController == null ? void 0 : abortController.abort();
1165
+ if (isCancelable(promise)) {
1166
+ promise.cancel();
1167
+ }
1168
+ };
1169
+ return finalPromise;
1170
+ };
1171
+ }
1172
+ };
1173
+ }
1174
+ function getNextPageParam(options, pages) {
1175
+ return options.getNextPageParam == null ? void 0 : options.getNextPageParam(pages[pages.length - 1], pages);
1176
+ }
1177
+ function getPreviousPageParam(options, pages) {
1178
+ return options.getPreviousPageParam == null ? void 0 : options.getPreviousPageParam(pages[0], pages);
1179
+ }
1180
+ function hasNextPage(options, pages) {
1181
+ if (options.getNextPageParam && Array.isArray(pages)) {
1182
+ var nextPageParam = getNextPageParam(options, pages);
1183
+ return typeof nextPageParam !== "undefined" && nextPageParam !== null && nextPageParam !== false;
1184
+ }
1185
+ }
1186
+ function hasPreviousPage(options, pages) {
1187
+ if (options.getPreviousPageParam && Array.isArray(pages)) {
1188
+ var previousPageParam = getPreviousPageParam(options, pages);
1189
+ return typeof previousPageParam !== "undefined" && previousPageParam !== null && previousPageParam !== false;
1190
+ }
1191
+ }
1192
+ var QueryClient = /* @__PURE__ */ (function() {
1193
+ function QueryClient2(config) {
1194
+ if (config === void 0) {
1195
+ config = {};
1196
+ }
1197
+ this.queryCache = config.queryCache || new QueryCache();
1198
+ this.mutationCache = config.mutationCache || new MutationCache();
1199
+ this.defaultOptions = config.defaultOptions || {};
1200
+ this.queryDefaults = [];
1201
+ this.mutationDefaults = [];
1202
+ }
1203
+ var _proto = QueryClient2.prototype;
1204
+ _proto.mount = function mount() {
1205
+ var _this = this;
1206
+ this.unsubscribeFocus = focusManager.subscribe(function() {
1207
+ if (focusManager.isFocused() && onlineManager.isOnline()) {
1208
+ _this.mutationCache.onFocus();
1209
+ _this.queryCache.onFocus();
1210
+ }
1211
+ });
1212
+ this.unsubscribeOnline = onlineManager.subscribe(function() {
1213
+ if (focusManager.isFocused() && onlineManager.isOnline()) {
1214
+ _this.mutationCache.onOnline();
1215
+ _this.queryCache.onOnline();
1216
+ }
1217
+ });
1218
+ };
1219
+ _proto.unmount = function unmount() {
1220
+ var _this$unsubscribeFocu, _this$unsubscribeOnli;
1221
+ (_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this);
1222
+ (_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this);
1223
+ };
1224
+ _proto.isFetching = function isFetching(arg1, arg2) {
1225
+ var _parseFilterArgs = parseFilterArgs(arg1, arg2), filters = _parseFilterArgs[0];
1226
+ filters.fetching = true;
1227
+ return this.queryCache.findAll(filters).length;
1228
+ };
1229
+ _proto.isMutating = function isMutating(filters) {
1230
+ return this.mutationCache.findAll(_extends({}, filters, {
1231
+ fetching: true
1232
+ })).length;
1233
+ };
1234
+ _proto.getQueryData = function getQueryData(queryKey, filters) {
1235
+ var _this$queryCache$find;
1236
+ return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data;
1237
+ };
1238
+ _proto.getQueriesData = function getQueriesData(queryKeyOrFilters) {
1239
+ return this.getQueryCache().findAll(queryKeyOrFilters).map(function(_ref) {
1240
+ var queryKey = _ref.queryKey, state = _ref.state;
1241
+ var data = state.data;
1242
+ return [queryKey, data];
1243
+ });
1244
+ };
1245
+ _proto.setQueryData = function setQueryData(queryKey, updater, options) {
1246
+ var parsedOptions = parseQueryArgs(queryKey);
1247
+ var defaultedOptions = this.defaultQueryOptions(parsedOptions);
1248
+ return this.queryCache.build(this, defaultedOptions).setData(updater, options);
1249
+ };
1250
+ _proto.setQueriesData = function setQueriesData(queryKeyOrFilters, updater, options) {
1251
+ var _this2 = this;
1252
+ return notifyManager.batch(function() {
1253
+ return _this2.getQueryCache().findAll(queryKeyOrFilters).map(function(_ref2) {
1254
+ var queryKey = _ref2.queryKey;
1255
+ return [queryKey, _this2.setQueryData(queryKey, updater, options)];
1256
+ });
1257
+ });
1258
+ };
1259
+ _proto.getQueryState = function getQueryState(queryKey, filters) {
1260
+ var _this$queryCache$find2;
1261
+ return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state;
1262
+ };
1263
+ _proto.removeQueries = function removeQueries(arg1, arg2) {
1264
+ var _parseFilterArgs2 = parseFilterArgs(arg1, arg2), filters = _parseFilterArgs2[0];
1265
+ var queryCache = this.queryCache;
1266
+ notifyManager.batch(function() {
1267
+ queryCache.findAll(filters).forEach(function(query) {
1268
+ queryCache.remove(query);
1269
+ });
1270
+ });
1271
+ };
1272
+ _proto.resetQueries = function resetQueries(arg1, arg2, arg3) {
1273
+ var _this3 = this;
1274
+ var _parseFilterArgs3 = parseFilterArgs(arg1, arg2, arg3), filters = _parseFilterArgs3[0], options = _parseFilterArgs3[1];
1275
+ var queryCache = this.queryCache;
1276
+ var refetchFilters = _extends({}, filters, {
1277
+ active: true
1278
+ });
1279
+ return notifyManager.batch(function() {
1280
+ queryCache.findAll(filters).forEach(function(query) {
1281
+ query.reset();
1282
+ });
1283
+ return _this3.refetchQueries(refetchFilters, options);
1284
+ });
1285
+ };
1286
+ _proto.cancelQueries = function cancelQueries(arg1, arg2, arg3) {
1287
+ var _this4 = this;
1288
+ var _parseFilterArgs4 = parseFilterArgs(arg1, arg2, arg3), filters = _parseFilterArgs4[0], _parseFilterArgs4$ = _parseFilterArgs4[1], cancelOptions = _parseFilterArgs4$ === void 0 ? {} : _parseFilterArgs4$;
1289
+ if (typeof cancelOptions.revert === "undefined") {
1290
+ cancelOptions.revert = true;
1291
+ }
1292
+ var promises = notifyManager.batch(function() {
1293
+ return _this4.queryCache.findAll(filters).map(function(query) {
1294
+ return query.cancel(cancelOptions);
1295
+ });
1296
+ });
1297
+ return Promise.all(promises).then(noop).catch(noop);
1298
+ };
1299
+ _proto.invalidateQueries = function invalidateQueries(arg1, arg2, arg3) {
1300
+ var _ref3, _filters$refetchActiv, _filters$refetchInact, _this5 = this;
1301
+ var _parseFilterArgs5 = parseFilterArgs(arg1, arg2, arg3), filters = _parseFilterArgs5[0], options = _parseFilterArgs5[1];
1302
+ var refetchFilters = _extends({}, filters, {
1303
+ // if filters.refetchActive is not provided and filters.active is explicitly false,
1304
+ // e.g. invalidateQueries({ active: false }), we don't want to refetch active queries
1305
+ active: (_ref3 = (_filters$refetchActiv = filters.refetchActive) != null ? _filters$refetchActiv : filters.active) != null ? _ref3 : true,
1306
+ inactive: (_filters$refetchInact = filters.refetchInactive) != null ? _filters$refetchInact : false
1307
+ });
1308
+ return notifyManager.batch(function() {
1309
+ _this5.queryCache.findAll(filters).forEach(function(query) {
1310
+ query.invalidate();
1311
+ });
1312
+ return _this5.refetchQueries(refetchFilters, options);
1313
+ });
1314
+ };
1315
+ _proto.refetchQueries = function refetchQueries(arg1, arg2, arg3) {
1316
+ var _this6 = this;
1317
+ var _parseFilterArgs6 = parseFilterArgs(arg1, arg2, arg3), filters = _parseFilterArgs6[0], options = _parseFilterArgs6[1];
1318
+ var promises = notifyManager.batch(function() {
1319
+ return _this6.queryCache.findAll(filters).map(function(query) {
1320
+ return query.fetch(void 0, _extends({}, options, {
1321
+ meta: {
1322
+ refetchPage: filters == null ? void 0 : filters.refetchPage
1323
+ }
1324
+ }));
1325
+ });
1326
+ });
1327
+ var promise = Promise.all(promises).then(noop);
1328
+ if (!(options == null ? void 0 : options.throwOnError)) {
1329
+ promise = promise.catch(noop);
1330
+ }
1331
+ return promise;
1332
+ };
1333
+ _proto.fetchQuery = function fetchQuery(arg1, arg2, arg3) {
1334
+ var parsedOptions = parseQueryArgs(arg1, arg2, arg3);
1335
+ var defaultedOptions = this.defaultQueryOptions(parsedOptions);
1336
+ if (typeof defaultedOptions.retry === "undefined") {
1337
+ defaultedOptions.retry = false;
1338
+ }
1339
+ var query = this.queryCache.build(this, defaultedOptions);
1340
+ return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
1341
+ };
1342
+ _proto.prefetchQuery = function prefetchQuery(arg1, arg2, arg3) {
1343
+ return this.fetchQuery(arg1, arg2, arg3).then(noop).catch(noop);
1344
+ };
1345
+ _proto.fetchInfiniteQuery = function fetchInfiniteQuery(arg1, arg2, arg3) {
1346
+ var parsedOptions = parseQueryArgs(arg1, arg2, arg3);
1347
+ parsedOptions.behavior = infiniteQueryBehavior();
1348
+ return this.fetchQuery(parsedOptions);
1349
+ };
1350
+ _proto.prefetchInfiniteQuery = function prefetchInfiniteQuery(arg1, arg2, arg3) {
1351
+ return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop).catch(noop);
1352
+ };
1353
+ _proto.cancelMutations = function cancelMutations() {
1354
+ var _this7 = this;
1355
+ var promises = notifyManager.batch(function() {
1356
+ return _this7.mutationCache.getAll().map(function(mutation) {
1357
+ return mutation.cancel();
1358
+ });
1359
+ });
1360
+ return Promise.all(promises).then(noop).catch(noop);
1361
+ };
1362
+ _proto.resumePausedMutations = function resumePausedMutations() {
1363
+ return this.getMutationCache().resumePausedMutations();
1364
+ };
1365
+ _proto.executeMutation = function executeMutation(options) {
1366
+ return this.mutationCache.build(this, options).execute();
1367
+ };
1368
+ _proto.getQueryCache = function getQueryCache() {
1369
+ return this.queryCache;
1370
+ };
1371
+ _proto.getMutationCache = function getMutationCache() {
1372
+ return this.mutationCache;
1373
+ };
1374
+ _proto.getDefaultOptions = function getDefaultOptions() {
1375
+ return this.defaultOptions;
1376
+ };
1377
+ _proto.setDefaultOptions = function setDefaultOptions(options) {
1378
+ this.defaultOptions = options;
1379
+ };
1380
+ _proto.setQueryDefaults = function setQueryDefaults(queryKey, options) {
1381
+ var result = this.queryDefaults.find(function(x) {
1382
+ return hashQueryKey(queryKey) === hashQueryKey(x.queryKey);
1383
+ });
1384
+ if (result) {
1385
+ result.defaultOptions = options;
1386
+ } else {
1387
+ this.queryDefaults.push({
1388
+ queryKey,
1389
+ defaultOptions: options
1390
+ });
1391
+ }
1392
+ };
1393
+ _proto.getQueryDefaults = function getQueryDefaults(queryKey) {
1394
+ var _this$queryDefaults$f;
1395
+ return queryKey ? (_this$queryDefaults$f = this.queryDefaults.find(function(x) {
1396
+ return partialMatchKey(queryKey, x.queryKey);
1397
+ })) == null ? void 0 : _this$queryDefaults$f.defaultOptions : void 0;
1398
+ };
1399
+ _proto.setMutationDefaults = function setMutationDefaults(mutationKey, options) {
1400
+ var result = this.mutationDefaults.find(function(x) {
1401
+ return hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey);
1402
+ });
1403
+ if (result) {
1404
+ result.defaultOptions = options;
1405
+ } else {
1406
+ this.mutationDefaults.push({
1407
+ mutationKey,
1408
+ defaultOptions: options
1409
+ });
1410
+ }
1411
+ };
1412
+ _proto.getMutationDefaults = function getMutationDefaults(mutationKey) {
1413
+ var _this$mutationDefault;
1414
+ return mutationKey ? (_this$mutationDefault = this.mutationDefaults.find(function(x) {
1415
+ return partialMatchKey(mutationKey, x.mutationKey);
1416
+ })) == null ? void 0 : _this$mutationDefault.defaultOptions : void 0;
1417
+ };
1418
+ _proto.defaultQueryOptions = function defaultQueryOptions(options) {
1419
+ if (options == null ? void 0 : options._defaulted) {
1420
+ return options;
1421
+ }
1422
+ var defaultedOptions = _extends({}, this.defaultOptions.queries, this.getQueryDefaults(options == null ? void 0 : options.queryKey), options, {
1423
+ _defaulted: true
1424
+ });
1425
+ if (!defaultedOptions.queryHash && defaultedOptions.queryKey) {
1426
+ defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions);
1427
+ }
1428
+ return defaultedOptions;
1429
+ };
1430
+ _proto.defaultQueryObserverOptions = function defaultQueryObserverOptions(options) {
1431
+ return this.defaultQueryOptions(options);
1432
+ };
1433
+ _proto.defaultMutationOptions = function defaultMutationOptions(options) {
1434
+ if (options == null ? void 0 : options._defaulted) {
1435
+ return options;
1436
+ }
1437
+ return _extends({}, this.defaultOptions.mutations, this.getMutationDefaults(options == null ? void 0 : options.mutationKey), options, {
1438
+ _defaulted: true
1439
+ });
1440
+ };
1441
+ _proto.clear = function clear() {
1442
+ this.queryCache.clear();
1443
+ this.mutationCache.clear();
1444
+ };
1445
+ return QueryClient2;
1446
+ })();
1447
+ var QueryObserver = /* @__PURE__ */ (function(_Subscribable) {
1448
+ _inheritsLoose(QueryObserver2, _Subscribable);
1449
+ function QueryObserver2(client, options) {
1450
+ var _this;
1451
+ _this = _Subscribable.call(this) || this;
1452
+ _this.client = client;
1453
+ _this.options = options;
1454
+ _this.trackedProps = [];
1455
+ _this.selectError = null;
1456
+ _this.bindMethods();
1457
+ _this.setOptions(options);
1458
+ return _this;
1459
+ }
1460
+ var _proto = QueryObserver2.prototype;
1461
+ _proto.bindMethods = function bindMethods() {
1462
+ this.remove = this.remove.bind(this);
1463
+ this.refetch = this.refetch.bind(this);
1464
+ };
1465
+ _proto.onSubscribe = function onSubscribe() {
1466
+ if (this.listeners.length === 1) {
1467
+ this.currentQuery.addObserver(this);
1468
+ if (shouldFetchOnMount(this.currentQuery, this.options)) {
1469
+ this.executeFetch();
1470
+ }
1471
+ this.updateTimers();
1472
+ }
1473
+ };
1474
+ _proto.onUnsubscribe = function onUnsubscribe() {
1475
+ if (!this.listeners.length) {
1476
+ this.destroy();
1477
+ }
1478
+ };
1479
+ _proto.shouldFetchOnReconnect = function shouldFetchOnReconnect() {
1480
+ return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
1481
+ };
1482
+ _proto.shouldFetchOnWindowFocus = function shouldFetchOnWindowFocus() {
1483
+ return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
1484
+ };
1485
+ _proto.destroy = function destroy() {
1486
+ this.listeners = [];
1487
+ this.clearTimers();
1488
+ this.currentQuery.removeObserver(this);
1489
+ };
1490
+ _proto.setOptions = function setOptions(options, notifyOptions) {
1491
+ var prevOptions = this.options;
1492
+ var prevQuery = this.currentQuery;
1493
+ this.options = this.client.defaultQueryObserverOptions(options);
1494
+ if (typeof this.options.enabled !== "undefined" && typeof this.options.enabled !== "boolean") {
1495
+ throw new Error("Expected enabled to be a boolean");
1496
+ }
1497
+ if (!this.options.queryKey) {
1498
+ this.options.queryKey = prevOptions.queryKey;
1499
+ }
1500
+ this.updateQuery();
1501
+ var mounted = this.hasListeners();
1502
+ if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
1503
+ this.executeFetch();
1504
+ }
1505
+ this.updateResult(notifyOptions);
1506
+ if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
1507
+ this.updateStaleTimeout();
1508
+ }
1509
+ var nextRefetchInterval = this.computeRefetchInterval();
1510
+ if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
1511
+ this.updateRefetchInterval(nextRefetchInterval);
1512
+ }
1513
+ };
1514
+ _proto.getOptimisticResult = function getOptimisticResult(options) {
1515
+ var defaultedOptions = this.client.defaultQueryObserverOptions(options);
1516
+ var query = this.client.getQueryCache().build(this.client, defaultedOptions);
1517
+ return this.createResult(query, defaultedOptions);
1518
+ };
1519
+ _proto.getCurrentResult = function getCurrentResult() {
1520
+ return this.currentResult;
1521
+ };
1522
+ _proto.trackResult = function trackResult(result, defaultedOptions) {
1523
+ var _this2 = this;
1524
+ var trackedResult = {};
1525
+ var trackProp = function trackProp2(key) {
1526
+ if (!_this2.trackedProps.includes(key)) {
1527
+ _this2.trackedProps.push(key);
1528
+ }
1529
+ };
1530
+ Object.keys(result).forEach(function(key) {
1531
+ Object.defineProperty(trackedResult, key, {
1532
+ configurable: false,
1533
+ enumerable: true,
1534
+ get: function get() {
1535
+ trackProp(key);
1536
+ return result[key];
1537
+ }
1538
+ });
1539
+ });
1540
+ if (defaultedOptions.useErrorBoundary || defaultedOptions.suspense) {
1541
+ trackProp("error");
1542
+ }
1543
+ return trackedResult;
1544
+ };
1545
+ _proto.getNextResult = function getNextResult(options) {
1546
+ var _this3 = this;
1547
+ return new Promise(function(resolve, reject) {
1548
+ var unsubscribe = _this3.subscribe(function(result) {
1549
+ if (!result.isFetching) {
1550
+ unsubscribe();
1551
+ if (result.isError && (options == null ? void 0 : options.throwOnError)) {
1552
+ reject(result.error);
1553
+ } else {
1554
+ resolve(result);
1555
+ }
1556
+ }
1557
+ });
1558
+ });
1559
+ };
1560
+ _proto.getCurrentQuery = function getCurrentQuery() {
1561
+ return this.currentQuery;
1562
+ };
1563
+ _proto.remove = function remove() {
1564
+ this.client.getQueryCache().remove(this.currentQuery);
1565
+ };
1566
+ _proto.refetch = function refetch(options) {
1567
+ return this.fetch(_extends({}, options, {
1568
+ meta: {
1569
+ refetchPage: options == null ? void 0 : options.refetchPage
1570
+ }
1571
+ }));
1572
+ };
1573
+ _proto.fetchOptimistic = function fetchOptimistic(options) {
1574
+ var _this4 = this;
1575
+ var defaultedOptions = this.client.defaultQueryObserverOptions(options);
1576
+ var query = this.client.getQueryCache().build(this.client, defaultedOptions);
1577
+ return query.fetch().then(function() {
1578
+ return _this4.createResult(query, defaultedOptions);
1579
+ });
1580
+ };
1581
+ _proto.fetch = function fetch(fetchOptions) {
1582
+ var _this5 = this;
1583
+ return this.executeFetch(fetchOptions).then(function() {
1584
+ _this5.updateResult();
1585
+ return _this5.currentResult;
1586
+ });
1587
+ };
1588
+ _proto.executeFetch = function executeFetch(fetchOptions) {
1589
+ this.updateQuery();
1590
+ var promise = this.currentQuery.fetch(this.options, fetchOptions);
1591
+ if (!(fetchOptions == null ? void 0 : fetchOptions.throwOnError)) {
1592
+ promise = promise.catch(noop);
1593
+ }
1594
+ return promise;
1595
+ };
1596
+ _proto.updateStaleTimeout = function updateStaleTimeout() {
1597
+ var _this6 = this;
1598
+ this.clearStaleTimeout();
1599
+ if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
1600
+ return;
1601
+ }
1602
+ var time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime);
1603
+ var timeout = time + 1;
1604
+ this.staleTimeoutId = setTimeout(function() {
1605
+ if (!_this6.currentResult.isStale) {
1606
+ _this6.updateResult();
1607
+ }
1608
+ }, timeout);
1609
+ };
1610
+ _proto.computeRefetchInterval = function computeRefetchInterval() {
1611
+ var _this$options$refetch;
1612
+ return typeof this.options.refetchInterval === "function" ? this.options.refetchInterval(this.currentResult.data, this.currentQuery) : (_this$options$refetch = this.options.refetchInterval) != null ? _this$options$refetch : false;
1613
+ };
1614
+ _proto.updateRefetchInterval = function updateRefetchInterval(nextInterval) {
1615
+ var _this7 = this;
1616
+ this.clearRefetchInterval();
1617
+ this.currentRefetchInterval = nextInterval;
1618
+ if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
1619
+ return;
1620
+ }
1621
+ this.refetchIntervalId = setInterval(function() {
1622
+ if (_this7.options.refetchIntervalInBackground || focusManager.isFocused()) {
1623
+ _this7.executeFetch();
1624
+ }
1625
+ }, this.currentRefetchInterval);
1626
+ };
1627
+ _proto.updateTimers = function updateTimers() {
1628
+ this.updateStaleTimeout();
1629
+ this.updateRefetchInterval(this.computeRefetchInterval());
1630
+ };
1631
+ _proto.clearTimers = function clearTimers() {
1632
+ this.clearStaleTimeout();
1633
+ this.clearRefetchInterval();
1634
+ };
1635
+ _proto.clearStaleTimeout = function clearStaleTimeout() {
1636
+ if (this.staleTimeoutId) {
1637
+ clearTimeout(this.staleTimeoutId);
1638
+ this.staleTimeoutId = void 0;
1639
+ }
1640
+ };
1641
+ _proto.clearRefetchInterval = function clearRefetchInterval() {
1642
+ if (this.refetchIntervalId) {
1643
+ clearInterval(this.refetchIntervalId);
1644
+ this.refetchIntervalId = void 0;
1645
+ }
1646
+ };
1647
+ _proto.createResult = function createResult(query, options) {
1648
+ var prevQuery = this.currentQuery;
1649
+ var prevOptions = this.options;
1650
+ var prevResult = this.currentResult;
1651
+ var prevResultState = this.currentResultState;
1652
+ var prevResultOptions = this.currentResultOptions;
1653
+ var queryChange = query !== prevQuery;
1654
+ var queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
1655
+ var prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
1656
+ var state = query.state;
1657
+ var dataUpdatedAt = state.dataUpdatedAt, error = state.error, errorUpdatedAt = state.errorUpdatedAt, isFetching = state.isFetching, status = state.status;
1658
+ var isPreviousData = false;
1659
+ var isPlaceholderData = false;
1660
+ var data;
1661
+ if (options.optimisticResults) {
1662
+ var mounted = this.hasListeners();
1663
+ var fetchOnMount = !mounted && shouldFetchOnMount(query, options);
1664
+ var fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
1665
+ if (fetchOnMount || fetchOptionally) {
1666
+ isFetching = true;
1667
+ if (!dataUpdatedAt) {
1668
+ status = "loading";
1669
+ }
1670
+ }
1671
+ }
1672
+ if (options.keepPreviousData && !state.dataUpdateCount && (prevQueryResult == null ? void 0 : prevQueryResult.isSuccess) && status !== "error") {
1673
+ data = prevQueryResult.data;
1674
+ dataUpdatedAt = prevQueryResult.dataUpdatedAt;
1675
+ status = prevQueryResult.status;
1676
+ isPreviousData = true;
1677
+ } else if (options.select && typeof state.data !== "undefined") {
1678
+ if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
1679
+ data = this.selectResult;
1680
+ } else {
1681
+ try {
1682
+ this.selectFn = options.select;
1683
+ data = options.select(state.data);
1684
+ if (options.structuralSharing !== false) {
1685
+ data = replaceEqualDeep(prevResult == null ? void 0 : prevResult.data, data);
1686
+ }
1687
+ this.selectResult = data;
1688
+ this.selectError = null;
1689
+ } catch (selectError) {
1690
+ getLogger().error(selectError);
1691
+ this.selectError = selectError;
1692
+ }
1693
+ }
1694
+ } else {
1695
+ data = state.data;
1696
+ }
1697
+ if (typeof options.placeholderData !== "undefined" && typeof data === "undefined" && (status === "loading" || status === "idle")) {
1698
+ var placeholderData;
1699
+ if ((prevResult == null ? void 0 : prevResult.isPlaceholderData) && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
1700
+ placeholderData = prevResult.data;
1701
+ } else {
1702
+ placeholderData = typeof options.placeholderData === "function" ? options.placeholderData() : options.placeholderData;
1703
+ if (options.select && typeof placeholderData !== "undefined") {
1704
+ try {
1705
+ placeholderData = options.select(placeholderData);
1706
+ if (options.structuralSharing !== false) {
1707
+ placeholderData = replaceEqualDeep(prevResult == null ? void 0 : prevResult.data, placeholderData);
1708
+ }
1709
+ this.selectError = null;
1710
+ } catch (selectError) {
1711
+ getLogger().error(selectError);
1712
+ this.selectError = selectError;
1713
+ }
1714
+ }
1715
+ }
1716
+ if (typeof placeholderData !== "undefined") {
1717
+ status = "success";
1718
+ data = placeholderData;
1719
+ isPlaceholderData = true;
1720
+ }
1721
+ }
1722
+ if (this.selectError) {
1723
+ error = this.selectError;
1724
+ data = this.selectResult;
1725
+ errorUpdatedAt = Date.now();
1726
+ status = "error";
1727
+ }
1728
+ var result = {
1729
+ status,
1730
+ isLoading: status === "loading",
1731
+ isSuccess: status === "success",
1732
+ isError: status === "error",
1733
+ isIdle: status === "idle",
1734
+ data,
1735
+ dataUpdatedAt,
1736
+ error,
1737
+ errorUpdatedAt,
1738
+ failureCount: state.fetchFailureCount,
1739
+ errorUpdateCount: state.errorUpdateCount,
1740
+ isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
1741
+ isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
1742
+ isFetching,
1743
+ isRefetching: isFetching && status !== "loading",
1744
+ isLoadingError: status === "error" && state.dataUpdatedAt === 0,
1745
+ isPlaceholderData,
1746
+ isPreviousData,
1747
+ isRefetchError: status === "error" && state.dataUpdatedAt !== 0,
1748
+ isStale: isStale(query, options),
1749
+ refetch: this.refetch,
1750
+ remove: this.remove
1751
+ };
1752
+ return result;
1753
+ };
1754
+ _proto.shouldNotifyListeners = function shouldNotifyListeners(result, prevResult) {
1755
+ if (!prevResult) {
1756
+ return true;
1757
+ }
1758
+ var _this$options = this.options, notifyOnChangeProps = _this$options.notifyOnChangeProps, notifyOnChangePropsExclusions = _this$options.notifyOnChangePropsExclusions;
1759
+ if (!notifyOnChangeProps && !notifyOnChangePropsExclusions) {
1760
+ return true;
1761
+ }
1762
+ if (notifyOnChangeProps === "tracked" && !this.trackedProps.length) {
1763
+ return true;
1764
+ }
1765
+ var includedProps = notifyOnChangeProps === "tracked" ? this.trackedProps : notifyOnChangeProps;
1766
+ return Object.keys(result).some(function(key) {
1767
+ var typedKey = key;
1768
+ var changed = result[typedKey] !== prevResult[typedKey];
1769
+ var isIncluded = includedProps == null ? void 0 : includedProps.some(function(x) {
1770
+ return x === key;
1771
+ });
1772
+ var isExcluded = notifyOnChangePropsExclusions == null ? void 0 : notifyOnChangePropsExclusions.some(function(x) {
1773
+ return x === key;
1774
+ });
1775
+ return changed && !isExcluded && (!includedProps || isIncluded);
1776
+ });
1777
+ };
1778
+ _proto.updateResult = function updateResult(notifyOptions) {
1779
+ var prevResult = this.currentResult;
1780
+ this.currentResult = this.createResult(this.currentQuery, this.options);
1781
+ this.currentResultState = this.currentQuery.state;
1782
+ this.currentResultOptions = this.options;
1783
+ if (shallowEqualObjects(this.currentResult, prevResult)) {
1784
+ return;
1785
+ }
1786
+ var defaultNotifyOptions = {
1787
+ cache: true
1788
+ };
1789
+ if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && this.shouldNotifyListeners(this.currentResult, prevResult)) {
1790
+ defaultNotifyOptions.listeners = true;
1791
+ }
1792
+ this.notify(_extends({}, defaultNotifyOptions, notifyOptions));
1793
+ };
1794
+ _proto.updateQuery = function updateQuery() {
1795
+ var query = this.client.getQueryCache().build(this.client, this.options);
1796
+ if (query === this.currentQuery) {
1797
+ return;
1798
+ }
1799
+ var prevQuery = this.currentQuery;
1800
+ this.currentQuery = query;
1801
+ this.currentQueryInitialState = query.state;
1802
+ this.previousQueryResult = this.currentResult;
1803
+ if (this.hasListeners()) {
1804
+ prevQuery == null ? void 0 : prevQuery.removeObserver(this);
1805
+ query.addObserver(this);
1806
+ }
1807
+ };
1808
+ _proto.onQueryUpdate = function onQueryUpdate(action) {
1809
+ var notifyOptions = {};
1810
+ if (action.type === "success") {
1811
+ notifyOptions.onSuccess = true;
1812
+ } else if (action.type === "error" && !isCancelledError(action.error)) {
1813
+ notifyOptions.onError = true;
1814
+ }
1815
+ this.updateResult(notifyOptions);
1816
+ if (this.hasListeners()) {
1817
+ this.updateTimers();
1818
+ }
1819
+ };
1820
+ _proto.notify = function notify(notifyOptions) {
1821
+ var _this8 = this;
1822
+ notifyManager.batch(function() {
1823
+ if (notifyOptions.onSuccess) {
1824
+ _this8.options.onSuccess == null ? void 0 : _this8.options.onSuccess(_this8.currentResult.data);
1825
+ _this8.options.onSettled == null ? void 0 : _this8.options.onSettled(_this8.currentResult.data, null);
1826
+ } else if (notifyOptions.onError) {
1827
+ _this8.options.onError == null ? void 0 : _this8.options.onError(_this8.currentResult.error);
1828
+ _this8.options.onSettled == null ? void 0 : _this8.options.onSettled(void 0, _this8.currentResult.error);
1829
+ }
1830
+ if (notifyOptions.listeners) {
1831
+ _this8.listeners.forEach(function(listener) {
1832
+ listener(_this8.currentResult);
1833
+ });
1834
+ }
1835
+ if (notifyOptions.cache) {
1836
+ _this8.client.getQueryCache().notify({
1837
+ query: _this8.currentQuery,
1838
+ type: "observerResultsUpdated"
1839
+ });
1840
+ }
1841
+ });
1842
+ };
1843
+ return QueryObserver2;
1844
+ })(Subscribable);
1845
+ function shouldLoadOnMount(query, options) {
1846
+ return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === "error" && options.retryOnMount === false);
1847
+ }
1848
+ function shouldFetchOnMount(query, options) {
1849
+ return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
1850
+ }
1851
+ function shouldFetchOn(query, options, field) {
1852
+ if (options.enabled !== false) {
1853
+ var value = typeof field === "function" ? field(query) : field;
1854
+ return value === "always" || value !== false && isStale(query, options);
1855
+ }
1856
+ return false;
1857
+ }
1858
+ function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
1859
+ return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== "error") && isStale(query, options);
1860
+ }
1861
+ function isStale(query, options) {
1862
+ return query.isStaleByTime(options.staleTime);
1863
+ }
1864
+ var QueriesObserver = /* @__PURE__ */ (function(_Subscribable) {
1865
+ _inheritsLoose(QueriesObserver2, _Subscribable);
1866
+ function QueriesObserver2(client, queries) {
1867
+ var _this;
1868
+ _this = _Subscribable.call(this) || this;
1869
+ _this.client = client;
1870
+ _this.queries = [];
1871
+ _this.result = [];
1872
+ _this.observers = [];
1873
+ _this.observersMap = {};
1874
+ if (queries) {
1875
+ _this.setQueries(queries);
1876
+ }
1877
+ return _this;
1878
+ }
1879
+ var _proto = QueriesObserver2.prototype;
1880
+ _proto.onSubscribe = function onSubscribe() {
1881
+ var _this2 = this;
1882
+ if (this.listeners.length === 1) {
1883
+ this.observers.forEach(function(observer) {
1884
+ observer.subscribe(function(result) {
1885
+ _this2.onUpdate(observer, result);
1886
+ });
1887
+ });
1888
+ }
1889
+ };
1890
+ _proto.onUnsubscribe = function onUnsubscribe() {
1891
+ if (!this.listeners.length) {
1892
+ this.destroy();
1893
+ }
1894
+ };
1895
+ _proto.destroy = function destroy() {
1896
+ this.listeners = [];
1897
+ this.observers.forEach(function(observer) {
1898
+ observer.destroy();
1899
+ });
1900
+ };
1901
+ _proto.setQueries = function setQueries(queries, notifyOptions) {
1902
+ this.queries = queries;
1903
+ this.updateObservers(notifyOptions);
1904
+ };
1905
+ _proto.getCurrentResult = function getCurrentResult() {
1906
+ return this.result;
1907
+ };
1908
+ _proto.getOptimisticResult = function getOptimisticResult(queries) {
1909
+ return this.findMatchingObservers(queries).map(function(match) {
1910
+ return match.observer.getOptimisticResult(match.defaultedQueryOptions);
1911
+ });
1912
+ };
1913
+ _proto.findMatchingObservers = function findMatchingObservers(queries) {
1914
+ var _this3 = this;
1915
+ var prevObservers = this.observers;
1916
+ var defaultedQueryOptions = queries.map(function(options) {
1917
+ return _this3.client.defaultQueryObserverOptions(options);
1918
+ });
1919
+ var matchingObservers = defaultedQueryOptions.flatMap(function(defaultedOptions) {
1920
+ var match = prevObservers.find(function(observer) {
1921
+ return observer.options.queryHash === defaultedOptions.queryHash;
1922
+ });
1923
+ if (match != null) {
1924
+ return [{
1925
+ defaultedQueryOptions: defaultedOptions,
1926
+ observer: match
1927
+ }];
1928
+ }
1929
+ return [];
1930
+ });
1931
+ var matchedQueryHashes = matchingObservers.map(function(match) {
1932
+ return match.defaultedQueryOptions.queryHash;
1933
+ });
1934
+ var unmatchedQueries = defaultedQueryOptions.filter(function(defaultedOptions) {
1935
+ return !matchedQueryHashes.includes(defaultedOptions.queryHash);
1936
+ });
1937
+ var unmatchedObservers = prevObservers.filter(function(prevObserver) {
1938
+ return !matchingObservers.some(function(match) {
1939
+ return match.observer === prevObserver;
1940
+ });
1941
+ });
1942
+ var newOrReusedObservers = unmatchedQueries.map(function(options, index) {
1943
+ if (options.keepPreviousData) {
1944
+ var previouslyUsedObserver = unmatchedObservers[index];
1945
+ if (previouslyUsedObserver !== void 0) {
1946
+ return {
1947
+ defaultedQueryOptions: options,
1948
+ observer: previouslyUsedObserver
1949
+ };
1950
+ }
1951
+ }
1952
+ return {
1953
+ defaultedQueryOptions: options,
1954
+ observer: _this3.getObserver(options)
1955
+ };
1956
+ });
1957
+ var sortMatchesByOrderOfQueries = function sortMatchesByOrderOfQueries2(a, b) {
1958
+ return defaultedQueryOptions.indexOf(a.defaultedQueryOptions) - defaultedQueryOptions.indexOf(b.defaultedQueryOptions);
1959
+ };
1960
+ return matchingObservers.concat(newOrReusedObservers).sort(sortMatchesByOrderOfQueries);
1961
+ };
1962
+ _proto.getObserver = function getObserver(options) {
1963
+ var defaultedOptions = this.client.defaultQueryObserverOptions(options);
1964
+ var currentObserver = this.observersMap[defaultedOptions.queryHash];
1965
+ return currentObserver != null ? currentObserver : new QueryObserver(this.client, defaultedOptions);
1966
+ };
1967
+ _proto.updateObservers = function updateObservers(notifyOptions) {
1968
+ var _this4 = this;
1969
+ notifyManager.batch(function() {
1970
+ var prevObservers = _this4.observers;
1971
+ var newObserverMatches = _this4.findMatchingObservers(_this4.queries);
1972
+ newObserverMatches.forEach(function(match) {
1973
+ return match.observer.setOptions(match.defaultedQueryOptions, notifyOptions);
1974
+ });
1975
+ var newObservers = newObserverMatches.map(function(match) {
1976
+ return match.observer;
1977
+ });
1978
+ var newObserversMap = Object.fromEntries(newObservers.map(function(observer) {
1979
+ return [observer.options.queryHash, observer];
1980
+ }));
1981
+ var newResult = newObservers.map(function(observer) {
1982
+ return observer.getCurrentResult();
1983
+ });
1984
+ var hasIndexChange = newObservers.some(function(observer, index) {
1985
+ return observer !== prevObservers[index];
1986
+ });
1987
+ if (prevObservers.length === newObservers.length && !hasIndexChange) {
1988
+ return;
1989
+ }
1990
+ _this4.observers = newObservers;
1991
+ _this4.observersMap = newObserversMap;
1992
+ _this4.result = newResult;
1993
+ if (!_this4.hasListeners()) {
1994
+ return;
1995
+ }
1996
+ difference(prevObservers, newObservers).forEach(function(observer) {
1997
+ observer.destroy();
1998
+ });
1999
+ difference(newObservers, prevObservers).forEach(function(observer) {
2000
+ observer.subscribe(function(result) {
2001
+ _this4.onUpdate(observer, result);
2002
+ });
2003
+ });
2004
+ _this4.notify();
2005
+ });
2006
+ };
2007
+ _proto.onUpdate = function onUpdate(observer, result) {
2008
+ var index = this.observers.indexOf(observer);
2009
+ if (index !== -1) {
2010
+ this.result = replaceAt(this.result, index, result);
2011
+ this.notify();
2012
+ }
2013
+ };
2014
+ _proto.notify = function notify() {
2015
+ var _this5 = this;
2016
+ notifyManager.batch(function() {
2017
+ _this5.listeners.forEach(function(listener) {
2018
+ listener(_this5.result);
2019
+ });
2020
+ });
2021
+ };
2022
+ return QueriesObserver2;
2023
+ })(Subscribable);
2024
+ var InfiniteQueryObserver = /* @__PURE__ */ (function(_QueryObserver) {
2025
+ _inheritsLoose(InfiniteQueryObserver2, _QueryObserver);
2026
+ function InfiniteQueryObserver2(client, options) {
2027
+ return _QueryObserver.call(this, client, options) || this;
2028
+ }
2029
+ var _proto = InfiniteQueryObserver2.prototype;
2030
+ _proto.bindMethods = function bindMethods() {
2031
+ _QueryObserver.prototype.bindMethods.call(this);
2032
+ this.fetchNextPage = this.fetchNextPage.bind(this);
2033
+ this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
2034
+ };
2035
+ _proto.setOptions = function setOptions(options, notifyOptions) {
2036
+ _QueryObserver.prototype.setOptions.call(this, _extends({}, options, {
2037
+ behavior: infiniteQueryBehavior()
2038
+ }), notifyOptions);
2039
+ };
2040
+ _proto.getOptimisticResult = function getOptimisticResult(options) {
2041
+ options.behavior = infiniteQueryBehavior();
2042
+ return _QueryObserver.prototype.getOptimisticResult.call(this, options);
2043
+ };
2044
+ _proto.fetchNextPage = function fetchNextPage(options) {
2045
+ var _options$cancelRefetc;
2046
+ return this.fetch({
2047
+ // TODO consider removing `?? true` in future breaking change, to be consistent with `refetch` API (see https://github.com/tannerlinsley/react-query/issues/2617)
2048
+ cancelRefetch: (_options$cancelRefetc = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc : true,
2049
+ throwOnError: options == null ? void 0 : options.throwOnError,
2050
+ meta: {
2051
+ fetchMore: {
2052
+ direction: "forward",
2053
+ pageParam: options == null ? void 0 : options.pageParam
2054
+ }
2055
+ }
2056
+ });
2057
+ };
2058
+ _proto.fetchPreviousPage = function fetchPreviousPage(options) {
2059
+ var _options$cancelRefetc2;
2060
+ return this.fetch({
2061
+ // TODO consider removing `?? true` in future breaking change, to be consistent with `refetch` API (see https://github.com/tannerlinsley/react-query/issues/2617)
2062
+ cancelRefetch: (_options$cancelRefetc2 = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc2 : true,
2063
+ throwOnError: options == null ? void 0 : options.throwOnError,
2064
+ meta: {
2065
+ fetchMore: {
2066
+ direction: "backward",
2067
+ pageParam: options == null ? void 0 : options.pageParam
2068
+ }
2069
+ }
2070
+ });
2071
+ };
2072
+ _proto.createResult = function createResult(query, options) {
2073
+ var _state$data, _state$data2, _state$fetchMeta, _state$fetchMeta$fetc, _state$fetchMeta2, _state$fetchMeta2$fet;
2074
+ var state = query.state;
2075
+ var result = _QueryObserver.prototype.createResult.call(this, query, options);
2076
+ return _extends({}, result, {
2077
+ fetchNextPage: this.fetchNextPage,
2078
+ fetchPreviousPage: this.fetchPreviousPage,
2079
+ hasNextPage: hasNextPage(options, (_state$data = state.data) == null ? void 0 : _state$data.pages),
2080
+ hasPreviousPage: hasPreviousPage(options, (_state$data2 = state.data) == null ? void 0 : _state$data2.pages),
2081
+ isFetchingNextPage: state.isFetching && ((_state$fetchMeta = state.fetchMeta) == null ? void 0 : (_state$fetchMeta$fetc = _state$fetchMeta.fetchMore) == null ? void 0 : _state$fetchMeta$fetc.direction) === "forward",
2082
+ isFetchingPreviousPage: state.isFetching && ((_state$fetchMeta2 = state.fetchMeta) == null ? void 0 : (_state$fetchMeta2$fet = _state$fetchMeta2.fetchMore) == null ? void 0 : _state$fetchMeta2$fet.direction) === "backward"
2083
+ });
2084
+ };
2085
+ return InfiniteQueryObserver2;
2086
+ })(QueryObserver);
2087
+ var MutationObserver = /* @__PURE__ */ (function(_Subscribable) {
2088
+ _inheritsLoose(MutationObserver2, _Subscribable);
2089
+ function MutationObserver2(client, options) {
2090
+ var _this;
2091
+ _this = _Subscribable.call(this) || this;
2092
+ _this.client = client;
2093
+ _this.setOptions(options);
2094
+ _this.bindMethods();
2095
+ _this.updateResult();
2096
+ return _this;
2097
+ }
2098
+ var _proto = MutationObserver2.prototype;
2099
+ _proto.bindMethods = function bindMethods() {
2100
+ this.mutate = this.mutate.bind(this);
2101
+ this.reset = this.reset.bind(this);
2102
+ };
2103
+ _proto.setOptions = function setOptions(options) {
2104
+ this.options = this.client.defaultMutationOptions(options);
2105
+ };
2106
+ _proto.onUnsubscribe = function onUnsubscribe() {
2107
+ if (!this.listeners.length) {
2108
+ var _this$currentMutation;
2109
+ (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.removeObserver(this);
2110
+ }
2111
+ };
2112
+ _proto.onMutationUpdate = function onMutationUpdate(action) {
2113
+ this.updateResult();
2114
+ var notifyOptions = {
2115
+ listeners: true
2116
+ };
2117
+ if (action.type === "success") {
2118
+ notifyOptions.onSuccess = true;
2119
+ } else if (action.type === "error") {
2120
+ notifyOptions.onError = true;
2121
+ }
2122
+ this.notify(notifyOptions);
2123
+ };
2124
+ _proto.getCurrentResult = function getCurrentResult() {
2125
+ return this.currentResult;
2126
+ };
2127
+ _proto.reset = function reset() {
2128
+ this.currentMutation = void 0;
2129
+ this.updateResult();
2130
+ this.notify({
2131
+ listeners: true
2132
+ });
2133
+ };
2134
+ _proto.mutate = function mutate(variables, options) {
2135
+ this.mutateOptions = options;
2136
+ if (this.currentMutation) {
2137
+ this.currentMutation.removeObserver(this);
2138
+ }
2139
+ this.currentMutation = this.client.getMutationCache().build(this.client, _extends({}, this.options, {
2140
+ variables: typeof variables !== "undefined" ? variables : this.options.variables
2141
+ }));
2142
+ this.currentMutation.addObserver(this);
2143
+ return this.currentMutation.execute();
2144
+ };
2145
+ _proto.updateResult = function updateResult() {
2146
+ var state = this.currentMutation ? this.currentMutation.state : getDefaultState();
2147
+ var result = _extends({}, state, {
2148
+ isLoading: state.status === "loading",
2149
+ isSuccess: state.status === "success",
2150
+ isError: state.status === "error",
2151
+ isIdle: state.status === "idle",
2152
+ mutate: this.mutate,
2153
+ reset: this.reset
2154
+ });
2155
+ this.currentResult = result;
2156
+ };
2157
+ _proto.notify = function notify(options) {
2158
+ var _this2 = this;
2159
+ notifyManager.batch(function() {
2160
+ if (_this2.mutateOptions) {
2161
+ if (options.onSuccess) {
2162
+ _this2.mutateOptions.onSuccess == null ? void 0 : _this2.mutateOptions.onSuccess(_this2.currentResult.data, _this2.currentResult.variables, _this2.currentResult.context);
2163
+ _this2.mutateOptions.onSettled == null ? void 0 : _this2.mutateOptions.onSettled(_this2.currentResult.data, null, _this2.currentResult.variables, _this2.currentResult.context);
2164
+ } else if (options.onError) {
2165
+ _this2.mutateOptions.onError == null ? void 0 : _this2.mutateOptions.onError(_this2.currentResult.error, _this2.currentResult.variables, _this2.currentResult.context);
2166
+ _this2.mutateOptions.onSettled == null ? void 0 : _this2.mutateOptions.onSettled(void 0, _this2.currentResult.error, _this2.currentResult.variables, _this2.currentResult.context);
2167
+ }
2168
+ }
2169
+ if (options.listeners) {
2170
+ _this2.listeners.forEach(function(listener) {
2171
+ listener(_this2.currentResult);
2172
+ });
2173
+ }
2174
+ });
2175
+ };
2176
+ return MutationObserver2;
2177
+ })(Subscribable);
2178
+ function dehydrateMutation(mutation) {
2179
+ return {
2180
+ mutationKey: mutation.options.mutationKey,
2181
+ state: mutation.state
2182
+ };
2183
+ }
2184
+ function dehydrateQuery(query) {
2185
+ return {
2186
+ state: query.state,
2187
+ queryKey: query.queryKey,
2188
+ queryHash: query.queryHash
2189
+ };
2190
+ }
2191
+ function defaultShouldDehydrateMutation(mutation) {
2192
+ return mutation.state.isPaused;
2193
+ }
2194
+ function defaultShouldDehydrateQuery(query) {
2195
+ return query.state.status === "success";
2196
+ }
2197
+ function dehydrate(client, options) {
2198
+ var _options, _options2;
2199
+ options = options || {};
2200
+ var mutations = [];
2201
+ var queries = [];
2202
+ if (((_options = options) == null ? void 0 : _options.dehydrateMutations) !== false) {
2203
+ var shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
2204
+ client.getMutationCache().getAll().forEach(function(mutation) {
2205
+ if (shouldDehydrateMutation(mutation)) {
2206
+ mutations.push(dehydrateMutation(mutation));
2207
+ }
2208
+ });
2209
+ }
2210
+ if (((_options2 = options) == null ? void 0 : _options2.dehydrateQueries) !== false) {
2211
+ var shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
2212
+ client.getQueryCache().getAll().forEach(function(query) {
2213
+ if (shouldDehydrateQuery(query)) {
2214
+ queries.push(dehydrateQuery(query));
2215
+ }
2216
+ });
2217
+ }
2218
+ return {
2219
+ mutations,
2220
+ queries
2221
+ };
2222
+ }
2223
+ function hydrate(client, dehydratedState, options) {
2224
+ if (typeof dehydratedState !== "object" || dehydratedState === null) {
2225
+ return;
2226
+ }
2227
+ var mutationCache = client.getMutationCache();
2228
+ var queryCache = client.getQueryCache();
2229
+ var mutations = dehydratedState.mutations || [];
2230
+ var queries = dehydratedState.queries || [];
2231
+ mutations.forEach(function(dehydratedMutation) {
2232
+ var _options$defaultOptio;
2233
+ mutationCache.build(client, _extends({}, options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations, {
2234
+ mutationKey: dehydratedMutation.mutationKey
2235
+ }), dehydratedMutation.state);
2236
+ });
2237
+ queries.forEach(function(dehydratedQuery) {
2238
+ var _options$defaultOptio2;
2239
+ var query = queryCache.get(dehydratedQuery.queryHash);
2240
+ if (query) {
2241
+ if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) {
2242
+ query.setState(dehydratedQuery.state);
2243
+ }
2244
+ return;
2245
+ }
2246
+ queryCache.build(client, _extends({}, options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries, {
2247
+ queryKey: dehydratedQuery.queryKey,
2248
+ queryHash: dehydratedQuery.queryHash
2249
+ }), dehydratedQuery.state);
2250
+ });
2251
+ }
2252
+ function createValue() {
2253
+ var _isReset = false;
2254
+ return {
2255
+ clearReset: function clearReset() {
2256
+ _isReset = false;
2257
+ },
2258
+ reset: function reset() {
2259
+ _isReset = true;
2260
+ },
2261
+ isReset: function isReset() {
2262
+ return _isReset;
2263
+ }
2264
+ };
2265
+ }
2266
+ var QueryErrorResetBoundaryContext = /* @__PURE__ */ React__default.createContext(createValue());
2267
+ var useQueryErrorResetBoundary = function useQueryErrorResetBoundary2() {
2268
+ return React__default.useContext(QueryErrorResetBoundaryContext);
2269
+ };
2270
+ var QueryErrorResetBoundary = function QueryErrorResetBoundary2(_ref) {
2271
+ var children = _ref.children;
2272
+ var value = React__default.useMemo(function() {
2273
+ return createValue();
2274
+ }, []);
2275
+ return /* @__PURE__ */ React__default.createElement(QueryErrorResetBoundaryContext.Provider, {
2276
+ value
2277
+ }, typeof children === "function" ? children(value) : children);
2278
+ };
2279
+ var checkIsFetching = function checkIsFetching2(queryClient, filters, isFetching, setIsFetching) {
2280
+ var newIsFetching = queryClient.isFetching(filters);
2281
+ if (isFetching !== newIsFetching) {
2282
+ setIsFetching(newIsFetching);
2283
+ }
2284
+ };
2285
+ function useIsFetching(arg1, arg2) {
2286
+ var mountedRef = React__default.useRef(false);
2287
+ var queryClient = useQueryClient();
2288
+ var _parseFilterArgs = parseFilterArgs(arg1, arg2), filters = _parseFilterArgs[0];
2289
+ var _React$useState = React__default.useState(queryClient.isFetching(filters)), isFetching = _React$useState[0], setIsFetching = _React$useState[1];
2290
+ var filtersRef = React__default.useRef(filters);
2291
+ filtersRef.current = filters;
2292
+ var isFetchingRef = React__default.useRef(isFetching);
2293
+ isFetchingRef.current = isFetching;
2294
+ React__default.useEffect(function() {
2295
+ mountedRef.current = true;
2296
+ checkIsFetching(queryClient, filtersRef.current, isFetchingRef.current, setIsFetching);
2297
+ var unsubscribe = queryClient.getQueryCache().subscribe(notifyManager.batchCalls(function() {
2298
+ if (mountedRef.current) {
2299
+ checkIsFetching(queryClient, filtersRef.current, isFetchingRef.current, setIsFetching);
2300
+ }
2301
+ }));
2302
+ return function() {
2303
+ mountedRef.current = false;
2304
+ unsubscribe();
2305
+ };
2306
+ }, [queryClient]);
2307
+ return isFetching;
2308
+ }
2309
+ function useIsMutating(arg1, arg2) {
2310
+ var mountedRef = React__default.useRef(false);
2311
+ var filters = parseMutationFilterArgs(arg1, arg2);
2312
+ var queryClient = useQueryClient();
2313
+ var _React$useState = React__default.useState(queryClient.isMutating(filters)), isMutating = _React$useState[0], setIsMutating = _React$useState[1];
2314
+ var filtersRef = React__default.useRef(filters);
2315
+ filtersRef.current = filters;
2316
+ var isMutatingRef = React__default.useRef(isMutating);
2317
+ isMutatingRef.current = isMutating;
2318
+ React__default.useEffect(function() {
2319
+ mountedRef.current = true;
2320
+ var unsubscribe = queryClient.getMutationCache().subscribe(notifyManager.batchCalls(function() {
2321
+ if (mountedRef.current) {
2322
+ var newIsMutating = queryClient.isMutating(filtersRef.current);
2323
+ if (isMutatingRef.current !== newIsMutating) {
2324
+ setIsMutating(newIsMutating);
2325
+ }
2326
+ }
2327
+ }));
2328
+ return function() {
2329
+ mountedRef.current = false;
2330
+ unsubscribe();
2331
+ };
2332
+ }, [queryClient]);
2333
+ return isMutating;
2334
+ }
2335
+ function shouldThrowError(suspense, _useErrorBoundary, params) {
2336
+ if (typeof _useErrorBoundary === "function") {
2337
+ return _useErrorBoundary.apply(void 0, params);
2338
+ }
2339
+ if (typeof _useErrorBoundary === "boolean") return _useErrorBoundary;
2340
+ return !!suspense;
2341
+ }
2342
+ function useMutation(arg1, arg2, arg3) {
2343
+ var mountedRef = React__default.useRef(false);
2344
+ var _React$useState = React__default.useState(0), forceUpdate = _React$useState[1];
2345
+ var options = parseMutationArgs(arg1, arg2, arg3);
2346
+ var queryClient = useQueryClient();
2347
+ var obsRef = React__default.useRef();
2348
+ if (!obsRef.current) {
2349
+ obsRef.current = new MutationObserver(queryClient, options);
2350
+ } else {
2351
+ obsRef.current.setOptions(options);
2352
+ }
2353
+ var currentResult = obsRef.current.getCurrentResult();
2354
+ React__default.useEffect(function() {
2355
+ mountedRef.current = true;
2356
+ var unsubscribe = obsRef.current.subscribe(notifyManager.batchCalls(function() {
2357
+ if (mountedRef.current) {
2358
+ forceUpdate(function(x) {
2359
+ return x + 1;
2360
+ });
2361
+ }
2362
+ }));
2363
+ return function() {
2364
+ mountedRef.current = false;
2365
+ unsubscribe();
2366
+ };
2367
+ }, []);
2368
+ var mutate = React__default.useCallback(function(variables, mutateOptions) {
2369
+ obsRef.current.mutate(variables, mutateOptions).catch(noop);
2370
+ }, []);
2371
+ if (currentResult.error && shouldThrowError(void 0, obsRef.current.options.useErrorBoundary, [currentResult.error])) {
2372
+ throw currentResult.error;
2373
+ }
2374
+ return _extends({}, currentResult, {
2375
+ mutate,
2376
+ mutateAsync: currentResult.mutate
2377
+ });
2378
+ }
2379
+ function useBaseQuery(options, Observer) {
2380
+ var mountedRef = React__default.useRef(false);
2381
+ var _React$useState = React__default.useState(0), forceUpdate = _React$useState[1];
2382
+ var queryClient = useQueryClient();
2383
+ var errorResetBoundary = useQueryErrorResetBoundary();
2384
+ var defaultedOptions = queryClient.defaultQueryObserverOptions(options);
2385
+ defaultedOptions.optimisticResults = true;
2386
+ if (defaultedOptions.onError) {
2387
+ defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
2388
+ }
2389
+ if (defaultedOptions.onSuccess) {
2390
+ defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
2391
+ }
2392
+ if (defaultedOptions.onSettled) {
2393
+ defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
2394
+ }
2395
+ if (defaultedOptions.suspense) {
2396
+ if (typeof defaultedOptions.staleTime !== "number") {
2397
+ defaultedOptions.staleTime = 1e3;
2398
+ }
2399
+ if (defaultedOptions.cacheTime === 0) {
2400
+ defaultedOptions.cacheTime = 1;
2401
+ }
2402
+ }
2403
+ if (defaultedOptions.suspense || defaultedOptions.useErrorBoundary) {
2404
+ if (!errorResetBoundary.isReset()) {
2405
+ defaultedOptions.retryOnMount = false;
2406
+ }
2407
+ }
2408
+ var _React$useState2 = React__default.useState(function() {
2409
+ return new Observer(queryClient, defaultedOptions);
2410
+ }), observer = _React$useState2[0];
2411
+ var result = observer.getOptimisticResult(defaultedOptions);
2412
+ React__default.useEffect(function() {
2413
+ mountedRef.current = true;
2414
+ errorResetBoundary.clearReset();
2415
+ var unsubscribe = observer.subscribe(notifyManager.batchCalls(function() {
2416
+ if (mountedRef.current) {
2417
+ forceUpdate(function(x) {
2418
+ return x + 1;
2419
+ });
2420
+ }
2421
+ }));
2422
+ observer.updateResult();
2423
+ return function() {
2424
+ mountedRef.current = false;
2425
+ unsubscribe();
2426
+ };
2427
+ }, [errorResetBoundary, observer]);
2428
+ React__default.useEffect(function() {
2429
+ observer.setOptions(defaultedOptions, {
2430
+ listeners: false
2431
+ });
2432
+ }, [defaultedOptions, observer]);
2433
+ if (defaultedOptions.suspense && result.isLoading) {
2434
+ throw observer.fetchOptimistic(defaultedOptions).then(function(_ref) {
2435
+ var data = _ref.data;
2436
+ defaultedOptions.onSuccess == null ? void 0 : defaultedOptions.onSuccess(data);
2437
+ defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(data, null);
2438
+ }).catch(function(error) {
2439
+ errorResetBoundary.clearReset();
2440
+ defaultedOptions.onError == null ? void 0 : defaultedOptions.onError(error);
2441
+ defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(void 0, error);
2442
+ });
2443
+ }
2444
+ if (result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(defaultedOptions.suspense, defaultedOptions.useErrorBoundary, [result.error, observer.getCurrentQuery()])) {
2445
+ throw result.error;
2446
+ }
2447
+ if (defaultedOptions.notifyOnChangeProps === "tracked") {
2448
+ result = observer.trackResult(result, defaultedOptions);
2449
+ }
2450
+ return result;
2451
+ }
2452
+ function useQuery(arg1, arg2, arg3) {
2453
+ var parsedOptions = parseQueryArgs(arg1, arg2, arg3);
2454
+ return useBaseQuery(parsedOptions, QueryObserver);
2455
+ }
2456
+ function useQueries(queries) {
2457
+ var mountedRef = React__default.useRef(false);
2458
+ var _React$useState = React__default.useState(0), forceUpdate = _React$useState[1];
2459
+ var queryClient = useQueryClient();
2460
+ var defaultedQueries = useMemo(function() {
2461
+ return queries.map(function(options) {
2462
+ var defaultedOptions = queryClient.defaultQueryObserverOptions(options);
2463
+ defaultedOptions.optimisticResults = true;
2464
+ return defaultedOptions;
2465
+ });
2466
+ }, [queries, queryClient]);
2467
+ var _React$useState2 = React__default.useState(function() {
2468
+ return new QueriesObserver(queryClient, defaultedQueries);
2469
+ }), observer = _React$useState2[0];
2470
+ var result = observer.getOptimisticResult(defaultedQueries);
2471
+ React__default.useEffect(function() {
2472
+ mountedRef.current = true;
2473
+ var unsubscribe = observer.subscribe(notifyManager.batchCalls(function() {
2474
+ if (mountedRef.current) {
2475
+ forceUpdate(function(x) {
2476
+ return x + 1;
2477
+ });
2478
+ }
2479
+ }));
2480
+ return function() {
2481
+ mountedRef.current = false;
2482
+ unsubscribe();
2483
+ };
2484
+ }, [observer]);
2485
+ React__default.useEffect(function() {
2486
+ observer.setQueries(defaultedQueries, {
2487
+ listeners: false
2488
+ });
2489
+ }, [defaultedQueries, observer]);
2490
+ return result;
2491
+ }
2492
+ function useInfiniteQuery(arg1, arg2, arg3) {
2493
+ var options = parseQueryArgs(arg1, arg2, arg3);
2494
+ return useBaseQuery(options, InfiniteQueryObserver);
2495
+ }
2496
+ function useHydrate(state, options) {
2497
+ var queryClient = useQueryClient();
2498
+ var optionsRef = React__default.useRef(options);
2499
+ optionsRef.current = options;
2500
+ React__default.useMemo(function() {
2501
+ if (state) {
2502
+ hydrate(queryClient, state, optionsRef.current);
2503
+ }
2504
+ }, [queryClient, state]);
2505
+ }
2506
+ var Hydrate = function Hydrate2(_ref) {
2507
+ var children = _ref.children, options = _ref.options, state = _ref.state;
2508
+ useHydrate(state, options);
2509
+ return children;
2510
+ };
2511
+ export {
2512
+ CancelledError,
2513
+ Hydrate,
2514
+ InfiniteQueryObserver,
2515
+ MutationCache,
2516
+ MutationObserver,
2517
+ QueriesObserver,
2518
+ QueryCache,
2519
+ QueryClient,
2520
+ Q as QueryClientProvider,
2521
+ QueryErrorResetBoundary,
2522
+ QueryObserver,
2523
+ dehydrate,
2524
+ focusManager,
2525
+ hashQueryKey,
2526
+ hydrate,
2527
+ isCancelledError,
2528
+ G as isError,
2529
+ notifyManager,
2530
+ onlineManager,
2531
+ H as setLogger,
2532
+ useHydrate,
2533
+ useInfiniteQuery,
2534
+ useIsFetching,
2535
+ useIsMutating,
2536
+ useMutation,
2537
+ useQueries,
2538
+ useQuery,
2539
+ useQueryClient,
2540
+ useQueryErrorResetBoundary
2541
+ };