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