@0xsquid/react-hooks 8.3.1-beta-stellar-issued-assets.1 → 8.3.1-embed-tanstack-build.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.
- package/dist/core/queries/queries-keys.d.ts +1 -5
- package/dist/core/types/index.d.ts +1 -1
- package/dist/core/types/stellar.d.ts +0 -52
- package/dist/core/types/tokens.d.ts +0 -4
- package/dist/hooks/index.d.ts +0 -1
- package/dist/{index-DPakWLan.js → index-D3LtAuC9.js} +3694 -361
- package/dist/index-D3LtAuC9.js.map +1 -0
- package/dist/{index-CJQHDfLM.js → index-DOoP75Yp.js} +3740 -418
- package/dist/index-DOoP75Yp.js.map +1 -0
- package/dist/{index.es-CMuCSzqv.js → index.es-C4uVlA0B.js} +2 -3
- package/dist/index.es-C4uVlA0B.js.map +1 -0
- package/dist/{index.es-CT2n71aB.js → index.es-C5DLFXAX.js} +2 -3
- package/dist/index.es-C5DLFXAX.js.map +1 -0
- package/dist/index.esm.js +1 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -13
- package/dist/index.js.map +1 -1
- package/dist/{secretService-D8hWz2Gz.js → secretService-BwFxfXV7.js} +2 -3
- package/dist/{secretService-D8hWz2Gz.js.map → secretService-BwFxfXV7.js.map} +1 -1
- package/dist/{secretService-CdbkGf17.js → secretService-CY6elBY4.js} +2 -3
- package/dist/{secretService-CdbkGf17.js.map → secretService-CY6elBY4.js.map} +1 -1
- package/dist/services/external/rpcService.d.ts +1 -1
- package/dist/services/external/stellarApiClient.d.ts +0 -4
- package/dist/services/external/stellarRpcClient.d.ts +2 -2
- package/dist/services/external/xrplRpcClient.d.ts +1 -2
- package/dist/services/index.d.ts +0 -1
- package/dist/services/internal/stellarService.d.ts +0 -9
- package/dist/services/internal/xrplService.d.ts +0 -2
- package/dist/{stellarService.client-ESo7_qhK.js → stellarService.client-2CVAOoJl.js} +2 -3
- package/dist/{stellarService.client-Dve9IZZm.js.map → stellarService.client-2CVAOoJl.js.map} +1 -1
- package/dist/{stellarService.client-Dve9IZZm.js → stellarService.client-OMPy5fjH.js} +2 -3
- package/dist/{stellarService.client-ESo7_qhK.js.map → stellarService.client-OMPy5fjH.js.map} +1 -1
- package/package.json +1 -1
- package/dist/hooks/stellar/useStellarTrustLine.d.ts +0 -13
- package/dist/index-CJQHDfLM.js.map +0 -1
- package/dist/index-DPakWLan.js.map +0 -1
- package/dist/index.es-CMuCSzqv.js.map +0 -1
- package/dist/index.es-CT2n71aB.js.map +0 -1
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
var squidTypes = require('@0xsquid/squid-types');
|
|
4
4
|
var viem = require('viem');
|
|
5
5
|
var React = require('react');
|
|
6
|
-
var reactQuery = require('@tanstack/react-query');
|
|
7
6
|
var encoding = require('@cosmjs/encoding');
|
|
8
7
|
var bitcoin = require('bitcoinjs-lib');
|
|
9
8
|
var ecc = require('@bitcoinerlab/secp256k1');
|
|
@@ -54,6 +53,7 @@ function _interopNamespaceDefault(e) {
|
|
|
54
53
|
return Object.freeze(n);
|
|
55
54
|
}
|
|
56
55
|
|
|
56
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
57
57
|
var bitcoin__namespace = /*#__PURE__*/_interopNamespaceDefault(bitcoin);
|
|
58
58
|
var ecc__namespace = /*#__PURE__*/_interopNamespaceDefault(ecc);
|
|
59
59
|
|
|
@@ -221,22 +221,3053 @@ const transactionEndStatuses = [
|
|
|
221
221
|
...transactionErrorStatuses,
|
|
222
222
|
];
|
|
223
223
|
|
|
224
|
-
|
|
225
|
-
(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
224
|
+
class Subscribable {
|
|
225
|
+
constructor() {
|
|
226
|
+
this.listeners = new Set();
|
|
227
|
+
this.subscribe = this.subscribe.bind(this);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
subscribe(listener) {
|
|
231
|
+
const identity = {
|
|
232
|
+
listener
|
|
233
|
+
};
|
|
234
|
+
this.listeners.add(identity);
|
|
235
|
+
this.onSubscribe();
|
|
236
|
+
return () => {
|
|
237
|
+
this.listeners.delete(identity);
|
|
238
|
+
this.onUnsubscribe();
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
hasListeners() {
|
|
243
|
+
return this.listeners.size > 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
onSubscribe() {// Do nothing
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
onUnsubscribe() {// Do nothing
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// TYPES
|
|
255
|
+
// UTILS
|
|
256
|
+
const isServer = typeof window === 'undefined' || 'Deno' in window;
|
|
257
|
+
function noop$1() {
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
function functionalUpdate(updater, input) {
|
|
261
|
+
return typeof updater === 'function' ? updater(input) : updater;
|
|
262
|
+
}
|
|
263
|
+
function isValidTimeout(value) {
|
|
264
|
+
return typeof value === 'number' && value >= 0 && value !== Infinity;
|
|
265
|
+
}
|
|
266
|
+
function difference(array1, array2) {
|
|
267
|
+
return array1.filter(x => !array2.includes(x));
|
|
268
|
+
}
|
|
269
|
+
function replaceAt(array, index, value) {
|
|
270
|
+
const copy = array.slice(0);
|
|
271
|
+
copy[index] = value;
|
|
272
|
+
return copy;
|
|
273
|
+
}
|
|
274
|
+
function timeUntilStale(updatedAt, staleTime) {
|
|
275
|
+
return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
|
|
276
|
+
}
|
|
277
|
+
function parseQueryArgs(arg1, arg2, arg3) {
|
|
278
|
+
if (!isQueryKey(arg1)) {
|
|
279
|
+
return arg1;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (typeof arg2 === 'function') {
|
|
283
|
+
return { ...arg3,
|
|
284
|
+
queryKey: arg1,
|
|
285
|
+
queryFn: arg2
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return { ...arg2,
|
|
290
|
+
queryKey: arg1
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function parseMutationArgs(arg1, arg2, arg3) {
|
|
294
|
+
if (isQueryKey(arg1)) {
|
|
295
|
+
if (typeof arg2 === 'function') {
|
|
296
|
+
return { ...arg3,
|
|
297
|
+
mutationKey: arg1,
|
|
298
|
+
mutationFn: arg2
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return { ...arg2,
|
|
303
|
+
mutationKey: arg1
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (typeof arg1 === 'function') {
|
|
308
|
+
return { ...arg2,
|
|
309
|
+
mutationFn: arg1
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return { ...arg1
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
function parseFilterArgs(arg1, arg2, arg3) {
|
|
317
|
+
return isQueryKey(arg1) ? [{ ...arg2,
|
|
318
|
+
queryKey: arg1
|
|
319
|
+
}, arg3] : [arg1 || {}, arg2];
|
|
320
|
+
}
|
|
321
|
+
function matchQuery(filters, query) {
|
|
322
|
+
const {
|
|
323
|
+
type = 'all',
|
|
324
|
+
exact,
|
|
325
|
+
fetchStatus,
|
|
326
|
+
predicate,
|
|
327
|
+
queryKey,
|
|
328
|
+
stale
|
|
329
|
+
} = filters;
|
|
330
|
+
|
|
331
|
+
if (isQueryKey(queryKey)) {
|
|
332
|
+
if (exact) {
|
|
333
|
+
if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
} else if (!partialMatchKey(query.queryKey, queryKey)) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (type !== 'all') {
|
|
342
|
+
const isActive = query.isActive();
|
|
343
|
+
|
|
344
|
+
if (type === 'active' && !isActive) {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (type === 'inactive' && isActive) {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (typeof stale === 'boolean' && query.isStale() !== stale) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (predicate && !predicate(query)) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
function matchMutation(filters, mutation) {
|
|
368
|
+
const {
|
|
369
|
+
exact,
|
|
370
|
+
fetching,
|
|
371
|
+
predicate,
|
|
372
|
+
mutationKey
|
|
373
|
+
} = filters;
|
|
374
|
+
|
|
375
|
+
if (isQueryKey(mutationKey)) {
|
|
376
|
+
if (!mutation.options.mutationKey) {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (exact) {
|
|
381
|
+
if (hashQueryKey(mutation.options.mutationKey) !== hashQueryKey(mutationKey)) {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
} else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (typeof fetching === 'boolean' && mutation.state.status === 'loading' !== fetching) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (predicate && !predicate(mutation)) {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
function hashQueryKeyByOptions(queryKey, options) {
|
|
400
|
+
const hashFn = (options == null ? void 0 : options.queryKeyHashFn) || hashQueryKey;
|
|
401
|
+
return hashFn(queryKey);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Default query keys hash function.
|
|
405
|
+
* Hashes the value into a stable hash.
|
|
406
|
+
*/
|
|
407
|
+
|
|
408
|
+
function hashQueryKey(queryKey) {
|
|
409
|
+
return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
|
|
410
|
+
result[key] = val[key];
|
|
411
|
+
return result;
|
|
412
|
+
}, {}) : val);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Checks if key `b` partially matches with key `a`.
|
|
416
|
+
*/
|
|
417
|
+
|
|
418
|
+
function partialMatchKey(a, b) {
|
|
419
|
+
return partialDeepEqual(a, b);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Checks if `b` partially matches with `a`.
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
function partialDeepEqual(a, b) {
|
|
426
|
+
if (a === b) {
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (typeof a !== typeof b) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
435
|
+
return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key]));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* This function returns `a` if `b` is deeply equal.
|
|
442
|
+
* If not, it will replace any deeply equal children of `b` with those of `a`.
|
|
443
|
+
* This can be used for structural sharing between JSON values for example.
|
|
444
|
+
*/
|
|
445
|
+
|
|
446
|
+
function replaceEqualDeep(a, b) {
|
|
447
|
+
if (a === b) {
|
|
448
|
+
return a;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const array = isPlainArray(a) && isPlainArray(b);
|
|
452
|
+
|
|
453
|
+
if (array || isPlainObject(a) && isPlainObject(b)) {
|
|
454
|
+
const aSize = array ? a.length : Object.keys(a).length;
|
|
455
|
+
const bItems = array ? b : Object.keys(b);
|
|
456
|
+
const bSize = bItems.length;
|
|
457
|
+
const copy = array ? [] : {};
|
|
458
|
+
let equalItems = 0;
|
|
459
|
+
|
|
460
|
+
for (let i = 0; i < bSize; i++) {
|
|
461
|
+
const key = array ? i : bItems[i];
|
|
462
|
+
copy[key] = replaceEqualDeep(a[key], b[key]);
|
|
463
|
+
|
|
464
|
+
if (copy[key] === a[key]) {
|
|
465
|
+
equalItems++;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return aSize === bSize && equalItems === aSize ? a : copy;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return b;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Shallow compare objects. Only works with objects that always have the same properties.
|
|
476
|
+
*/
|
|
477
|
+
|
|
478
|
+
function shallowEqualObjects(a, b) {
|
|
479
|
+
if (a && !b || b && !a) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
for (const key in a) {
|
|
484
|
+
if (a[key] !== b[key]) {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
function isPlainArray(value) {
|
|
492
|
+
return Array.isArray(value) && value.length === Object.keys(value).length;
|
|
493
|
+
} // Copied from: https://github.com/jonschlinkert/is-plain-object
|
|
494
|
+
|
|
495
|
+
function isPlainObject(o) {
|
|
496
|
+
if (!hasObjectPrototype(o)) {
|
|
497
|
+
return false;
|
|
498
|
+
} // If has modified constructor
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
const ctor = o.constructor;
|
|
502
|
+
|
|
503
|
+
if (typeof ctor === 'undefined') {
|
|
504
|
+
return true;
|
|
505
|
+
} // If has modified prototype
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
const prot = ctor.prototype;
|
|
509
|
+
|
|
510
|
+
if (!hasObjectPrototype(prot)) {
|
|
511
|
+
return false;
|
|
512
|
+
} // If constructor does not have an Object-specific method
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
if (!prot.hasOwnProperty('isPrototypeOf')) {
|
|
516
|
+
return false;
|
|
517
|
+
} // Most likely a plain Object
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function hasObjectPrototype(o) {
|
|
524
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function isQueryKey(value) {
|
|
528
|
+
return Array.isArray(value);
|
|
529
|
+
}
|
|
530
|
+
function sleep$1(timeout) {
|
|
531
|
+
return new Promise(resolve => {
|
|
532
|
+
setTimeout(resolve, timeout);
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Schedules a microtask.
|
|
537
|
+
* This can be useful to schedule state updates after rendering.
|
|
538
|
+
*/
|
|
539
|
+
|
|
540
|
+
function scheduleMicrotask(callback) {
|
|
541
|
+
sleep$1(0).then(callback);
|
|
542
|
+
}
|
|
543
|
+
function getAbortController() {
|
|
544
|
+
if (typeof AbortController === 'function') {
|
|
545
|
+
return new AbortController();
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
function replaceData(prevData, data, options) {
|
|
551
|
+
// Use prev data if an isDataEqual function is defined and returns `true`
|
|
552
|
+
if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
|
|
553
|
+
return prevData;
|
|
554
|
+
} else if (typeof options.structuralSharing === 'function') {
|
|
555
|
+
return options.structuralSharing(prevData, data);
|
|
556
|
+
} else if (options.structuralSharing !== false) {
|
|
557
|
+
// Structurally share data between prev and new data if needed
|
|
558
|
+
return replaceEqualDeep(prevData, data);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return data;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
class FocusManager extends Subscribable {
|
|
565
|
+
constructor() {
|
|
566
|
+
super();
|
|
567
|
+
|
|
568
|
+
this.setup = onFocus => {
|
|
569
|
+
// addEventListener does not exist in React Native, but window does
|
|
570
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
571
|
+
if (!isServer && window.addEventListener) {
|
|
572
|
+
const listener = () => onFocus(); // Listen to visibillitychange and focus
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
window.addEventListener('visibilitychange', listener, false);
|
|
576
|
+
window.addEventListener('focus', listener, false);
|
|
577
|
+
return () => {
|
|
578
|
+
// Be sure to unsubscribe if a new handler is set
|
|
579
|
+
window.removeEventListener('visibilitychange', listener);
|
|
580
|
+
window.removeEventListener('focus', listener);
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
return;
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
onSubscribe() {
|
|
589
|
+
if (!this.cleanup) {
|
|
590
|
+
this.setEventListener(this.setup);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
onUnsubscribe() {
|
|
595
|
+
if (!this.hasListeners()) {
|
|
596
|
+
var _this$cleanup;
|
|
597
|
+
|
|
598
|
+
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
599
|
+
this.cleanup = undefined;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
setEventListener(setup) {
|
|
604
|
+
var _this$cleanup2;
|
|
605
|
+
|
|
606
|
+
this.setup = setup;
|
|
607
|
+
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
608
|
+
this.cleanup = setup(focused => {
|
|
609
|
+
if (typeof focused === 'boolean') {
|
|
610
|
+
this.setFocused(focused);
|
|
611
|
+
} else {
|
|
612
|
+
this.onFocus();
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
setFocused(focused) {
|
|
618
|
+
const changed = this.focused !== focused;
|
|
619
|
+
|
|
620
|
+
if (changed) {
|
|
621
|
+
this.focused = focused;
|
|
622
|
+
this.onFocus();
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
onFocus() {
|
|
627
|
+
this.listeners.forEach(({
|
|
628
|
+
listener
|
|
629
|
+
}) => {
|
|
630
|
+
listener();
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
isFocused() {
|
|
635
|
+
if (typeof this.focused === 'boolean') {
|
|
636
|
+
return this.focused;
|
|
637
|
+
} // document global can be unavailable in react native
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
if (typeof document === 'undefined') {
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
return [undefined, 'visible', 'prerender'].includes(document.visibilityState);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
}
|
|
648
|
+
const focusManager = new FocusManager();
|
|
649
|
+
|
|
650
|
+
const onlineEvents = ['online', 'offline'];
|
|
651
|
+
class OnlineManager extends Subscribable {
|
|
652
|
+
constructor() {
|
|
653
|
+
super();
|
|
654
|
+
|
|
655
|
+
this.setup = onOnline => {
|
|
656
|
+
// addEventListener does not exist in React Native, but window does
|
|
657
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
658
|
+
if (!isServer && window.addEventListener) {
|
|
659
|
+
const listener = () => onOnline(); // Listen to online
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
onlineEvents.forEach(event => {
|
|
663
|
+
window.addEventListener(event, listener, false);
|
|
664
|
+
});
|
|
665
|
+
return () => {
|
|
666
|
+
// Be sure to unsubscribe if a new handler is set
|
|
667
|
+
onlineEvents.forEach(event => {
|
|
668
|
+
window.removeEventListener(event, listener);
|
|
669
|
+
});
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return;
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
onSubscribe() {
|
|
678
|
+
if (!this.cleanup) {
|
|
679
|
+
this.setEventListener(this.setup);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
onUnsubscribe() {
|
|
684
|
+
if (!this.hasListeners()) {
|
|
685
|
+
var _this$cleanup;
|
|
686
|
+
|
|
687
|
+
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
688
|
+
this.cleanup = undefined;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
setEventListener(setup) {
|
|
693
|
+
var _this$cleanup2;
|
|
694
|
+
|
|
695
|
+
this.setup = setup;
|
|
696
|
+
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
697
|
+
this.cleanup = setup(online => {
|
|
698
|
+
if (typeof online === 'boolean') {
|
|
699
|
+
this.setOnline(online);
|
|
700
|
+
} else {
|
|
701
|
+
this.onOnline();
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
setOnline(online) {
|
|
707
|
+
const changed = this.online !== online;
|
|
708
|
+
|
|
709
|
+
if (changed) {
|
|
710
|
+
this.online = online;
|
|
711
|
+
this.onOnline();
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
onOnline() {
|
|
716
|
+
this.listeners.forEach(({
|
|
717
|
+
listener
|
|
718
|
+
}) => {
|
|
719
|
+
listener();
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
isOnline() {
|
|
724
|
+
if (typeof this.online === 'boolean') {
|
|
725
|
+
return this.online;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
return navigator.onLine;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
}
|
|
736
|
+
const onlineManager = new OnlineManager();
|
|
737
|
+
|
|
738
|
+
function defaultRetryDelay(failureCount) {
|
|
739
|
+
return Math.min(1000 * 2 ** failureCount, 30000);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function canFetch(networkMode) {
|
|
743
|
+
return (networkMode != null ? networkMode : 'online') === 'online' ? onlineManager.isOnline() : true;
|
|
744
|
+
}
|
|
745
|
+
class CancelledError {
|
|
746
|
+
constructor(options) {
|
|
747
|
+
this.revert = options == null ? void 0 : options.revert;
|
|
748
|
+
this.silent = options == null ? void 0 : options.silent;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
}
|
|
752
|
+
function isCancelledError(value) {
|
|
753
|
+
return value instanceof CancelledError;
|
|
754
|
+
}
|
|
755
|
+
function createRetryer(config) {
|
|
756
|
+
let isRetryCancelled = false;
|
|
757
|
+
let failureCount = 0;
|
|
758
|
+
let isResolved = false;
|
|
759
|
+
let continueFn;
|
|
760
|
+
let promiseResolve;
|
|
761
|
+
let promiseReject;
|
|
762
|
+
const promise = new Promise((outerResolve, outerReject) => {
|
|
763
|
+
promiseResolve = outerResolve;
|
|
764
|
+
promiseReject = outerReject;
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
const cancel = cancelOptions => {
|
|
768
|
+
if (!isResolved) {
|
|
769
|
+
reject(new CancelledError(cancelOptions));
|
|
770
|
+
config.abort == null ? void 0 : config.abort();
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
const cancelRetry = () => {
|
|
775
|
+
isRetryCancelled = true;
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
const continueRetry = () => {
|
|
779
|
+
isRetryCancelled = false;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
const shouldPause = () => !focusManager.isFocused() || config.networkMode !== 'always' && !onlineManager.isOnline();
|
|
783
|
+
|
|
784
|
+
const resolve = value => {
|
|
785
|
+
if (!isResolved) {
|
|
786
|
+
isResolved = true;
|
|
787
|
+
config.onSuccess == null ? void 0 : config.onSuccess(value);
|
|
788
|
+
continueFn == null ? void 0 : continueFn();
|
|
789
|
+
promiseResolve(value);
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const reject = value => {
|
|
794
|
+
if (!isResolved) {
|
|
795
|
+
isResolved = true;
|
|
796
|
+
config.onError == null ? void 0 : config.onError(value);
|
|
797
|
+
continueFn == null ? void 0 : continueFn();
|
|
798
|
+
promiseReject(value);
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
const pause = () => {
|
|
803
|
+
return new Promise(continueResolve => {
|
|
804
|
+
continueFn = value => {
|
|
805
|
+
const canContinue = isResolved || !shouldPause();
|
|
806
|
+
|
|
807
|
+
if (canContinue) {
|
|
808
|
+
continueResolve(value);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
return canContinue;
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
config.onPause == null ? void 0 : config.onPause();
|
|
815
|
+
}).then(() => {
|
|
816
|
+
continueFn = undefined;
|
|
817
|
+
|
|
818
|
+
if (!isResolved) {
|
|
819
|
+
config.onContinue == null ? void 0 : config.onContinue();
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
}; // Create loop function
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
const run = () => {
|
|
826
|
+
// Do nothing if already resolved
|
|
827
|
+
if (isResolved) {
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
let promiseOrValue; // Execute query
|
|
832
|
+
|
|
833
|
+
try {
|
|
834
|
+
promiseOrValue = config.fn();
|
|
835
|
+
} catch (error) {
|
|
836
|
+
promiseOrValue = Promise.reject(error);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
Promise.resolve(promiseOrValue).then(resolve).catch(error => {
|
|
840
|
+
var _config$retry, _config$retryDelay;
|
|
841
|
+
|
|
842
|
+
// Stop if the fetch is already resolved
|
|
843
|
+
if (isResolved) {
|
|
844
|
+
return;
|
|
845
|
+
} // Do we need to retry the request?
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
const retry = (_config$retry = config.retry) != null ? _config$retry : 3;
|
|
849
|
+
const retryDelay = (_config$retryDelay = config.retryDelay) != null ? _config$retryDelay : defaultRetryDelay;
|
|
850
|
+
const delay = typeof retryDelay === 'function' ? retryDelay(failureCount, error) : retryDelay;
|
|
851
|
+
const shouldRetry = retry === true || typeof retry === 'number' && failureCount < retry || typeof retry === 'function' && retry(failureCount, error);
|
|
852
|
+
|
|
853
|
+
if (isRetryCancelled || !shouldRetry) {
|
|
854
|
+
// We are done if the query does not need to be retried
|
|
855
|
+
reject(error);
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
failureCount++; // Notify on fail
|
|
860
|
+
|
|
861
|
+
config.onFail == null ? void 0 : config.onFail(failureCount, error); // Delay
|
|
862
|
+
|
|
863
|
+
sleep$1(delay) // Pause if the document is not visible or when the device is offline
|
|
864
|
+
.then(() => {
|
|
865
|
+
if (shouldPause()) {
|
|
866
|
+
return pause();
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
return;
|
|
870
|
+
}).then(() => {
|
|
871
|
+
if (isRetryCancelled) {
|
|
872
|
+
reject(error);
|
|
873
|
+
} else {
|
|
874
|
+
run();
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
});
|
|
878
|
+
}; // Start loop
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
if (canFetch(config.networkMode)) {
|
|
882
|
+
run();
|
|
883
|
+
} else {
|
|
884
|
+
pause().then(run);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
return {
|
|
888
|
+
promise,
|
|
889
|
+
cancel,
|
|
890
|
+
continue: () => {
|
|
891
|
+
const didContinue = continueFn == null ? void 0 : continueFn();
|
|
892
|
+
return didContinue ? promise : Promise.resolve();
|
|
893
|
+
},
|
|
894
|
+
cancelRetry,
|
|
895
|
+
continueRetry
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
const defaultLogger = console;
|
|
900
|
+
|
|
901
|
+
function createNotifyManager() {
|
|
902
|
+
let queue = [];
|
|
903
|
+
let transactions = 0;
|
|
904
|
+
|
|
905
|
+
let notifyFn = callback => {
|
|
906
|
+
callback();
|
|
907
|
+
};
|
|
908
|
+
|
|
909
|
+
let batchNotifyFn = callback => {
|
|
910
|
+
callback();
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
const batch = callback => {
|
|
914
|
+
let result;
|
|
915
|
+
transactions++;
|
|
916
|
+
|
|
917
|
+
try {
|
|
918
|
+
result = callback();
|
|
919
|
+
} finally {
|
|
920
|
+
transactions--;
|
|
921
|
+
|
|
922
|
+
if (!transactions) {
|
|
923
|
+
flush();
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
return result;
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
const schedule = callback => {
|
|
931
|
+
if (transactions) {
|
|
932
|
+
queue.push(callback);
|
|
933
|
+
} else {
|
|
934
|
+
scheduleMicrotask(() => {
|
|
935
|
+
notifyFn(callback);
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
/**
|
|
940
|
+
* All calls to the wrapped function will be batched.
|
|
941
|
+
*/
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
const batchCalls = callback => {
|
|
945
|
+
return (...args) => {
|
|
946
|
+
schedule(() => {
|
|
947
|
+
callback(...args);
|
|
948
|
+
});
|
|
949
|
+
};
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
const flush = () => {
|
|
953
|
+
const originalQueue = queue;
|
|
954
|
+
queue = [];
|
|
955
|
+
|
|
956
|
+
if (originalQueue.length) {
|
|
957
|
+
scheduleMicrotask(() => {
|
|
958
|
+
batchNotifyFn(() => {
|
|
959
|
+
originalQueue.forEach(callback => {
|
|
960
|
+
notifyFn(callback);
|
|
961
|
+
});
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
/**
|
|
967
|
+
* Use this method to set a custom notify function.
|
|
968
|
+
* This can be used to for example wrap notifications with `React.act` while running tests.
|
|
969
|
+
*/
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
const setNotifyFunction = fn => {
|
|
973
|
+
notifyFn = fn;
|
|
974
|
+
};
|
|
975
|
+
/**
|
|
976
|
+
* Use this method to set a custom function to batch notifications together into a single tick.
|
|
977
|
+
* By default React Query will use the batch function provided by ReactDOM or React Native.
|
|
978
|
+
*/
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
const setBatchNotifyFunction = fn => {
|
|
982
|
+
batchNotifyFn = fn;
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
return {
|
|
986
|
+
batch,
|
|
987
|
+
batchCalls,
|
|
988
|
+
schedule,
|
|
989
|
+
setNotifyFunction,
|
|
990
|
+
setBatchNotifyFunction
|
|
991
|
+
};
|
|
992
|
+
} // SINGLETON
|
|
993
|
+
|
|
994
|
+
const notifyManager = createNotifyManager();
|
|
995
|
+
|
|
996
|
+
class Removable {
|
|
997
|
+
destroy() {
|
|
998
|
+
this.clearGcTimeout();
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
scheduleGc() {
|
|
1002
|
+
this.clearGcTimeout();
|
|
1003
|
+
|
|
1004
|
+
if (isValidTimeout(this.cacheTime)) {
|
|
1005
|
+
this.gcTimeout = setTimeout(() => {
|
|
1006
|
+
this.optionalRemove();
|
|
1007
|
+
}, this.cacheTime);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
updateCacheTime(newCacheTime) {
|
|
1012
|
+
// Default to 5 minutes (Infinity for server-side) if no cache time is set
|
|
1013
|
+
this.cacheTime = Math.max(this.cacheTime || 0, newCacheTime != null ? newCacheTime : isServer ? Infinity : 5 * 60 * 1000);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
clearGcTimeout() {
|
|
1017
|
+
if (this.gcTimeout) {
|
|
1018
|
+
clearTimeout(this.gcTimeout);
|
|
1019
|
+
this.gcTimeout = undefined;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// CLASS
|
|
1026
|
+
class Query extends Removable {
|
|
1027
|
+
constructor(config) {
|
|
1028
|
+
super();
|
|
1029
|
+
this.abortSignalConsumed = false;
|
|
1030
|
+
this.defaultOptions = config.defaultOptions;
|
|
1031
|
+
this.setOptions(config.options);
|
|
1032
|
+
this.observers = [];
|
|
1033
|
+
this.cache = config.cache;
|
|
1034
|
+
this.logger = config.logger || defaultLogger;
|
|
1035
|
+
this.queryKey = config.queryKey;
|
|
1036
|
+
this.queryHash = config.queryHash;
|
|
1037
|
+
this.initialState = config.state || getDefaultState$1(this.options);
|
|
1038
|
+
this.state = this.initialState;
|
|
1039
|
+
this.scheduleGc();
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
get meta() {
|
|
1043
|
+
return this.options.meta;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
setOptions(options) {
|
|
1047
|
+
this.options = { ...this.defaultOptions,
|
|
1048
|
+
...options
|
|
1049
|
+
};
|
|
1050
|
+
this.updateCacheTime(this.options.cacheTime);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
optionalRemove() {
|
|
1054
|
+
if (!this.observers.length && this.state.fetchStatus === 'idle') {
|
|
1055
|
+
this.cache.remove(this);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
setData(newData, options) {
|
|
1060
|
+
const data = replaceData(this.state.data, newData, this.options); // Set data and mark it as cached
|
|
1061
|
+
|
|
1062
|
+
this.dispatch({
|
|
1063
|
+
data,
|
|
1064
|
+
type: 'success',
|
|
1065
|
+
dataUpdatedAt: options == null ? void 0 : options.updatedAt,
|
|
1066
|
+
manual: options == null ? void 0 : options.manual
|
|
1067
|
+
});
|
|
1068
|
+
return data;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
setState(state, setStateOptions) {
|
|
1072
|
+
this.dispatch({
|
|
1073
|
+
type: 'setState',
|
|
1074
|
+
state,
|
|
1075
|
+
setStateOptions
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
cancel(options) {
|
|
1080
|
+
var _this$retryer;
|
|
1081
|
+
|
|
1082
|
+
const promise = this.promise;
|
|
1083
|
+
(_this$retryer = this.retryer) == null ? void 0 : _this$retryer.cancel(options);
|
|
1084
|
+
return promise ? promise.then(noop$1).catch(noop$1) : Promise.resolve();
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
destroy() {
|
|
1088
|
+
super.destroy();
|
|
1089
|
+
this.cancel({
|
|
1090
|
+
silent: true
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
reset() {
|
|
1095
|
+
this.destroy();
|
|
1096
|
+
this.setState(this.initialState);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
isActive() {
|
|
1100
|
+
return this.observers.some(observer => observer.options.enabled !== false);
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
isDisabled() {
|
|
1104
|
+
return this.getObserversCount() > 0 && !this.isActive();
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
isStale() {
|
|
1108
|
+
return this.state.isInvalidated || !this.state.dataUpdatedAt || this.observers.some(observer => observer.getCurrentResult().isStale);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
isStaleByTime(staleTime = 0) {
|
|
1112
|
+
return this.state.isInvalidated || !this.state.dataUpdatedAt || !timeUntilStale(this.state.dataUpdatedAt, staleTime);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
onFocus() {
|
|
1116
|
+
var _this$retryer2;
|
|
1117
|
+
|
|
1118
|
+
const observer = this.observers.find(x => x.shouldFetchOnWindowFocus());
|
|
1119
|
+
|
|
1120
|
+
if (observer) {
|
|
1121
|
+
observer.refetch({
|
|
1122
|
+
cancelRefetch: false
|
|
1123
|
+
});
|
|
1124
|
+
} // Continue fetch if currently paused
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
(_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
onOnline() {
|
|
1131
|
+
var _this$retryer3;
|
|
1132
|
+
|
|
1133
|
+
const observer = this.observers.find(x => x.shouldFetchOnReconnect());
|
|
1134
|
+
|
|
1135
|
+
if (observer) {
|
|
1136
|
+
observer.refetch({
|
|
1137
|
+
cancelRefetch: false
|
|
1138
|
+
});
|
|
1139
|
+
} // Continue fetch if currently paused
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
(_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
addObserver(observer) {
|
|
1146
|
+
if (!this.observers.includes(observer)) {
|
|
1147
|
+
this.observers.push(observer); // Stop the query from being garbage collected
|
|
1148
|
+
|
|
1149
|
+
this.clearGcTimeout();
|
|
1150
|
+
this.cache.notify({
|
|
1151
|
+
type: 'observerAdded',
|
|
1152
|
+
query: this,
|
|
1153
|
+
observer
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
removeObserver(observer) {
|
|
1159
|
+
if (this.observers.includes(observer)) {
|
|
1160
|
+
this.observers = this.observers.filter(x => x !== observer);
|
|
1161
|
+
|
|
1162
|
+
if (!this.observers.length) {
|
|
1163
|
+
// If the transport layer does not support cancellation
|
|
1164
|
+
// we'll let the query continue so the result can be cached
|
|
1165
|
+
if (this.retryer) {
|
|
1166
|
+
if (this.abortSignalConsumed) {
|
|
1167
|
+
this.retryer.cancel({
|
|
1168
|
+
revert: true
|
|
1169
|
+
});
|
|
1170
|
+
} else {
|
|
1171
|
+
this.retryer.cancelRetry();
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
this.scheduleGc();
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
this.cache.notify({
|
|
1179
|
+
type: 'observerRemoved',
|
|
1180
|
+
query: this,
|
|
1181
|
+
observer
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
getObserversCount() {
|
|
1187
|
+
return this.observers.length;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
invalidate() {
|
|
1191
|
+
if (!this.state.isInvalidated) {
|
|
1192
|
+
this.dispatch({
|
|
1193
|
+
type: 'invalidate'
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
fetch(options, fetchOptions) {
|
|
1199
|
+
var _this$options$behavio, _context$fetchOptions;
|
|
1200
|
+
|
|
1201
|
+
if (this.state.fetchStatus !== 'idle') {
|
|
1202
|
+
if (this.state.dataUpdatedAt && fetchOptions != null && fetchOptions.cancelRefetch) {
|
|
1203
|
+
// Silently cancel current fetch if the user wants to cancel refetches
|
|
1204
|
+
this.cancel({
|
|
1205
|
+
silent: true
|
|
1206
|
+
});
|
|
1207
|
+
} else if (this.promise) {
|
|
1208
|
+
var _this$retryer4;
|
|
1209
|
+
|
|
1210
|
+
// make sure that retries that were potentially cancelled due to unmounts can continue
|
|
1211
|
+
(_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry(); // Return current promise if we are already fetching
|
|
1212
|
+
|
|
1213
|
+
return this.promise;
|
|
1214
|
+
}
|
|
1215
|
+
} // Update config if passed, otherwise the config from the last execution is used
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
if (options) {
|
|
1219
|
+
this.setOptions(options);
|
|
1220
|
+
} // Use the options from the first observer with a query function if no function is found.
|
|
1221
|
+
// This can happen when the query is hydrated or created with setQueryData.
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
if (!this.options.queryFn) {
|
|
1225
|
+
const observer = this.observers.find(x => x.options.queryFn);
|
|
1226
|
+
|
|
1227
|
+
if (observer) {
|
|
1228
|
+
this.setOptions(observer.options);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1233
|
+
if (!Array.isArray(this.options.queryKey)) {
|
|
1234
|
+
this.logger.error("As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']");
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
const abortController = getAbortController(); // Create query function context
|
|
1239
|
+
|
|
1240
|
+
const queryFnContext = {
|
|
1241
|
+
queryKey: this.queryKey,
|
|
1242
|
+
pageParam: undefined,
|
|
1243
|
+
meta: this.meta
|
|
1244
|
+
}; // Adds an enumerable signal property to the object that
|
|
1245
|
+
// which sets abortSignalConsumed to true when the signal
|
|
1246
|
+
// is read.
|
|
1247
|
+
|
|
1248
|
+
const addSignalProperty = object => {
|
|
1249
|
+
Object.defineProperty(object, 'signal', {
|
|
1250
|
+
enumerable: true,
|
|
1251
|
+
get: () => {
|
|
1252
|
+
if (abortController) {
|
|
1253
|
+
this.abortSignalConsumed = true;
|
|
1254
|
+
return abortController.signal;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
return undefined;
|
|
1258
|
+
}
|
|
1259
|
+
});
|
|
1260
|
+
};
|
|
1261
|
+
|
|
1262
|
+
addSignalProperty(queryFnContext); // Create fetch function
|
|
1263
|
+
|
|
1264
|
+
const fetchFn = () => {
|
|
1265
|
+
if (!this.options.queryFn) {
|
|
1266
|
+
return Promise.reject("Missing queryFn for queryKey '" + this.options.queryHash + "'");
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
this.abortSignalConsumed = false;
|
|
1270
|
+
return this.options.queryFn(queryFnContext);
|
|
1271
|
+
}; // Trigger behavior hook
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
const context = {
|
|
1275
|
+
fetchOptions,
|
|
1276
|
+
options: this.options,
|
|
1277
|
+
queryKey: this.queryKey,
|
|
1278
|
+
state: this.state,
|
|
1279
|
+
fetchFn
|
|
1280
|
+
};
|
|
1281
|
+
addSignalProperty(context);
|
|
1282
|
+
(_this$options$behavio = this.options.behavior) == null ? void 0 : _this$options$behavio.onFetch(context); // Store state in case the current fetch needs to be reverted
|
|
1283
|
+
|
|
1284
|
+
this.revertState = this.state; // Set to fetching state if not already in it
|
|
1285
|
+
|
|
1286
|
+
if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
|
|
1287
|
+
var _context$fetchOptions2;
|
|
1288
|
+
|
|
1289
|
+
this.dispatch({
|
|
1290
|
+
type: 'fetch',
|
|
1291
|
+
meta: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.meta
|
|
1292
|
+
});
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
const onError = error => {
|
|
1296
|
+
// Optimistically update state if needed
|
|
1297
|
+
if (!(isCancelledError(error) && error.silent)) {
|
|
1298
|
+
this.dispatch({
|
|
1299
|
+
type: 'error',
|
|
1300
|
+
error: error
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
if (!isCancelledError(error)) {
|
|
1305
|
+
var _this$cache$config$on, _this$cache$config, _this$cache$config$on2, _this$cache$config2;
|
|
1306
|
+
|
|
1307
|
+
// Notify cache callback
|
|
1308
|
+
(_this$cache$config$on = (_this$cache$config = this.cache.config).onError) == null ? void 0 : _this$cache$config$on.call(_this$cache$config, error, this);
|
|
1309
|
+
(_this$cache$config$on2 = (_this$cache$config2 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on2.call(_this$cache$config2, this.state.data, error, this);
|
|
1310
|
+
|
|
1311
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1312
|
+
this.logger.error(error);
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
if (!this.isFetchingOptimistic) {
|
|
1317
|
+
// Schedule query gc after fetching
|
|
1318
|
+
this.scheduleGc();
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
this.isFetchingOptimistic = false;
|
|
1322
|
+
}; // Try to fetch the data
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
this.retryer = createRetryer({
|
|
1326
|
+
fn: context.fetchFn,
|
|
1327
|
+
abort: abortController == null ? void 0 : abortController.abort.bind(abortController),
|
|
1328
|
+
onSuccess: data => {
|
|
1329
|
+
var _this$cache$config$on3, _this$cache$config3, _this$cache$config$on4, _this$cache$config4;
|
|
1330
|
+
|
|
1331
|
+
if (typeof data === 'undefined') {
|
|
1332
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1333
|
+
this.logger.error("Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: " + this.queryHash);
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
onError(new Error(this.queryHash + " data is undefined"));
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
this.setData(data); // Notify cache callback
|
|
1341
|
+
|
|
1342
|
+
(_this$cache$config$on3 = (_this$cache$config3 = this.cache.config).onSuccess) == null ? void 0 : _this$cache$config$on3.call(_this$cache$config3, data, this);
|
|
1343
|
+
(_this$cache$config$on4 = (_this$cache$config4 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on4.call(_this$cache$config4, data, this.state.error, this);
|
|
1344
|
+
|
|
1345
|
+
if (!this.isFetchingOptimistic) {
|
|
1346
|
+
// Schedule query gc after fetching
|
|
1347
|
+
this.scheduleGc();
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
this.isFetchingOptimistic = false;
|
|
1351
|
+
},
|
|
1352
|
+
onError,
|
|
1353
|
+
onFail: (failureCount, error) => {
|
|
1354
|
+
this.dispatch({
|
|
1355
|
+
type: 'failed',
|
|
1356
|
+
failureCount,
|
|
1357
|
+
error
|
|
1358
|
+
});
|
|
1359
|
+
},
|
|
1360
|
+
onPause: () => {
|
|
1361
|
+
this.dispatch({
|
|
1362
|
+
type: 'pause'
|
|
1363
|
+
});
|
|
1364
|
+
},
|
|
1365
|
+
onContinue: () => {
|
|
1366
|
+
this.dispatch({
|
|
1367
|
+
type: 'continue'
|
|
1368
|
+
});
|
|
1369
|
+
},
|
|
1370
|
+
retry: context.options.retry,
|
|
1371
|
+
retryDelay: context.options.retryDelay,
|
|
1372
|
+
networkMode: context.options.networkMode
|
|
1373
|
+
});
|
|
1374
|
+
this.promise = this.retryer.promise;
|
|
1375
|
+
return this.promise;
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
dispatch(action) {
|
|
1379
|
+
const reducer = state => {
|
|
1380
|
+
var _action$meta, _action$dataUpdatedAt;
|
|
1381
|
+
|
|
1382
|
+
switch (action.type) {
|
|
1383
|
+
case 'failed':
|
|
1384
|
+
return { ...state,
|
|
1385
|
+
fetchFailureCount: action.failureCount,
|
|
1386
|
+
fetchFailureReason: action.error
|
|
1387
|
+
};
|
|
1388
|
+
|
|
1389
|
+
case 'pause':
|
|
1390
|
+
return { ...state,
|
|
1391
|
+
fetchStatus: 'paused'
|
|
1392
|
+
};
|
|
1393
|
+
|
|
1394
|
+
case 'continue':
|
|
1395
|
+
return { ...state,
|
|
1396
|
+
fetchStatus: 'fetching'
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
case 'fetch':
|
|
1400
|
+
return { ...state,
|
|
1401
|
+
fetchFailureCount: 0,
|
|
1402
|
+
fetchFailureReason: null,
|
|
1403
|
+
fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
|
|
1404
|
+
fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused',
|
|
1405
|
+
...(!state.dataUpdatedAt && {
|
|
1406
|
+
error: null,
|
|
1407
|
+
status: 'loading'
|
|
1408
|
+
})
|
|
1409
|
+
};
|
|
1410
|
+
|
|
1411
|
+
case 'success':
|
|
1412
|
+
return { ...state,
|
|
1413
|
+
data: action.data,
|
|
1414
|
+
dataUpdateCount: state.dataUpdateCount + 1,
|
|
1415
|
+
dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
|
|
1416
|
+
error: null,
|
|
1417
|
+
isInvalidated: false,
|
|
1418
|
+
status: 'success',
|
|
1419
|
+
...(!action.manual && {
|
|
1420
|
+
fetchStatus: 'idle',
|
|
1421
|
+
fetchFailureCount: 0,
|
|
1422
|
+
fetchFailureReason: null
|
|
1423
|
+
})
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
case 'error':
|
|
1427
|
+
const error = action.error;
|
|
1428
|
+
|
|
1429
|
+
if (isCancelledError(error) && error.revert && this.revertState) {
|
|
1430
|
+
return { ...this.revertState,
|
|
1431
|
+
fetchStatus: 'idle'
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
return { ...state,
|
|
1436
|
+
error: error,
|
|
1437
|
+
errorUpdateCount: state.errorUpdateCount + 1,
|
|
1438
|
+
errorUpdatedAt: Date.now(),
|
|
1439
|
+
fetchFailureCount: state.fetchFailureCount + 1,
|
|
1440
|
+
fetchFailureReason: error,
|
|
1441
|
+
fetchStatus: 'idle',
|
|
1442
|
+
status: 'error'
|
|
1443
|
+
};
|
|
1444
|
+
|
|
1445
|
+
case 'invalidate':
|
|
1446
|
+
return { ...state,
|
|
1447
|
+
isInvalidated: true
|
|
1448
|
+
};
|
|
1449
|
+
|
|
1450
|
+
case 'setState':
|
|
1451
|
+
return { ...state,
|
|
1452
|
+
...action.state
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
};
|
|
1456
|
+
|
|
1457
|
+
this.state = reducer(this.state);
|
|
1458
|
+
notifyManager.batch(() => {
|
|
1459
|
+
this.observers.forEach(observer => {
|
|
1460
|
+
observer.onQueryUpdate(action);
|
|
1461
|
+
});
|
|
1462
|
+
this.cache.notify({
|
|
1463
|
+
query: this,
|
|
1464
|
+
type: 'updated',
|
|
1465
|
+
action
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
function getDefaultState$1(options) {
|
|
1473
|
+
const data = typeof options.initialData === 'function' ? options.initialData() : options.initialData;
|
|
1474
|
+
const hasData = typeof data !== 'undefined';
|
|
1475
|
+
const initialDataUpdatedAt = hasData ? typeof options.initialDataUpdatedAt === 'function' ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
|
|
1476
|
+
return {
|
|
1477
|
+
data,
|
|
1478
|
+
dataUpdateCount: 0,
|
|
1479
|
+
dataUpdatedAt: hasData ? initialDataUpdatedAt != null ? initialDataUpdatedAt : Date.now() : 0,
|
|
1480
|
+
error: null,
|
|
1481
|
+
errorUpdateCount: 0,
|
|
1482
|
+
errorUpdatedAt: 0,
|
|
1483
|
+
fetchFailureCount: 0,
|
|
1484
|
+
fetchFailureReason: null,
|
|
1485
|
+
fetchMeta: null,
|
|
1486
|
+
isInvalidated: false,
|
|
1487
|
+
status: hasData ? 'success' : 'loading',
|
|
1488
|
+
fetchStatus: 'idle'
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// CLASS
|
|
1493
|
+
class QueryCache extends Subscribable {
|
|
1494
|
+
constructor(config) {
|
|
1495
|
+
super();
|
|
1496
|
+
this.config = config || {};
|
|
1497
|
+
this.queries = [];
|
|
1498
|
+
this.queriesMap = {};
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
build(client, options, state) {
|
|
1502
|
+
var _options$queryHash;
|
|
1503
|
+
|
|
1504
|
+
const queryKey = options.queryKey;
|
|
1505
|
+
const queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : hashQueryKeyByOptions(queryKey, options);
|
|
1506
|
+
let query = this.get(queryHash);
|
|
1507
|
+
|
|
1508
|
+
if (!query) {
|
|
1509
|
+
query = new Query({
|
|
1510
|
+
cache: this,
|
|
1511
|
+
logger: client.getLogger(),
|
|
1512
|
+
queryKey,
|
|
1513
|
+
queryHash,
|
|
1514
|
+
options: client.defaultQueryOptions(options),
|
|
1515
|
+
state,
|
|
1516
|
+
defaultOptions: client.getQueryDefaults(queryKey)
|
|
1517
|
+
});
|
|
1518
|
+
this.add(query);
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
return query;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
add(query) {
|
|
1525
|
+
if (!this.queriesMap[query.queryHash]) {
|
|
1526
|
+
this.queriesMap[query.queryHash] = query;
|
|
1527
|
+
this.queries.push(query);
|
|
1528
|
+
this.notify({
|
|
1529
|
+
type: 'added',
|
|
1530
|
+
query
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
remove(query) {
|
|
1536
|
+
const queryInMap = this.queriesMap[query.queryHash];
|
|
1537
|
+
|
|
1538
|
+
if (queryInMap) {
|
|
1539
|
+
query.destroy();
|
|
1540
|
+
this.queries = this.queries.filter(x => x !== query);
|
|
1541
|
+
|
|
1542
|
+
if (queryInMap === query) {
|
|
1543
|
+
delete this.queriesMap[query.queryHash];
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
this.notify({
|
|
1547
|
+
type: 'removed',
|
|
1548
|
+
query
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
clear() {
|
|
1554
|
+
notifyManager.batch(() => {
|
|
1555
|
+
this.queries.forEach(query => {
|
|
1556
|
+
this.remove(query);
|
|
1557
|
+
});
|
|
1558
|
+
});
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
get(queryHash) {
|
|
1562
|
+
return this.queriesMap[queryHash];
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
getAll() {
|
|
1566
|
+
return this.queries;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
find(arg1, arg2) {
|
|
1570
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
1571
|
+
|
|
1572
|
+
if (typeof filters.exact === 'undefined') {
|
|
1573
|
+
filters.exact = true;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
return this.queries.find(query => matchQuery(filters, query));
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
findAll(arg1, arg2) {
|
|
1580
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
1581
|
+
return Object.keys(filters).length > 0 ? this.queries.filter(query => matchQuery(filters, query)) : this.queries;
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
notify(event) {
|
|
1585
|
+
notifyManager.batch(() => {
|
|
1586
|
+
this.listeners.forEach(({
|
|
1587
|
+
listener
|
|
1588
|
+
}) => {
|
|
1589
|
+
listener(event);
|
|
1590
|
+
});
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
onFocus() {
|
|
1595
|
+
notifyManager.batch(() => {
|
|
1596
|
+
this.queries.forEach(query => {
|
|
1597
|
+
query.onFocus();
|
|
1598
|
+
});
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
onOnline() {
|
|
1603
|
+
notifyManager.batch(() => {
|
|
1604
|
+
this.queries.forEach(query => {
|
|
1605
|
+
query.onOnline();
|
|
1606
|
+
});
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// CLASS
|
|
1613
|
+
class Mutation extends Removable {
|
|
1614
|
+
constructor(config) {
|
|
1615
|
+
super();
|
|
1616
|
+
this.defaultOptions = config.defaultOptions;
|
|
1617
|
+
this.mutationId = config.mutationId;
|
|
1618
|
+
this.mutationCache = config.mutationCache;
|
|
1619
|
+
this.logger = config.logger || defaultLogger;
|
|
1620
|
+
this.observers = [];
|
|
1621
|
+
this.state = config.state || getDefaultState();
|
|
1622
|
+
this.setOptions(config.options);
|
|
1623
|
+
this.scheduleGc();
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
setOptions(options) {
|
|
1627
|
+
this.options = { ...this.defaultOptions,
|
|
1628
|
+
...options
|
|
1629
|
+
};
|
|
1630
|
+
this.updateCacheTime(this.options.cacheTime);
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
get meta() {
|
|
1634
|
+
return this.options.meta;
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
setState(state) {
|
|
1638
|
+
this.dispatch({
|
|
1639
|
+
type: 'setState',
|
|
1640
|
+
state
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
addObserver(observer) {
|
|
1645
|
+
if (!this.observers.includes(observer)) {
|
|
1646
|
+
this.observers.push(observer); // Stop the mutation from being garbage collected
|
|
1647
|
+
|
|
1648
|
+
this.clearGcTimeout();
|
|
1649
|
+
this.mutationCache.notify({
|
|
1650
|
+
type: 'observerAdded',
|
|
1651
|
+
mutation: this,
|
|
1652
|
+
observer
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
removeObserver(observer) {
|
|
1658
|
+
this.observers = this.observers.filter(x => x !== observer);
|
|
1659
|
+
this.scheduleGc();
|
|
1660
|
+
this.mutationCache.notify({
|
|
1661
|
+
type: 'observerRemoved',
|
|
1662
|
+
mutation: this,
|
|
1663
|
+
observer
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
optionalRemove() {
|
|
1668
|
+
if (!this.observers.length) {
|
|
1669
|
+
if (this.state.status === 'loading') {
|
|
1670
|
+
this.scheduleGc();
|
|
1671
|
+
} else {
|
|
1672
|
+
this.mutationCache.remove(this);
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
continue() {
|
|
1678
|
+
var _this$retryer$continu, _this$retryer;
|
|
1679
|
+
|
|
1680
|
+
return (_this$retryer$continu = (_this$retryer = this.retryer) == null ? void 0 : _this$retryer.continue()) != null ? _this$retryer$continu : this.execute();
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
async execute() {
|
|
1684
|
+
const executeMutation = () => {
|
|
1685
|
+
var _this$options$retry;
|
|
1686
|
+
|
|
1687
|
+
this.retryer = createRetryer({
|
|
1688
|
+
fn: () => {
|
|
1689
|
+
if (!this.options.mutationFn) {
|
|
1690
|
+
return Promise.reject('No mutationFn found');
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
return this.options.mutationFn(this.state.variables);
|
|
1694
|
+
},
|
|
1695
|
+
onFail: (failureCount, error) => {
|
|
1696
|
+
this.dispatch({
|
|
1697
|
+
type: 'failed',
|
|
1698
|
+
failureCount,
|
|
1699
|
+
error
|
|
1700
|
+
});
|
|
1701
|
+
},
|
|
1702
|
+
onPause: () => {
|
|
1703
|
+
this.dispatch({
|
|
1704
|
+
type: 'pause'
|
|
1705
|
+
});
|
|
1706
|
+
},
|
|
1707
|
+
onContinue: () => {
|
|
1708
|
+
this.dispatch({
|
|
1709
|
+
type: 'continue'
|
|
1710
|
+
});
|
|
1711
|
+
},
|
|
1712
|
+
retry: (_this$options$retry = this.options.retry) != null ? _this$options$retry : 0,
|
|
1713
|
+
retryDelay: this.options.retryDelay,
|
|
1714
|
+
networkMode: this.options.networkMode
|
|
1715
|
+
});
|
|
1716
|
+
return this.retryer.promise;
|
|
1717
|
+
};
|
|
1718
|
+
|
|
1719
|
+
const restored = this.state.status === 'loading';
|
|
1720
|
+
|
|
1721
|
+
try {
|
|
1722
|
+
var _this$mutationCache$c3, _this$mutationCache$c4, _this$options$onSucce, _this$options2, _this$mutationCache$c5, _this$mutationCache$c6, _this$options$onSettl, _this$options3;
|
|
1723
|
+
|
|
1724
|
+
if (!restored) {
|
|
1725
|
+
var _this$mutationCache$c, _this$mutationCache$c2, _this$options$onMutat, _this$options;
|
|
1726
|
+
|
|
1727
|
+
this.dispatch({
|
|
1728
|
+
type: 'loading',
|
|
1729
|
+
variables: this.options.variables
|
|
1730
|
+
}); // Notify cache callback
|
|
1731
|
+
|
|
1732
|
+
await ((_this$mutationCache$c = (_this$mutationCache$c2 = this.mutationCache.config).onMutate) == null ? void 0 : _this$mutationCache$c.call(_this$mutationCache$c2, this.state.variables, this));
|
|
1733
|
+
const context = await ((_this$options$onMutat = (_this$options = this.options).onMutate) == null ? void 0 : _this$options$onMutat.call(_this$options, this.state.variables));
|
|
1734
|
+
|
|
1735
|
+
if (context !== this.state.context) {
|
|
1736
|
+
this.dispatch({
|
|
1737
|
+
type: 'loading',
|
|
1738
|
+
context,
|
|
1739
|
+
variables: this.state.variables
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
const data = await executeMutation(); // Notify cache callback
|
|
1745
|
+
|
|
1746
|
+
await ((_this$mutationCache$c3 = (_this$mutationCache$c4 = this.mutationCache.config).onSuccess) == null ? void 0 : _this$mutationCache$c3.call(_this$mutationCache$c4, data, this.state.variables, this.state.context, this));
|
|
1747
|
+
await ((_this$options$onSucce = (_this$options2 = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options2, data, this.state.variables, this.state.context)); // Notify cache callback
|
|
1748
|
+
|
|
1749
|
+
await ((_this$mutationCache$c5 = (_this$mutationCache$c6 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c5.call(_this$mutationCache$c6, data, null, this.state.variables, this.state.context, this));
|
|
1750
|
+
await ((_this$options$onSettl = (_this$options3 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options3, data, null, this.state.variables, this.state.context));
|
|
1751
|
+
this.dispatch({
|
|
1752
|
+
type: 'success',
|
|
1753
|
+
data
|
|
1754
|
+
});
|
|
1755
|
+
return data;
|
|
1756
|
+
} catch (error) {
|
|
1757
|
+
try {
|
|
1758
|
+
var _this$mutationCache$c7, _this$mutationCache$c8, _this$options$onError, _this$options4, _this$mutationCache$c9, _this$mutationCache$c10, _this$options$onSettl2, _this$options5;
|
|
1759
|
+
|
|
1760
|
+
// Notify cache callback
|
|
1761
|
+
await ((_this$mutationCache$c7 = (_this$mutationCache$c8 = this.mutationCache.config).onError) == null ? void 0 : _this$mutationCache$c7.call(_this$mutationCache$c8, error, this.state.variables, this.state.context, this));
|
|
1762
|
+
|
|
1763
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1764
|
+
this.logger.error(error);
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
await ((_this$options$onError = (_this$options4 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options4, error, this.state.variables, this.state.context)); // Notify cache callback
|
|
1768
|
+
|
|
1769
|
+
await ((_this$mutationCache$c9 = (_this$mutationCache$c10 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c9.call(_this$mutationCache$c10, undefined, error, this.state.variables, this.state.context, this));
|
|
1770
|
+
await ((_this$options$onSettl2 = (_this$options5 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options5, undefined, error, this.state.variables, this.state.context));
|
|
1771
|
+
throw error;
|
|
1772
|
+
} finally {
|
|
1773
|
+
this.dispatch({
|
|
1774
|
+
type: 'error',
|
|
1775
|
+
error: error
|
|
1776
|
+
});
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
dispatch(action) {
|
|
1782
|
+
const reducer = state => {
|
|
1783
|
+
switch (action.type) {
|
|
1784
|
+
case 'failed':
|
|
1785
|
+
return { ...state,
|
|
1786
|
+
failureCount: action.failureCount,
|
|
1787
|
+
failureReason: action.error
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1790
|
+
case 'pause':
|
|
1791
|
+
return { ...state,
|
|
1792
|
+
isPaused: true
|
|
1793
|
+
};
|
|
1794
|
+
|
|
1795
|
+
case 'continue':
|
|
1796
|
+
return { ...state,
|
|
1797
|
+
isPaused: false
|
|
1798
|
+
};
|
|
1799
|
+
|
|
1800
|
+
case 'loading':
|
|
1801
|
+
return { ...state,
|
|
1802
|
+
context: action.context,
|
|
1803
|
+
data: undefined,
|
|
1804
|
+
failureCount: 0,
|
|
1805
|
+
failureReason: null,
|
|
1806
|
+
error: null,
|
|
1807
|
+
isPaused: !canFetch(this.options.networkMode),
|
|
1808
|
+
status: 'loading',
|
|
1809
|
+
variables: action.variables
|
|
1810
|
+
};
|
|
1811
|
+
|
|
1812
|
+
case 'success':
|
|
1813
|
+
return { ...state,
|
|
1814
|
+
data: action.data,
|
|
1815
|
+
failureCount: 0,
|
|
1816
|
+
failureReason: null,
|
|
1817
|
+
error: null,
|
|
1818
|
+
status: 'success',
|
|
1819
|
+
isPaused: false
|
|
1820
|
+
};
|
|
1821
|
+
|
|
1822
|
+
case 'error':
|
|
1823
|
+
return { ...state,
|
|
1824
|
+
data: undefined,
|
|
1825
|
+
error: action.error,
|
|
1826
|
+
failureCount: state.failureCount + 1,
|
|
1827
|
+
failureReason: action.error,
|
|
1828
|
+
isPaused: false,
|
|
1829
|
+
status: 'error'
|
|
1830
|
+
};
|
|
1831
|
+
|
|
1832
|
+
case 'setState':
|
|
1833
|
+
return { ...state,
|
|
1834
|
+
...action.state
|
|
1835
|
+
};
|
|
1836
|
+
}
|
|
1837
|
+
};
|
|
1838
|
+
|
|
1839
|
+
this.state = reducer(this.state);
|
|
1840
|
+
notifyManager.batch(() => {
|
|
1841
|
+
this.observers.forEach(observer => {
|
|
1842
|
+
observer.onMutationUpdate(action);
|
|
1843
|
+
});
|
|
1844
|
+
this.mutationCache.notify({
|
|
1845
|
+
mutation: this,
|
|
1846
|
+
type: 'updated',
|
|
1847
|
+
action
|
|
1848
|
+
});
|
|
1849
|
+
});
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
}
|
|
1853
|
+
function getDefaultState() {
|
|
1854
|
+
return {
|
|
1855
|
+
context: undefined,
|
|
1856
|
+
data: undefined,
|
|
1857
|
+
error: null,
|
|
1858
|
+
failureCount: 0,
|
|
1859
|
+
failureReason: null,
|
|
1860
|
+
isPaused: false,
|
|
1861
|
+
status: 'idle',
|
|
1862
|
+
variables: undefined
|
|
1863
|
+
};
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
// CLASS
|
|
1867
|
+
class MutationCache extends Subscribable {
|
|
1868
|
+
constructor(config) {
|
|
1869
|
+
super();
|
|
1870
|
+
this.config = config || {};
|
|
1871
|
+
this.mutations = [];
|
|
1872
|
+
this.mutationId = 0;
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
build(client, options, state) {
|
|
1876
|
+
const mutation = new Mutation({
|
|
1877
|
+
mutationCache: this,
|
|
1878
|
+
logger: client.getLogger(),
|
|
1879
|
+
mutationId: ++this.mutationId,
|
|
1880
|
+
options: client.defaultMutationOptions(options),
|
|
1881
|
+
state,
|
|
1882
|
+
defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined
|
|
1883
|
+
});
|
|
1884
|
+
this.add(mutation);
|
|
1885
|
+
return mutation;
|
|
1886
|
+
}
|
|
1887
|
+
|
|
1888
|
+
add(mutation) {
|
|
1889
|
+
this.mutations.push(mutation);
|
|
1890
|
+
this.notify({
|
|
1891
|
+
type: 'added',
|
|
1892
|
+
mutation
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
remove(mutation) {
|
|
1897
|
+
this.mutations = this.mutations.filter(x => x !== mutation);
|
|
1898
|
+
this.notify({
|
|
1899
|
+
type: 'removed',
|
|
1900
|
+
mutation
|
|
1901
|
+
});
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
clear() {
|
|
1905
|
+
notifyManager.batch(() => {
|
|
1906
|
+
this.mutations.forEach(mutation => {
|
|
1907
|
+
this.remove(mutation);
|
|
1908
|
+
});
|
|
1909
|
+
});
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
getAll() {
|
|
1913
|
+
return this.mutations;
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
find(filters) {
|
|
1917
|
+
if (typeof filters.exact === 'undefined') {
|
|
1918
|
+
filters.exact = true;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
return this.mutations.find(mutation => matchMutation(filters, mutation));
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
findAll(filters) {
|
|
1925
|
+
return this.mutations.filter(mutation => matchMutation(filters, mutation));
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
notify(event) {
|
|
1929
|
+
notifyManager.batch(() => {
|
|
1930
|
+
this.listeners.forEach(({
|
|
1931
|
+
listener
|
|
1932
|
+
}) => {
|
|
1933
|
+
listener(event);
|
|
1934
|
+
});
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
resumePausedMutations() {
|
|
1939
|
+
var _this$resuming;
|
|
1940
|
+
|
|
1941
|
+
this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
|
|
1942
|
+
const pausedMutations = this.mutations.filter(x => x.state.isPaused);
|
|
1943
|
+
return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop$1)), Promise.resolve()));
|
|
1944
|
+
}).then(() => {
|
|
1945
|
+
this.resuming = undefined;
|
|
1946
|
+
});
|
|
1947
|
+
return this.resuming;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
function infiniteQueryBehavior() {
|
|
1953
|
+
return {
|
|
1954
|
+
onFetch: context => {
|
|
1955
|
+
context.fetchFn = () => {
|
|
1956
|
+
var _context$fetchOptions, _context$fetchOptions2, _context$fetchOptions3, _context$fetchOptions4, _context$state$data, _context$state$data2;
|
|
1957
|
+
|
|
1958
|
+
const refetchPage = (_context$fetchOptions = context.fetchOptions) == null ? void 0 : (_context$fetchOptions2 = _context$fetchOptions.meta) == null ? void 0 : _context$fetchOptions2.refetchPage;
|
|
1959
|
+
const fetchMore = (_context$fetchOptions3 = context.fetchOptions) == null ? void 0 : (_context$fetchOptions4 = _context$fetchOptions3.meta) == null ? void 0 : _context$fetchOptions4.fetchMore;
|
|
1960
|
+
const pageParam = fetchMore == null ? void 0 : fetchMore.pageParam;
|
|
1961
|
+
const isFetchingNextPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'forward';
|
|
1962
|
+
const isFetchingPreviousPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'backward';
|
|
1963
|
+
const oldPages = ((_context$state$data = context.state.data) == null ? void 0 : _context$state$data.pages) || [];
|
|
1964
|
+
const oldPageParams = ((_context$state$data2 = context.state.data) == null ? void 0 : _context$state$data2.pageParams) || [];
|
|
1965
|
+
let newPageParams = oldPageParams;
|
|
1966
|
+
let cancelled = false;
|
|
1967
|
+
|
|
1968
|
+
const addSignalProperty = object => {
|
|
1969
|
+
Object.defineProperty(object, 'signal', {
|
|
1970
|
+
enumerable: true,
|
|
1971
|
+
get: () => {
|
|
1972
|
+
var _context$signal;
|
|
1973
|
+
|
|
1974
|
+
if ((_context$signal = context.signal) != null && _context$signal.aborted) {
|
|
1975
|
+
cancelled = true;
|
|
1976
|
+
} else {
|
|
1977
|
+
var _context$signal2;
|
|
1978
|
+
|
|
1979
|
+
(_context$signal2 = context.signal) == null ? void 0 : _context$signal2.addEventListener('abort', () => {
|
|
1980
|
+
cancelled = true;
|
|
1981
|
+
});
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
return context.signal;
|
|
1985
|
+
}
|
|
1986
|
+
});
|
|
1987
|
+
}; // Get query function
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
const queryFn = context.options.queryFn || (() => Promise.reject("Missing queryFn for queryKey '" + context.options.queryHash + "'"));
|
|
1991
|
+
|
|
1992
|
+
const buildNewPages = (pages, param, page, previous) => {
|
|
1993
|
+
newPageParams = previous ? [param, ...newPageParams] : [...newPageParams, param];
|
|
1994
|
+
return previous ? [page, ...pages] : [...pages, page];
|
|
1995
|
+
}; // Create function to fetch a page
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
const fetchPage = (pages, manual, param, previous) => {
|
|
1999
|
+
if (cancelled) {
|
|
2000
|
+
return Promise.reject('Cancelled');
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
if (typeof param === 'undefined' && !manual && pages.length) {
|
|
2004
|
+
return Promise.resolve(pages);
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
const queryFnContext = {
|
|
2008
|
+
queryKey: context.queryKey,
|
|
2009
|
+
pageParam: param,
|
|
2010
|
+
meta: context.options.meta
|
|
2011
|
+
};
|
|
2012
|
+
addSignalProperty(queryFnContext);
|
|
2013
|
+
const queryFnResult = queryFn(queryFnContext);
|
|
2014
|
+
const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
|
|
2015
|
+
return promise;
|
|
2016
|
+
};
|
|
2017
|
+
|
|
2018
|
+
let promise; // Fetch first page?
|
|
2019
|
+
|
|
2020
|
+
if (!oldPages.length) {
|
|
2021
|
+
promise = fetchPage([]);
|
|
2022
|
+
} // Fetch next page?
|
|
2023
|
+
else if (isFetchingNextPage) {
|
|
2024
|
+
const manual = typeof pageParam !== 'undefined';
|
|
2025
|
+
const param = manual ? pageParam : getNextPageParam(context.options, oldPages);
|
|
2026
|
+
promise = fetchPage(oldPages, manual, param);
|
|
2027
|
+
} // Fetch previous page?
|
|
2028
|
+
else if (isFetchingPreviousPage) {
|
|
2029
|
+
const manual = typeof pageParam !== 'undefined';
|
|
2030
|
+
const param = manual ? pageParam : getPreviousPageParam(context.options, oldPages);
|
|
2031
|
+
promise = fetchPage(oldPages, manual, param, true);
|
|
2032
|
+
} // Refetch pages
|
|
2033
|
+
else {
|
|
2034
|
+
newPageParams = [];
|
|
2035
|
+
const manual = typeof context.options.getNextPageParam === 'undefined';
|
|
2036
|
+
const shouldFetchFirstPage = refetchPage && oldPages[0] ? refetchPage(oldPages[0], 0, oldPages) : true; // Fetch first page
|
|
2037
|
+
|
|
2038
|
+
promise = shouldFetchFirstPage ? fetchPage([], manual, oldPageParams[0]) : Promise.resolve(buildNewPages([], oldPageParams[0], oldPages[0])); // Fetch remaining pages
|
|
2039
|
+
|
|
2040
|
+
for (let i = 1; i < oldPages.length; i++) {
|
|
2041
|
+
promise = promise.then(pages => {
|
|
2042
|
+
const shouldFetchNextPage = refetchPage && oldPages[i] ? refetchPage(oldPages[i], i, oldPages) : true;
|
|
2043
|
+
|
|
2044
|
+
if (shouldFetchNextPage) {
|
|
2045
|
+
const param = manual ? oldPageParams[i] : getNextPageParam(context.options, pages);
|
|
2046
|
+
return fetchPage(pages, manual, param);
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
return Promise.resolve(buildNewPages(pages, oldPageParams[i], oldPages[i]));
|
|
2050
|
+
});
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
const finalPromise = promise.then(pages => ({
|
|
2055
|
+
pages,
|
|
2056
|
+
pageParams: newPageParams
|
|
2057
|
+
}));
|
|
2058
|
+
return finalPromise;
|
|
2059
|
+
};
|
|
2060
|
+
}
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
2063
|
+
function getNextPageParam(options, pages) {
|
|
2064
|
+
return options.getNextPageParam == null ? void 0 : options.getNextPageParam(pages[pages.length - 1], pages);
|
|
2065
|
+
}
|
|
2066
|
+
function getPreviousPageParam(options, pages) {
|
|
2067
|
+
return options.getPreviousPageParam == null ? void 0 : options.getPreviousPageParam(pages[0], pages);
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
// CLASS
|
|
2071
|
+
class QueryClient {
|
|
2072
|
+
constructor(config = {}) {
|
|
2073
|
+
this.queryCache = config.queryCache || new QueryCache();
|
|
2074
|
+
this.mutationCache = config.mutationCache || new MutationCache();
|
|
2075
|
+
this.logger = config.logger || defaultLogger;
|
|
2076
|
+
this.defaultOptions = config.defaultOptions || {};
|
|
2077
|
+
this.queryDefaults = [];
|
|
2078
|
+
this.mutationDefaults = [];
|
|
2079
|
+
this.mountCount = 0;
|
|
2080
|
+
|
|
2081
|
+
if (process.env.NODE_ENV !== 'production' && config.logger) {
|
|
2082
|
+
this.logger.error("Passing a custom logger has been deprecated and will be removed in the next major version.");
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
mount() {
|
|
2087
|
+
this.mountCount++;
|
|
2088
|
+
if (this.mountCount !== 1) return;
|
|
2089
|
+
this.unsubscribeFocus = focusManager.subscribe(() => {
|
|
2090
|
+
if (focusManager.isFocused()) {
|
|
2091
|
+
this.resumePausedMutations();
|
|
2092
|
+
this.queryCache.onFocus();
|
|
2093
|
+
}
|
|
2094
|
+
});
|
|
2095
|
+
this.unsubscribeOnline = onlineManager.subscribe(() => {
|
|
2096
|
+
if (onlineManager.isOnline()) {
|
|
2097
|
+
this.resumePausedMutations();
|
|
2098
|
+
this.queryCache.onOnline();
|
|
2099
|
+
}
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
unmount() {
|
|
2104
|
+
var _this$unsubscribeFocu, _this$unsubscribeOnli;
|
|
2105
|
+
|
|
2106
|
+
this.mountCount--;
|
|
2107
|
+
if (this.mountCount !== 0) return;
|
|
2108
|
+
(_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this);
|
|
2109
|
+
this.unsubscribeFocus = undefined;
|
|
2110
|
+
(_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this);
|
|
2111
|
+
this.unsubscribeOnline = undefined;
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
isFetching(arg1, arg2) {
|
|
2115
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
2116
|
+
filters.fetchStatus = 'fetching';
|
|
2117
|
+
return this.queryCache.findAll(filters).length;
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
isMutating(filters) {
|
|
2121
|
+
return this.mutationCache.findAll({ ...filters,
|
|
2122
|
+
fetching: true
|
|
2123
|
+
}).length;
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
getQueryData(queryKey, filters) {
|
|
2127
|
+
var _this$queryCache$find;
|
|
2128
|
+
|
|
2129
|
+
return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
ensureQueryData(arg1, arg2, arg3) {
|
|
2133
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
2134
|
+
const cachedData = this.getQueryData(parsedOptions.queryKey);
|
|
2135
|
+
return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(parsedOptions);
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
getQueriesData(queryKeyOrFilters) {
|
|
2139
|
+
return this.getQueryCache().findAll(queryKeyOrFilters).map(({
|
|
2140
|
+
queryKey,
|
|
2141
|
+
state
|
|
2142
|
+
}) => {
|
|
2143
|
+
const data = state.data;
|
|
2144
|
+
return [queryKey, data];
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
setQueryData(queryKey, updater, options) {
|
|
2149
|
+
const query = this.queryCache.find(queryKey);
|
|
2150
|
+
const prevData = query == null ? void 0 : query.state.data;
|
|
2151
|
+
const data = functionalUpdate(updater, prevData);
|
|
2152
|
+
|
|
2153
|
+
if (typeof data === 'undefined') {
|
|
2154
|
+
return undefined;
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
const parsedOptions = parseQueryArgs(queryKey);
|
|
2158
|
+
const defaultedOptions = this.defaultQueryOptions(parsedOptions);
|
|
2159
|
+
return this.queryCache.build(this, defaultedOptions).setData(data, { ...options,
|
|
2160
|
+
manual: true
|
|
2161
|
+
});
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
setQueriesData(queryKeyOrFilters, updater, options) {
|
|
2165
|
+
return notifyManager.batch(() => this.getQueryCache().findAll(queryKeyOrFilters).map(({
|
|
2166
|
+
queryKey
|
|
2167
|
+
}) => [queryKey, this.setQueryData(queryKey, updater, options)]));
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
getQueryState(queryKey, filters) {
|
|
2171
|
+
var _this$queryCache$find2;
|
|
2172
|
+
|
|
2173
|
+
return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state;
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
removeQueries(arg1, arg2) {
|
|
2177
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
2178
|
+
const queryCache = this.queryCache;
|
|
2179
|
+
notifyManager.batch(() => {
|
|
2180
|
+
queryCache.findAll(filters).forEach(query => {
|
|
2181
|
+
queryCache.remove(query);
|
|
2182
|
+
});
|
|
2183
|
+
});
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
resetQueries(arg1, arg2, arg3) {
|
|
2187
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2188
|
+
const queryCache = this.queryCache;
|
|
2189
|
+
const refetchFilters = {
|
|
2190
|
+
type: 'active',
|
|
2191
|
+
...filters
|
|
2192
|
+
};
|
|
2193
|
+
return notifyManager.batch(() => {
|
|
2194
|
+
queryCache.findAll(filters).forEach(query => {
|
|
2195
|
+
query.reset();
|
|
2196
|
+
});
|
|
2197
|
+
return this.refetchQueries(refetchFilters, options);
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
cancelQueries(arg1, arg2, arg3) {
|
|
2202
|
+
const [filters, cancelOptions = {}] = parseFilterArgs(arg1, arg2, arg3);
|
|
2203
|
+
|
|
2204
|
+
if (typeof cancelOptions.revert === 'undefined') {
|
|
2205
|
+
cancelOptions.revert = true;
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
const promises = notifyManager.batch(() => this.queryCache.findAll(filters).map(query => query.cancel(cancelOptions)));
|
|
2209
|
+
return Promise.all(promises).then(noop$1).catch(noop$1);
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
invalidateQueries(arg1, arg2, arg3) {
|
|
2213
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2214
|
+
return notifyManager.batch(() => {
|
|
2215
|
+
var _ref, _filters$refetchType;
|
|
2216
|
+
|
|
2217
|
+
this.queryCache.findAll(filters).forEach(query => {
|
|
2218
|
+
query.invalidate();
|
|
2219
|
+
});
|
|
2220
|
+
|
|
2221
|
+
if (filters.refetchType === 'none') {
|
|
2222
|
+
return Promise.resolve();
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
const refetchFilters = { ...filters,
|
|
2226
|
+
type: (_ref = (_filters$refetchType = filters.refetchType) != null ? _filters$refetchType : filters.type) != null ? _ref : 'active'
|
|
2227
|
+
};
|
|
2228
|
+
return this.refetchQueries(refetchFilters, options);
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
refetchQueries(arg1, arg2, arg3) {
|
|
2233
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2234
|
+
const promises = notifyManager.batch(() => this.queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
|
|
2235
|
+
var _options$cancelRefetc;
|
|
2236
|
+
|
|
2237
|
+
return query.fetch(undefined, { ...options,
|
|
2238
|
+
cancelRefetch: (_options$cancelRefetc = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc : true,
|
|
2239
|
+
meta: {
|
|
2240
|
+
refetchPage: filters.refetchPage
|
|
2241
|
+
}
|
|
2242
|
+
});
|
|
2243
|
+
}));
|
|
2244
|
+
let promise = Promise.all(promises).then(noop$1);
|
|
2245
|
+
|
|
2246
|
+
if (!(options != null && options.throwOnError)) {
|
|
2247
|
+
promise = promise.catch(noop$1);
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
return promise;
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
fetchQuery(arg1, arg2, arg3) {
|
|
2254
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
2255
|
+
const defaultedOptions = this.defaultQueryOptions(parsedOptions); // https://github.com/tannerlinsley/react-query/issues/652
|
|
2256
|
+
|
|
2257
|
+
if (typeof defaultedOptions.retry === 'undefined') {
|
|
2258
|
+
defaultedOptions.retry = false;
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
const query = this.queryCache.build(this, defaultedOptions);
|
|
2262
|
+
return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
prefetchQuery(arg1, arg2, arg3) {
|
|
2266
|
+
return this.fetchQuery(arg1, arg2, arg3).then(noop$1).catch(noop$1);
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
fetchInfiniteQuery(arg1, arg2, arg3) {
|
|
2270
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
2271
|
+
parsedOptions.behavior = infiniteQueryBehavior();
|
|
2272
|
+
return this.fetchQuery(parsedOptions);
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
prefetchInfiniteQuery(arg1, arg2, arg3) {
|
|
2276
|
+
return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop$1).catch(noop$1);
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
resumePausedMutations() {
|
|
2280
|
+
return this.mutationCache.resumePausedMutations();
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
getQueryCache() {
|
|
2284
|
+
return this.queryCache;
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
getMutationCache() {
|
|
2288
|
+
return this.mutationCache;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
getLogger() {
|
|
2292
|
+
return this.logger;
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
getDefaultOptions() {
|
|
2296
|
+
return this.defaultOptions;
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
setDefaultOptions(options) {
|
|
2300
|
+
this.defaultOptions = options;
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
setQueryDefaults(queryKey, options) {
|
|
2304
|
+
const result = this.queryDefaults.find(x => hashQueryKey(queryKey) === hashQueryKey(x.queryKey));
|
|
2305
|
+
|
|
2306
|
+
if (result) {
|
|
2307
|
+
result.defaultOptions = options;
|
|
2308
|
+
} else {
|
|
2309
|
+
this.queryDefaults.push({
|
|
2310
|
+
queryKey,
|
|
2311
|
+
defaultOptions: options
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
getQueryDefaults(queryKey) {
|
|
2317
|
+
if (!queryKey) {
|
|
2318
|
+
return undefined;
|
|
2319
|
+
} // Get the first matching defaults
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
const firstMatchingDefaults = this.queryDefaults.find(x => partialMatchKey(queryKey, x.queryKey)); // Additional checks and error in dev mode
|
|
2323
|
+
|
|
2324
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
2325
|
+
// Retrieve all matching defaults for the given key
|
|
2326
|
+
const matchingDefaults = this.queryDefaults.filter(x => partialMatchKey(queryKey, x.queryKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
2327
|
+
|
|
2328
|
+
if (matchingDefaults.length > 1) {
|
|
2329
|
+
this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
setMutationDefaults(mutationKey, options) {
|
|
2337
|
+
const result = this.mutationDefaults.find(x => hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey));
|
|
2338
|
+
|
|
2339
|
+
if (result) {
|
|
2340
|
+
result.defaultOptions = options;
|
|
2341
|
+
} else {
|
|
2342
|
+
this.mutationDefaults.push({
|
|
2343
|
+
mutationKey,
|
|
2344
|
+
defaultOptions: options
|
|
2345
|
+
});
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
|
|
2349
|
+
getMutationDefaults(mutationKey) {
|
|
2350
|
+
if (!mutationKey) {
|
|
2351
|
+
return undefined;
|
|
2352
|
+
} // Get the first matching defaults
|
|
2353
|
+
|
|
2354
|
+
|
|
2355
|
+
const firstMatchingDefaults = this.mutationDefaults.find(x => partialMatchKey(mutationKey, x.mutationKey)); // Additional checks and error in dev mode
|
|
2356
|
+
|
|
2357
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
2358
|
+
// Retrieve all matching defaults for the given key
|
|
2359
|
+
const matchingDefaults = this.mutationDefaults.filter(x => partialMatchKey(mutationKey, x.mutationKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
2360
|
+
|
|
2361
|
+
if (matchingDefaults.length > 1) {
|
|
2362
|
+
this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
defaultQueryOptions(options) {
|
|
2370
|
+
if (options != null && options._defaulted) {
|
|
2371
|
+
return options;
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
const defaultedOptions = { ...this.defaultOptions.queries,
|
|
2375
|
+
...this.getQueryDefaults(options == null ? void 0 : options.queryKey),
|
|
2376
|
+
...options,
|
|
2377
|
+
_defaulted: true
|
|
2378
|
+
};
|
|
2379
|
+
|
|
2380
|
+
if (!defaultedOptions.queryHash && defaultedOptions.queryKey) {
|
|
2381
|
+
defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions);
|
|
2382
|
+
} // dependent default values
|
|
2383
|
+
|
|
2384
|
+
|
|
2385
|
+
if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
|
|
2386
|
+
defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2389
|
+
if (typeof defaultedOptions.useErrorBoundary === 'undefined') {
|
|
2390
|
+
defaultedOptions.useErrorBoundary = !!defaultedOptions.suspense;
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
return defaultedOptions;
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
defaultMutationOptions(options) {
|
|
2397
|
+
if (options != null && options._defaulted) {
|
|
2398
|
+
return options;
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
return { ...this.defaultOptions.mutations,
|
|
2402
|
+
...this.getMutationDefaults(options == null ? void 0 : options.mutationKey),
|
|
2403
|
+
...options,
|
|
2404
|
+
_defaulted: true
|
|
2405
|
+
};
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
clear() {
|
|
2409
|
+
this.queryCache.clear();
|
|
2410
|
+
this.mutationCache.clear();
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
class QueryObserver extends Subscribable {
|
|
2416
|
+
constructor(client, options) {
|
|
2417
|
+
super();
|
|
2418
|
+
this.client = client;
|
|
2419
|
+
this.options = options;
|
|
2420
|
+
this.trackedProps = new Set();
|
|
2421
|
+
this.selectError = null;
|
|
2422
|
+
this.bindMethods();
|
|
2423
|
+
this.setOptions(options);
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
bindMethods() {
|
|
2427
|
+
this.remove = this.remove.bind(this);
|
|
2428
|
+
this.refetch = this.refetch.bind(this);
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
onSubscribe() {
|
|
2432
|
+
if (this.listeners.size === 1) {
|
|
2433
|
+
this.currentQuery.addObserver(this);
|
|
2434
|
+
|
|
2435
|
+
if (shouldFetchOnMount(this.currentQuery, this.options)) {
|
|
2436
|
+
this.executeFetch();
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
this.updateTimers();
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
onUnsubscribe() {
|
|
2444
|
+
if (!this.hasListeners()) {
|
|
2445
|
+
this.destroy();
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
|
|
2449
|
+
shouldFetchOnReconnect() {
|
|
2450
|
+
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
shouldFetchOnWindowFocus() {
|
|
2454
|
+
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
destroy() {
|
|
2458
|
+
this.listeners = new Set();
|
|
2459
|
+
this.clearStaleTimeout();
|
|
2460
|
+
this.clearRefetchInterval();
|
|
2461
|
+
this.currentQuery.removeObserver(this);
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
setOptions(options, notifyOptions) {
|
|
2465
|
+
const prevOptions = this.options;
|
|
2466
|
+
const prevQuery = this.currentQuery;
|
|
2467
|
+
this.options = this.client.defaultQueryOptions(options);
|
|
2468
|
+
|
|
2469
|
+
if (process.env.NODE_ENV !== 'production' && typeof (options == null ? void 0 : options.isDataEqual) !== 'undefined') {
|
|
2470
|
+
this.client.getLogger().error("The isDataEqual option has been deprecated and will be removed in the next major version. You can achieve the same functionality by passing a function as the structuralSharing option");
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
2474
|
+
this.client.getQueryCache().notify({
|
|
2475
|
+
type: 'observerOptionsUpdated',
|
|
2476
|
+
query: this.currentQuery,
|
|
2477
|
+
observer: this
|
|
2478
|
+
});
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
|
|
2482
|
+
throw new Error('Expected enabled to be a boolean');
|
|
2483
|
+
} // Keep previous query key if the user does not supply one
|
|
2484
|
+
|
|
2485
|
+
|
|
2486
|
+
if (!this.options.queryKey) {
|
|
2487
|
+
this.options.queryKey = prevOptions.queryKey;
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
this.updateQuery();
|
|
2491
|
+
const mounted = this.hasListeners(); // Fetch if there are subscribers
|
|
2492
|
+
|
|
2493
|
+
if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
|
|
2494
|
+
this.executeFetch();
|
|
2495
|
+
} // Update result
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
this.updateResult(notifyOptions); // Update stale interval if needed
|
|
2499
|
+
|
|
2500
|
+
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
|
|
2501
|
+
this.updateStaleTimeout();
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
const nextRefetchInterval = this.computeRefetchInterval(); // Update refetch interval if needed
|
|
2505
|
+
|
|
2506
|
+
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
|
|
2507
|
+
this.updateRefetchInterval(nextRefetchInterval);
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
getOptimisticResult(options) {
|
|
2512
|
+
const query = this.client.getQueryCache().build(this.client, options);
|
|
2513
|
+
const result = this.createResult(query, options);
|
|
2514
|
+
|
|
2515
|
+
if (shouldAssignObserverCurrentProperties(this, result, options)) {
|
|
2516
|
+
// this assigns the optimistic result to the current Observer
|
|
2517
|
+
// because if the query function changes, useQuery will be performing
|
|
2518
|
+
// an effect where it would fetch again.
|
|
2519
|
+
// When the fetch finishes, we perform a deep data cloning in order
|
|
2520
|
+
// to reuse objects references. This deep data clone is performed against
|
|
2521
|
+
// the `observer.currentResult.data` property
|
|
2522
|
+
// When QueryKey changes, we refresh the query and get new `optimistic`
|
|
2523
|
+
// result, while we leave the `observer.currentResult`, so when new data
|
|
2524
|
+
// arrives, it finds the old `observer.currentResult` which is related
|
|
2525
|
+
// to the old QueryKey. Which means that currentResult and selectData are
|
|
2526
|
+
// out of sync already.
|
|
2527
|
+
// To solve this, we move the cursor of the currentResult everytime
|
|
2528
|
+
// an observer reads an optimistic value.
|
|
2529
|
+
// When keeping the previous data, the result doesn't change until new
|
|
2530
|
+
// data arrives.
|
|
2531
|
+
this.currentResult = result;
|
|
2532
|
+
this.currentResultOptions = this.options;
|
|
2533
|
+
this.currentResultState = this.currentQuery.state;
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
return result;
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
getCurrentResult() {
|
|
2540
|
+
return this.currentResult;
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
trackResult(result) {
|
|
2544
|
+
const trackedResult = {};
|
|
2545
|
+
Object.keys(result).forEach(key => {
|
|
2546
|
+
Object.defineProperty(trackedResult, key, {
|
|
2547
|
+
configurable: false,
|
|
2548
|
+
enumerable: true,
|
|
2549
|
+
get: () => {
|
|
2550
|
+
this.trackedProps.add(key);
|
|
2551
|
+
return result[key];
|
|
2552
|
+
}
|
|
2553
|
+
});
|
|
2554
|
+
});
|
|
2555
|
+
return trackedResult;
|
|
2556
|
+
}
|
|
2557
|
+
|
|
2558
|
+
getCurrentQuery() {
|
|
2559
|
+
return this.currentQuery;
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
remove() {
|
|
2563
|
+
this.client.getQueryCache().remove(this.currentQuery);
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
refetch({
|
|
2567
|
+
refetchPage,
|
|
2568
|
+
...options
|
|
2569
|
+
} = {}) {
|
|
2570
|
+
return this.fetch({ ...options,
|
|
2571
|
+
meta: {
|
|
2572
|
+
refetchPage
|
|
2573
|
+
}
|
|
2574
|
+
});
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
fetchOptimistic(options) {
|
|
2578
|
+
const defaultedOptions = this.client.defaultQueryOptions(options);
|
|
2579
|
+
const query = this.client.getQueryCache().build(this.client, defaultedOptions);
|
|
2580
|
+
query.isFetchingOptimistic = true;
|
|
2581
|
+
return query.fetch().then(() => this.createResult(query, defaultedOptions));
|
|
2582
|
+
}
|
|
2583
|
+
|
|
2584
|
+
fetch(fetchOptions) {
|
|
2585
|
+
var _fetchOptions$cancelR;
|
|
2586
|
+
|
|
2587
|
+
return this.executeFetch({ ...fetchOptions,
|
|
2588
|
+
cancelRefetch: (_fetchOptions$cancelR = fetchOptions.cancelRefetch) != null ? _fetchOptions$cancelR : true
|
|
2589
|
+
}).then(() => {
|
|
2590
|
+
this.updateResult();
|
|
2591
|
+
return this.currentResult;
|
|
2592
|
+
});
|
|
2593
|
+
}
|
|
2594
|
+
|
|
2595
|
+
executeFetch(fetchOptions) {
|
|
2596
|
+
// Make sure we reference the latest query as the current one might have been removed
|
|
2597
|
+
this.updateQuery(); // Fetch
|
|
2598
|
+
|
|
2599
|
+
let promise = this.currentQuery.fetch(this.options, fetchOptions);
|
|
2600
|
+
|
|
2601
|
+
if (!(fetchOptions != null && fetchOptions.throwOnError)) {
|
|
2602
|
+
promise = promise.catch(noop$1);
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
return promise;
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
updateStaleTimeout() {
|
|
2609
|
+
this.clearStaleTimeout();
|
|
2610
|
+
|
|
2611
|
+
if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
|
|
2612
|
+
return;
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
const time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime); // The timeout is sometimes triggered 1 ms before the stale time expiration.
|
|
2616
|
+
// To mitigate this issue we always add 1 ms to the timeout.
|
|
2617
|
+
|
|
2618
|
+
const timeout = time + 1;
|
|
2619
|
+
this.staleTimeoutId = setTimeout(() => {
|
|
2620
|
+
if (!this.currentResult.isStale) {
|
|
2621
|
+
this.updateResult();
|
|
2622
|
+
}
|
|
2623
|
+
}, timeout);
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
computeRefetchInterval() {
|
|
2627
|
+
var _this$options$refetch;
|
|
2628
|
+
|
|
2629
|
+
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;
|
|
2630
|
+
}
|
|
2631
|
+
|
|
2632
|
+
updateRefetchInterval(nextInterval) {
|
|
2633
|
+
this.clearRefetchInterval();
|
|
2634
|
+
this.currentRefetchInterval = nextInterval;
|
|
2635
|
+
|
|
2636
|
+
if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
|
|
2637
|
+
return;
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2640
|
+
this.refetchIntervalId = setInterval(() => {
|
|
2641
|
+
if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
|
|
2642
|
+
this.executeFetch();
|
|
2643
|
+
}
|
|
2644
|
+
}, this.currentRefetchInterval);
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
updateTimers() {
|
|
2648
|
+
this.updateStaleTimeout();
|
|
2649
|
+
this.updateRefetchInterval(this.computeRefetchInterval());
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
clearStaleTimeout() {
|
|
2653
|
+
if (this.staleTimeoutId) {
|
|
2654
|
+
clearTimeout(this.staleTimeoutId);
|
|
2655
|
+
this.staleTimeoutId = undefined;
|
|
2656
|
+
}
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2659
|
+
clearRefetchInterval() {
|
|
2660
|
+
if (this.refetchIntervalId) {
|
|
2661
|
+
clearInterval(this.refetchIntervalId);
|
|
2662
|
+
this.refetchIntervalId = undefined;
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
createResult(query, options) {
|
|
2667
|
+
const prevQuery = this.currentQuery;
|
|
2668
|
+
const prevOptions = this.options;
|
|
2669
|
+
const prevResult = this.currentResult;
|
|
2670
|
+
const prevResultState = this.currentResultState;
|
|
2671
|
+
const prevResultOptions = this.currentResultOptions;
|
|
2672
|
+
const queryChange = query !== prevQuery;
|
|
2673
|
+
const queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
|
|
2674
|
+
const prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
|
|
2675
|
+
const {
|
|
2676
|
+
state
|
|
2677
|
+
} = query;
|
|
2678
|
+
let {
|
|
2679
|
+
dataUpdatedAt,
|
|
2680
|
+
error,
|
|
2681
|
+
errorUpdatedAt,
|
|
2682
|
+
fetchStatus,
|
|
2683
|
+
status
|
|
2684
|
+
} = state;
|
|
2685
|
+
let isPreviousData = false;
|
|
2686
|
+
let isPlaceholderData = false;
|
|
2687
|
+
let data; // Optimistically set result in fetching state if needed
|
|
2688
|
+
|
|
2689
|
+
if (options._optimisticResults) {
|
|
2690
|
+
const mounted = this.hasListeners();
|
|
2691
|
+
const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
|
|
2692
|
+
const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
|
|
2693
|
+
|
|
2694
|
+
if (fetchOnMount || fetchOptionally) {
|
|
2695
|
+
fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
|
|
2696
|
+
|
|
2697
|
+
if (!dataUpdatedAt) {
|
|
2698
|
+
status = 'loading';
|
|
2699
|
+
}
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
if (options._optimisticResults === 'isRestoring') {
|
|
2703
|
+
fetchStatus = 'idle';
|
|
2704
|
+
}
|
|
2705
|
+
} // Keep previous data if needed
|
|
2706
|
+
|
|
2707
|
+
|
|
2708
|
+
if (options.keepPreviousData && !state.dataUpdatedAt && prevQueryResult != null && prevQueryResult.isSuccess && status !== 'error') {
|
|
2709
|
+
data = prevQueryResult.data;
|
|
2710
|
+
dataUpdatedAt = prevQueryResult.dataUpdatedAt;
|
|
2711
|
+
status = prevQueryResult.status;
|
|
2712
|
+
isPreviousData = true;
|
|
2713
|
+
} // Select data if needed
|
|
2714
|
+
else if (options.select && typeof state.data !== 'undefined') {
|
|
2715
|
+
// Memoize select result
|
|
2716
|
+
if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
|
|
2717
|
+
data = this.selectResult;
|
|
2718
|
+
} else {
|
|
2719
|
+
try {
|
|
2720
|
+
this.selectFn = options.select;
|
|
2721
|
+
data = options.select(state.data);
|
|
2722
|
+
data = replaceData(prevResult == null ? void 0 : prevResult.data, data, options);
|
|
2723
|
+
this.selectResult = data;
|
|
2724
|
+
this.selectError = null;
|
|
2725
|
+
} catch (selectError) {
|
|
2726
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
2727
|
+
this.client.getLogger().error(selectError);
|
|
2728
|
+
}
|
|
2729
|
+
|
|
2730
|
+
this.selectError = selectError;
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
} // Use query data
|
|
2734
|
+
else {
|
|
2735
|
+
data = state.data;
|
|
2736
|
+
} // Show placeholder data if needed
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'loading') {
|
|
2740
|
+
let placeholderData; // Memoize placeholder data
|
|
2741
|
+
|
|
2742
|
+
if (prevResult != null && prevResult.isPlaceholderData && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
|
|
2743
|
+
placeholderData = prevResult.data;
|
|
2744
|
+
} else {
|
|
2745
|
+
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData() : options.placeholderData;
|
|
2746
|
+
|
|
2747
|
+
if (options.select && typeof placeholderData !== 'undefined') {
|
|
2748
|
+
try {
|
|
2749
|
+
placeholderData = options.select(placeholderData);
|
|
2750
|
+
this.selectError = null;
|
|
2751
|
+
} catch (selectError) {
|
|
2752
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
2753
|
+
this.client.getLogger().error(selectError);
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
this.selectError = selectError;
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
|
|
2761
|
+
if (typeof placeholderData !== 'undefined') {
|
|
2762
|
+
status = 'success';
|
|
2763
|
+
data = replaceData(prevResult == null ? void 0 : prevResult.data, placeholderData, options);
|
|
2764
|
+
isPlaceholderData = true;
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
if (this.selectError) {
|
|
2769
|
+
error = this.selectError;
|
|
2770
|
+
data = this.selectResult;
|
|
2771
|
+
errorUpdatedAt = Date.now();
|
|
2772
|
+
status = 'error';
|
|
2773
|
+
}
|
|
2774
|
+
|
|
2775
|
+
const isFetching = fetchStatus === 'fetching';
|
|
2776
|
+
const isLoading = status === 'loading';
|
|
2777
|
+
const isError = status === 'error';
|
|
2778
|
+
const result = {
|
|
2779
|
+
status,
|
|
2780
|
+
fetchStatus,
|
|
2781
|
+
isLoading,
|
|
2782
|
+
isSuccess: status === 'success',
|
|
2783
|
+
isError,
|
|
2784
|
+
isInitialLoading: isLoading && isFetching,
|
|
2785
|
+
data,
|
|
2786
|
+
dataUpdatedAt,
|
|
2787
|
+
error,
|
|
2788
|
+
errorUpdatedAt,
|
|
2789
|
+
failureCount: state.fetchFailureCount,
|
|
2790
|
+
failureReason: state.fetchFailureReason,
|
|
2791
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
2792
|
+
isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
|
|
2793
|
+
isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
|
|
2794
|
+
isFetching,
|
|
2795
|
+
isRefetching: isFetching && !isLoading,
|
|
2796
|
+
isLoadingError: isError && state.dataUpdatedAt === 0,
|
|
2797
|
+
isPaused: fetchStatus === 'paused',
|
|
2798
|
+
isPlaceholderData,
|
|
2799
|
+
isPreviousData,
|
|
2800
|
+
isRefetchError: isError && state.dataUpdatedAt !== 0,
|
|
2801
|
+
isStale: isStale(query, options),
|
|
2802
|
+
refetch: this.refetch,
|
|
2803
|
+
remove: this.remove
|
|
2804
|
+
};
|
|
2805
|
+
return result;
|
|
2806
|
+
}
|
|
2807
|
+
|
|
2808
|
+
updateResult(notifyOptions) {
|
|
2809
|
+
const prevResult = this.currentResult;
|
|
2810
|
+
const nextResult = this.createResult(this.currentQuery, this.options);
|
|
2811
|
+
this.currentResultState = this.currentQuery.state;
|
|
2812
|
+
this.currentResultOptions = this.options; // Only notify and update result if something has changed
|
|
2813
|
+
|
|
2814
|
+
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
2815
|
+
return;
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
this.currentResult = nextResult; // Determine which callbacks to trigger
|
|
2819
|
+
|
|
2820
|
+
const defaultNotifyOptions = {
|
|
2821
|
+
cache: true
|
|
2822
|
+
};
|
|
2823
|
+
|
|
2824
|
+
const shouldNotifyListeners = () => {
|
|
2825
|
+
if (!prevResult) {
|
|
2826
|
+
return true;
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
const {
|
|
2830
|
+
notifyOnChangeProps
|
|
2831
|
+
} = this.options;
|
|
2832
|
+
const notifyOnChangePropsValue = typeof notifyOnChangeProps === 'function' ? notifyOnChangeProps() : notifyOnChangeProps;
|
|
2833
|
+
|
|
2834
|
+
if (notifyOnChangePropsValue === 'all' || !notifyOnChangePropsValue && !this.trackedProps.size) {
|
|
2835
|
+
return true;
|
|
2836
|
+
}
|
|
2837
|
+
|
|
2838
|
+
const includedProps = new Set(notifyOnChangePropsValue != null ? notifyOnChangePropsValue : this.trackedProps);
|
|
2839
|
+
|
|
2840
|
+
if (this.options.useErrorBoundary) {
|
|
2841
|
+
includedProps.add('error');
|
|
2842
|
+
}
|
|
2843
|
+
|
|
2844
|
+
return Object.keys(this.currentResult).some(key => {
|
|
2845
|
+
const typedKey = key;
|
|
2846
|
+
const changed = this.currentResult[typedKey] !== prevResult[typedKey];
|
|
2847
|
+
return changed && includedProps.has(typedKey);
|
|
2848
|
+
});
|
|
2849
|
+
};
|
|
2850
|
+
|
|
2851
|
+
if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
|
|
2852
|
+
defaultNotifyOptions.listeners = true;
|
|
2853
|
+
}
|
|
2854
|
+
|
|
2855
|
+
this.notify({ ...defaultNotifyOptions,
|
|
2856
|
+
...notifyOptions
|
|
2857
|
+
});
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
updateQuery() {
|
|
2861
|
+
const query = this.client.getQueryCache().build(this.client, this.options);
|
|
2862
|
+
|
|
2863
|
+
if (query === this.currentQuery) {
|
|
2864
|
+
return;
|
|
2865
|
+
}
|
|
2866
|
+
|
|
2867
|
+
const prevQuery = this.currentQuery;
|
|
2868
|
+
this.currentQuery = query;
|
|
2869
|
+
this.currentQueryInitialState = query.state;
|
|
2870
|
+
this.previousQueryResult = this.currentResult;
|
|
2871
|
+
|
|
2872
|
+
if (this.hasListeners()) {
|
|
2873
|
+
prevQuery == null ? void 0 : prevQuery.removeObserver(this);
|
|
2874
|
+
query.addObserver(this);
|
|
2875
|
+
}
|
|
2876
|
+
}
|
|
2877
|
+
|
|
2878
|
+
onQueryUpdate(action) {
|
|
2879
|
+
const notifyOptions = {};
|
|
2880
|
+
|
|
2881
|
+
if (action.type === 'success') {
|
|
2882
|
+
notifyOptions.onSuccess = !action.manual;
|
|
2883
|
+
} else if (action.type === 'error' && !isCancelledError(action.error)) {
|
|
2884
|
+
notifyOptions.onError = true;
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2887
|
+
this.updateResult(notifyOptions);
|
|
2888
|
+
|
|
2889
|
+
if (this.hasListeners()) {
|
|
2890
|
+
this.updateTimers();
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2894
|
+
notify(notifyOptions) {
|
|
2895
|
+
notifyManager.batch(() => {
|
|
2896
|
+
// First trigger the configuration callbacks
|
|
2897
|
+
if (notifyOptions.onSuccess) {
|
|
2898
|
+
var _this$options$onSucce, _this$options, _this$options$onSettl, _this$options2;
|
|
2899
|
+
|
|
2900
|
+
(_this$options$onSucce = (_this$options = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options, this.currentResult.data);
|
|
2901
|
+
(_this$options$onSettl = (_this$options2 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options2, this.currentResult.data, null);
|
|
2902
|
+
} else if (notifyOptions.onError) {
|
|
2903
|
+
var _this$options$onError, _this$options3, _this$options$onSettl2, _this$options4;
|
|
2904
|
+
|
|
2905
|
+
(_this$options$onError = (_this$options3 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options3, this.currentResult.error);
|
|
2906
|
+
(_this$options$onSettl2 = (_this$options4 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options4, undefined, this.currentResult.error);
|
|
2907
|
+
} // Then trigger the listeners
|
|
2908
|
+
|
|
2909
|
+
|
|
2910
|
+
if (notifyOptions.listeners) {
|
|
2911
|
+
this.listeners.forEach(({
|
|
2912
|
+
listener
|
|
2913
|
+
}) => {
|
|
2914
|
+
listener(this.currentResult);
|
|
2915
|
+
});
|
|
2916
|
+
} // Then the cache listeners
|
|
2917
|
+
|
|
2918
|
+
|
|
2919
|
+
if (notifyOptions.cache) {
|
|
2920
|
+
this.client.getQueryCache().notify({
|
|
2921
|
+
query: this.currentQuery,
|
|
2922
|
+
type: 'observerResultsUpdated'
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
|
|
2928
|
+
}
|
|
2929
|
+
|
|
2930
|
+
function shouldLoadOnMount(query, options) {
|
|
2931
|
+
return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
|
|
2932
|
+
}
|
|
2933
|
+
|
|
2934
|
+
function shouldFetchOnMount(query, options) {
|
|
2935
|
+
return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
|
|
2936
|
+
}
|
|
2937
|
+
|
|
2938
|
+
function shouldFetchOn(query, options, field) {
|
|
2939
|
+
if (options.enabled !== false) {
|
|
2940
|
+
const value = typeof field === 'function' ? field(query) : field;
|
|
2941
|
+
return value === 'always' || value !== false && isStale(query, options);
|
|
2942
|
+
}
|
|
2943
|
+
|
|
2944
|
+
return false;
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
|
|
2948
|
+
return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
|
|
2949
|
+
}
|
|
2950
|
+
|
|
2951
|
+
function isStale(query, options) {
|
|
2952
|
+
return query.isStaleByTime(options.staleTime);
|
|
2953
|
+
} // this function would decide if we will update the observer's 'current'
|
|
2954
|
+
// properties after an optimistic reading via getOptimisticResult
|
|
2955
|
+
|
|
2956
|
+
|
|
2957
|
+
function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
|
|
2958
|
+
// it is important to keep this condition like this for three reasons:
|
|
2959
|
+
// 1. It will get removed in the v5
|
|
2960
|
+
// 2. it reads: don't update the properties if we want to keep the previous
|
|
2961
|
+
// data.
|
|
2962
|
+
// 3. The opposite condition (!options.keepPreviousData) would fallthrough
|
|
2963
|
+
// and will result in a bad decision
|
|
2964
|
+
if (options.keepPreviousData) {
|
|
2965
|
+
return false;
|
|
2966
|
+
} // this means we want to put some placeholder data when pending and queryKey
|
|
2967
|
+
// changed.
|
|
2968
|
+
|
|
2969
|
+
|
|
2970
|
+
if (options.placeholderData !== undefined) {
|
|
2971
|
+
// re-assign properties only if current data is placeholder data
|
|
2972
|
+
// which means that data did not arrive yet, so, if there is some cached data
|
|
2973
|
+
// we need to "prepare" to receive it
|
|
2974
|
+
return optimisticResult.isPlaceholderData;
|
|
2975
|
+
} // if the newly created result isn't what the observer is holding as current,
|
|
2976
|
+
// then we'll need to update the properties as well
|
|
2977
|
+
|
|
2978
|
+
|
|
2979
|
+
if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
|
|
2980
|
+
return true;
|
|
2981
|
+
} // basically, just keep previous properties if nothing changed
|
|
2982
|
+
|
|
2983
|
+
|
|
2984
|
+
return false;
|
|
2985
|
+
}
|
|
2986
|
+
|
|
2987
|
+
class QueriesObserver extends Subscribable {
|
|
2988
|
+
constructor(client, queries) {
|
|
2989
|
+
super();
|
|
2990
|
+
this.client = client;
|
|
2991
|
+
this.queries = [];
|
|
2992
|
+
this.result = [];
|
|
2993
|
+
this.observers = [];
|
|
2994
|
+
this.observersMap = {};
|
|
2995
|
+
|
|
2996
|
+
if (queries) {
|
|
2997
|
+
this.setQueries(queries);
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
|
|
3001
|
+
onSubscribe() {
|
|
3002
|
+
if (this.listeners.size === 1) {
|
|
3003
|
+
this.observers.forEach(observer => {
|
|
3004
|
+
observer.subscribe(result => {
|
|
3005
|
+
this.onUpdate(observer, result);
|
|
3006
|
+
});
|
|
3007
|
+
});
|
|
3008
|
+
}
|
|
3009
|
+
}
|
|
3010
|
+
|
|
3011
|
+
onUnsubscribe() {
|
|
3012
|
+
if (!this.listeners.size) {
|
|
3013
|
+
this.destroy();
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
destroy() {
|
|
3018
|
+
this.listeners = new Set();
|
|
3019
|
+
this.observers.forEach(observer => {
|
|
3020
|
+
observer.destroy();
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
|
|
3024
|
+
setQueries(queries, notifyOptions) {
|
|
3025
|
+
this.queries = queries;
|
|
3026
|
+
notifyManager.batch(() => {
|
|
3027
|
+
const prevObservers = this.observers;
|
|
3028
|
+
const newObserverMatches = this.findMatchingObservers(this.queries); // set options for the new observers to notify of changes
|
|
3029
|
+
|
|
3030
|
+
newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
|
|
3031
|
+
const newObservers = newObserverMatches.map(match => match.observer);
|
|
3032
|
+
const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
|
|
3033
|
+
const newResult = newObservers.map(observer => observer.getCurrentResult());
|
|
3034
|
+
const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
|
|
3035
|
+
|
|
3036
|
+
if (prevObservers.length === newObservers.length && !hasIndexChange) {
|
|
3037
|
+
return;
|
|
3038
|
+
}
|
|
3039
|
+
|
|
3040
|
+
this.observers = newObservers;
|
|
3041
|
+
this.observersMap = newObserversMap;
|
|
3042
|
+
this.result = newResult;
|
|
3043
|
+
|
|
3044
|
+
if (!this.hasListeners()) {
|
|
3045
|
+
return;
|
|
3046
|
+
}
|
|
3047
|
+
|
|
3048
|
+
difference(prevObservers, newObservers).forEach(observer => {
|
|
3049
|
+
observer.destroy();
|
|
3050
|
+
});
|
|
3051
|
+
difference(newObservers, prevObservers).forEach(observer => {
|
|
3052
|
+
observer.subscribe(result => {
|
|
3053
|
+
this.onUpdate(observer, result);
|
|
3054
|
+
});
|
|
3055
|
+
});
|
|
3056
|
+
this.notify();
|
|
3057
|
+
});
|
|
3058
|
+
}
|
|
3059
|
+
|
|
3060
|
+
getCurrentResult() {
|
|
3061
|
+
return this.result;
|
|
3062
|
+
}
|
|
3063
|
+
|
|
3064
|
+
getQueries() {
|
|
3065
|
+
return this.observers.map(observer => observer.getCurrentQuery());
|
|
3066
|
+
}
|
|
3067
|
+
|
|
3068
|
+
getObservers() {
|
|
3069
|
+
return this.observers;
|
|
3070
|
+
}
|
|
3071
|
+
|
|
3072
|
+
getOptimisticResult(queries) {
|
|
3073
|
+
return this.findMatchingObservers(queries).map(match => match.observer.getOptimisticResult(match.defaultedQueryOptions));
|
|
3074
|
+
}
|
|
3075
|
+
|
|
3076
|
+
findMatchingObservers(queries) {
|
|
3077
|
+
const prevObservers = this.observers;
|
|
3078
|
+
const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
|
|
3079
|
+
const defaultedQueryOptions = queries.map(options => this.client.defaultQueryOptions(options));
|
|
3080
|
+
const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
|
|
3081
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash);
|
|
3082
|
+
|
|
3083
|
+
if (match != null) {
|
|
3084
|
+
return [{
|
|
3085
|
+
defaultedQueryOptions: defaultedOptions,
|
|
3086
|
+
observer: match
|
|
3087
|
+
}];
|
|
3088
|
+
}
|
|
3089
|
+
|
|
3090
|
+
return [];
|
|
3091
|
+
});
|
|
3092
|
+
const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
|
|
3093
|
+
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
|
|
3094
|
+
const matchingObserversSet = new Set(matchingObservers.map(match => match.observer));
|
|
3095
|
+
const unmatchedObservers = prevObservers.filter(prevObserver => !matchingObserversSet.has(prevObserver));
|
|
3096
|
+
|
|
3097
|
+
const getObserver = options => {
|
|
3098
|
+
const defaultedOptions = this.client.defaultQueryOptions(options);
|
|
3099
|
+
const currentObserver = this.observersMap[defaultedOptions.queryHash];
|
|
3100
|
+
return currentObserver != null ? currentObserver : new QueryObserver(this.client, defaultedOptions);
|
|
3101
|
+
};
|
|
3102
|
+
|
|
3103
|
+
const newOrReusedObservers = unmatchedQueries.map((options, index) => {
|
|
3104
|
+
if (options.keepPreviousData) {
|
|
3105
|
+
// return previous data from one of the observers that no longer match
|
|
3106
|
+
const previouslyUsedObserver = unmatchedObservers[index];
|
|
3107
|
+
|
|
3108
|
+
if (previouslyUsedObserver !== undefined) {
|
|
3109
|
+
return {
|
|
3110
|
+
defaultedQueryOptions: options,
|
|
3111
|
+
observer: previouslyUsedObserver
|
|
3112
|
+
};
|
|
3113
|
+
}
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
return {
|
|
3117
|
+
defaultedQueryOptions: options,
|
|
3118
|
+
observer: getObserver(options)
|
|
3119
|
+
};
|
|
3120
|
+
});
|
|
3121
|
+
|
|
3122
|
+
const sortMatchesByOrderOfQueries = (a, b) => defaultedQueryOptions.indexOf(a.defaultedQueryOptions) - defaultedQueryOptions.indexOf(b.defaultedQueryOptions);
|
|
3123
|
+
|
|
3124
|
+
return matchingObservers.concat(newOrReusedObservers).sort(sortMatchesByOrderOfQueries);
|
|
3125
|
+
}
|
|
3126
|
+
|
|
3127
|
+
onUpdate(observer, result) {
|
|
3128
|
+
const index = this.observers.indexOf(observer);
|
|
3129
|
+
|
|
3130
|
+
if (index !== -1) {
|
|
3131
|
+
this.result = replaceAt(this.result, index, result);
|
|
3132
|
+
this.notify();
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
|
|
3136
|
+
notify() {
|
|
3137
|
+
notifyManager.batch(() => {
|
|
3138
|
+
this.listeners.forEach(({
|
|
3139
|
+
listener
|
|
3140
|
+
}) => {
|
|
3141
|
+
listener(this.result);
|
|
3142
|
+
});
|
|
3143
|
+
});
|
|
3144
|
+
}
|
|
3145
|
+
|
|
3146
|
+
}
|
|
3147
|
+
|
|
3148
|
+
// CLASS
|
|
3149
|
+
class MutationObserver extends Subscribable {
|
|
3150
|
+
constructor(client, options) {
|
|
3151
|
+
super();
|
|
3152
|
+
this.client = client;
|
|
3153
|
+
this.setOptions(options);
|
|
3154
|
+
this.bindMethods();
|
|
3155
|
+
this.updateResult();
|
|
3156
|
+
}
|
|
3157
|
+
|
|
3158
|
+
bindMethods() {
|
|
3159
|
+
this.mutate = this.mutate.bind(this);
|
|
3160
|
+
this.reset = this.reset.bind(this);
|
|
3161
|
+
}
|
|
3162
|
+
|
|
3163
|
+
setOptions(options) {
|
|
3164
|
+
var _this$currentMutation;
|
|
3165
|
+
|
|
3166
|
+
const prevOptions = this.options;
|
|
3167
|
+
this.options = this.client.defaultMutationOptions(options);
|
|
3168
|
+
|
|
3169
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
3170
|
+
this.client.getMutationCache().notify({
|
|
3171
|
+
type: 'observerOptionsUpdated',
|
|
3172
|
+
mutation: this.currentMutation,
|
|
3173
|
+
observer: this
|
|
3174
|
+
});
|
|
3175
|
+
}
|
|
3176
|
+
|
|
3177
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
3178
|
+
}
|
|
3179
|
+
|
|
3180
|
+
onUnsubscribe() {
|
|
3181
|
+
if (!this.hasListeners()) {
|
|
3182
|
+
var _this$currentMutation2;
|
|
3183
|
+
|
|
3184
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
3187
|
+
|
|
3188
|
+
onMutationUpdate(action) {
|
|
3189
|
+
this.updateResult(); // Determine which callbacks to trigger
|
|
3190
|
+
|
|
3191
|
+
const notifyOptions = {
|
|
3192
|
+
listeners: true
|
|
3193
|
+
};
|
|
3194
|
+
|
|
3195
|
+
if (action.type === 'success') {
|
|
3196
|
+
notifyOptions.onSuccess = true;
|
|
3197
|
+
} else if (action.type === 'error') {
|
|
3198
|
+
notifyOptions.onError = true;
|
|
3199
|
+
}
|
|
3200
|
+
|
|
3201
|
+
this.notify(notifyOptions);
|
|
3202
|
+
}
|
|
3203
|
+
|
|
3204
|
+
getCurrentResult() {
|
|
3205
|
+
return this.currentResult;
|
|
3206
|
+
}
|
|
3207
|
+
|
|
3208
|
+
reset() {
|
|
3209
|
+
this.currentMutation = undefined;
|
|
3210
|
+
this.updateResult();
|
|
3211
|
+
this.notify({
|
|
3212
|
+
listeners: true
|
|
3213
|
+
});
|
|
3214
|
+
}
|
|
3215
|
+
|
|
3216
|
+
mutate(variables, options) {
|
|
3217
|
+
this.mutateOptions = options;
|
|
3218
|
+
|
|
3219
|
+
if (this.currentMutation) {
|
|
3220
|
+
this.currentMutation.removeObserver(this);
|
|
239
3221
|
}
|
|
3222
|
+
|
|
3223
|
+
this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
|
|
3224
|
+
variables: typeof variables !== 'undefined' ? variables : this.options.variables
|
|
3225
|
+
});
|
|
3226
|
+
this.currentMutation.addObserver(this);
|
|
3227
|
+
return this.currentMutation.execute();
|
|
3228
|
+
}
|
|
3229
|
+
|
|
3230
|
+
updateResult() {
|
|
3231
|
+
const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
|
|
3232
|
+
const result = { ...state,
|
|
3233
|
+
isLoading: state.status === 'loading',
|
|
3234
|
+
isSuccess: state.status === 'success',
|
|
3235
|
+
isError: state.status === 'error',
|
|
3236
|
+
isIdle: state.status === 'idle',
|
|
3237
|
+
mutate: this.mutate,
|
|
3238
|
+
reset: this.reset
|
|
3239
|
+
};
|
|
3240
|
+
this.currentResult = result;
|
|
3241
|
+
}
|
|
3242
|
+
|
|
3243
|
+
notify(options) {
|
|
3244
|
+
notifyManager.batch(() => {
|
|
3245
|
+
// First trigger the mutate callbacks
|
|
3246
|
+
if (this.mutateOptions && this.hasListeners()) {
|
|
3247
|
+
if (options.onSuccess) {
|
|
3248
|
+
var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
|
|
3249
|
+
|
|
3250
|
+
(_this$mutateOptions$o = (_this$mutateOptions = this.mutateOptions).onSuccess) == null ? void 0 : _this$mutateOptions$o.call(_this$mutateOptions, this.currentResult.data, this.currentResult.variables, this.currentResult.context);
|
|
3251
|
+
(_this$mutateOptions$o2 = (_this$mutateOptions2 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o2.call(_this$mutateOptions2, this.currentResult.data, null, this.currentResult.variables, this.currentResult.context);
|
|
3252
|
+
} else if (options.onError) {
|
|
3253
|
+
var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
|
|
3254
|
+
|
|
3255
|
+
(_this$mutateOptions$o3 = (_this$mutateOptions3 = this.mutateOptions).onError) == null ? void 0 : _this$mutateOptions$o3.call(_this$mutateOptions3, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
|
|
3256
|
+
(_this$mutateOptions$o4 = (_this$mutateOptions4 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o4.call(_this$mutateOptions4, undefined, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
|
|
3257
|
+
}
|
|
3258
|
+
} // Then trigger the listeners
|
|
3259
|
+
|
|
3260
|
+
|
|
3261
|
+
if (options.listeners) {
|
|
3262
|
+
this.listeners.forEach(({
|
|
3263
|
+
listener
|
|
3264
|
+
}) => {
|
|
3265
|
+
listener(this.currentResult);
|
|
3266
|
+
});
|
|
3267
|
+
}
|
|
3268
|
+
});
|
|
3269
|
+
}
|
|
3270
|
+
|
|
240
3271
|
}
|
|
241
3272
|
|
|
242
3273
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
@@ -270,6 +3301,603 @@ function getAugmentedNamespace(n) {
|
|
|
270
3301
|
return a;
|
|
271
3302
|
}
|
|
272
3303
|
|
|
3304
|
+
var shim = {exports: {}};
|
|
3305
|
+
|
|
3306
|
+
var useSyncExternalStoreShim_production_min = {};
|
|
3307
|
+
|
|
3308
|
+
var require$$0$3 = /*@__PURE__*/getAugmentedNamespace(React__namespace);
|
|
3309
|
+
|
|
3310
|
+
/**
|
|
3311
|
+
* @license React
|
|
3312
|
+
* use-sync-external-store-shim.production.min.js
|
|
3313
|
+
*
|
|
3314
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3315
|
+
*
|
|
3316
|
+
* This source code is licensed under the MIT license found in the
|
|
3317
|
+
* LICENSE file in the root directory of this source tree.
|
|
3318
|
+
*/
|
|
3319
|
+
|
|
3320
|
+
var hasRequiredUseSyncExternalStoreShim_production_min;
|
|
3321
|
+
|
|
3322
|
+
function requireUseSyncExternalStoreShim_production_min () {
|
|
3323
|
+
if (hasRequiredUseSyncExternalStoreShim_production_min) return useSyncExternalStoreShim_production_min;
|
|
3324
|
+
hasRequiredUseSyncExternalStoreShim_production_min = 1;
|
|
3325
|
+
var e=require$$0$3;function h(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var k="function"===typeof Object.is?Object.is:h,l=e.useState,m=e.useEffect,n=e.useLayoutEffect,p=e.useDebugValue;function q(a,b){var d=b(),f=l({inst:{value:d,getSnapshot:b}}),c=f[0].inst,g=f[1];n(function(){c.value=d;c.getSnapshot=b;r(c)&&g({inst:c});},[a,d,b]);m(function(){r(c)&&g({inst:c});return a(function(){r(c)&&g({inst:c});})},[a]);p(d);return d}
|
|
3326
|
+
function r(a){var b=a.getSnapshot;a=a.value;try{var d=b();return !k(a,d)}catch(f){return true}}function t(a,b){return b()}var u="undefined"===typeof window||"undefined"===typeof window.document||"undefined"===typeof window.document.createElement?t:q;useSyncExternalStoreShim_production_min.useSyncExternalStore=void 0!==e.useSyncExternalStore?e.useSyncExternalStore:u;
|
|
3327
|
+
return useSyncExternalStoreShim_production_min;
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
var useSyncExternalStoreShim_development = {};
|
|
3331
|
+
|
|
3332
|
+
/**
|
|
3333
|
+
* @license React
|
|
3334
|
+
* use-sync-external-store-shim.development.js
|
|
3335
|
+
*
|
|
3336
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3337
|
+
*
|
|
3338
|
+
* This source code is licensed under the MIT license found in the
|
|
3339
|
+
* LICENSE file in the root directory of this source tree.
|
|
3340
|
+
*/
|
|
3341
|
+
|
|
3342
|
+
var hasRequiredUseSyncExternalStoreShim_development;
|
|
3343
|
+
|
|
3344
|
+
function requireUseSyncExternalStoreShim_development () {
|
|
3345
|
+
if (hasRequiredUseSyncExternalStoreShim_development) return useSyncExternalStoreShim_development;
|
|
3346
|
+
hasRequiredUseSyncExternalStoreShim_development = 1;
|
|
3347
|
+
|
|
3348
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3349
|
+
(function() {
|
|
3350
|
+
|
|
3351
|
+
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
|
|
3352
|
+
if (
|
|
3353
|
+
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
|
|
3354
|
+
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
|
|
3355
|
+
'function'
|
|
3356
|
+
) {
|
|
3357
|
+
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
|
|
3358
|
+
}
|
|
3359
|
+
var React = require$$0$3;
|
|
3360
|
+
|
|
3361
|
+
var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
3362
|
+
|
|
3363
|
+
function error(format) {
|
|
3364
|
+
{
|
|
3365
|
+
{
|
|
3366
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
3367
|
+
args[_key2 - 1] = arguments[_key2];
|
|
3368
|
+
}
|
|
3369
|
+
|
|
3370
|
+
printWarning('error', format, args);
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3375
|
+
function printWarning(level, format, args) {
|
|
3376
|
+
// When changing this logic, you might want to also
|
|
3377
|
+
// update consoleWithStackDev.www.js as well.
|
|
3378
|
+
{
|
|
3379
|
+
var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
|
|
3380
|
+
var stack = ReactDebugCurrentFrame.getStackAddendum();
|
|
3381
|
+
|
|
3382
|
+
if (stack !== '') {
|
|
3383
|
+
format += '%s';
|
|
3384
|
+
args = args.concat([stack]);
|
|
3385
|
+
} // eslint-disable-next-line react-internal/safe-string-coercion
|
|
3386
|
+
|
|
3387
|
+
|
|
3388
|
+
var argsWithFormat = args.map(function (item) {
|
|
3389
|
+
return String(item);
|
|
3390
|
+
}); // Careful: RN currently depends on this prefix
|
|
3391
|
+
|
|
3392
|
+
argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
|
|
3393
|
+
// breaks IE9: https://github.com/facebook/react/issues/13610
|
|
3394
|
+
// eslint-disable-next-line react-internal/no-production-logging
|
|
3395
|
+
|
|
3396
|
+
Function.prototype.apply.call(console[level], console, argsWithFormat);
|
|
3397
|
+
}
|
|
3398
|
+
}
|
|
3399
|
+
|
|
3400
|
+
/**
|
|
3401
|
+
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
|
3402
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
3403
|
+
*/
|
|
3404
|
+
function is(x, y) {
|
|
3405
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
|
3406
|
+
;
|
|
3407
|
+
}
|
|
3408
|
+
|
|
3409
|
+
var objectIs = typeof Object.is === 'function' ? Object.is : is;
|
|
3410
|
+
|
|
3411
|
+
// dispatch for CommonJS interop named imports.
|
|
3412
|
+
|
|
3413
|
+
var useState = React.useState,
|
|
3414
|
+
useEffect = React.useEffect,
|
|
3415
|
+
useLayoutEffect = React.useLayoutEffect,
|
|
3416
|
+
useDebugValue = React.useDebugValue;
|
|
3417
|
+
var didWarnOld18Alpha = false;
|
|
3418
|
+
var didWarnUncachedGetSnapshot = false; // Disclaimer: This shim breaks many of the rules of React, and only works
|
|
3419
|
+
// because of a very particular set of implementation details and assumptions
|
|
3420
|
+
// -- change any one of them and it will break. The most important assumption
|
|
3421
|
+
// is that updates are always synchronous, because concurrent rendering is
|
|
3422
|
+
// only available in versions of React that also have a built-in
|
|
3423
|
+
// useSyncExternalStore API. And we only use this shim when the built-in API
|
|
3424
|
+
// does not exist.
|
|
3425
|
+
//
|
|
3426
|
+
// Do not assume that the clever hacks used by this hook also work in general.
|
|
3427
|
+
// The point of this shim is to replace the need for hacks by other libraries.
|
|
3428
|
+
|
|
3429
|
+
function useSyncExternalStore(subscribe, getSnapshot, // Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
|
3430
|
+
// React do not expose a way to check if we're hydrating. So users of the shim
|
|
3431
|
+
// will need to track that themselves and return the correct value
|
|
3432
|
+
// from `getSnapshot`.
|
|
3433
|
+
getServerSnapshot) {
|
|
3434
|
+
{
|
|
3435
|
+
if (!didWarnOld18Alpha) {
|
|
3436
|
+
if (React.startTransition !== undefined) {
|
|
3437
|
+
didWarnOld18Alpha = true;
|
|
3438
|
+
|
|
3439
|
+
error('You are using an outdated, pre-release alpha of React 18 that ' + 'does not support useSyncExternalStore. The ' + 'use-sync-external-store shim will not work correctly. Upgrade ' + 'to a newer pre-release.');
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
} // Read the current snapshot from the store on every render. Again, this
|
|
3443
|
+
// breaks the rules of React, and only works here because of specific
|
|
3444
|
+
// implementation details, most importantly that updates are
|
|
3445
|
+
// always synchronous.
|
|
3446
|
+
|
|
3447
|
+
|
|
3448
|
+
var value = getSnapshot();
|
|
3449
|
+
|
|
3450
|
+
{
|
|
3451
|
+
if (!didWarnUncachedGetSnapshot) {
|
|
3452
|
+
var cachedValue = getSnapshot();
|
|
3453
|
+
|
|
3454
|
+
if (!objectIs(value, cachedValue)) {
|
|
3455
|
+
error('The result of getSnapshot should be cached to avoid an infinite loop');
|
|
3456
|
+
|
|
3457
|
+
didWarnUncachedGetSnapshot = true;
|
|
3458
|
+
}
|
|
3459
|
+
}
|
|
3460
|
+
} // Because updates are synchronous, we don't queue them. Instead we force a
|
|
3461
|
+
// re-render whenever the subscribed state changes by updating an some
|
|
3462
|
+
// arbitrary useState hook. Then, during render, we call getSnapshot to read
|
|
3463
|
+
// the current value.
|
|
3464
|
+
//
|
|
3465
|
+
// Because we don't actually use the state returned by the useState hook, we
|
|
3466
|
+
// can save a bit of memory by storing other stuff in that slot.
|
|
3467
|
+
//
|
|
3468
|
+
// To implement the early bailout, we need to track some things on a mutable
|
|
3469
|
+
// object. Usually, we would put that in a useRef hook, but we can stash it in
|
|
3470
|
+
// our useState hook instead.
|
|
3471
|
+
//
|
|
3472
|
+
// To force a re-render, we call forceUpdate({inst}). That works because the
|
|
3473
|
+
// new object always fails an equality check.
|
|
3474
|
+
|
|
3475
|
+
|
|
3476
|
+
var _useState = useState({
|
|
3477
|
+
inst: {
|
|
3478
|
+
value: value,
|
|
3479
|
+
getSnapshot: getSnapshot
|
|
3480
|
+
}
|
|
3481
|
+
}),
|
|
3482
|
+
inst = _useState[0].inst,
|
|
3483
|
+
forceUpdate = _useState[1]; // Track the latest getSnapshot function with a ref. This needs to be updated
|
|
3484
|
+
// in the layout phase so we can access it during the tearing check that
|
|
3485
|
+
// happens on subscribe.
|
|
3486
|
+
|
|
3487
|
+
|
|
3488
|
+
useLayoutEffect(function () {
|
|
3489
|
+
inst.value = value;
|
|
3490
|
+
inst.getSnapshot = getSnapshot; // Whenever getSnapshot or subscribe changes, we need to check in the
|
|
3491
|
+
// commit phase if there was an interleaved mutation. In concurrent mode
|
|
3492
|
+
// this can happen all the time, but even in synchronous mode, an earlier
|
|
3493
|
+
// effect may have mutated the store.
|
|
3494
|
+
|
|
3495
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
3496
|
+
// Force a re-render.
|
|
3497
|
+
forceUpdate({
|
|
3498
|
+
inst: inst
|
|
3499
|
+
});
|
|
3500
|
+
}
|
|
3501
|
+
}, [subscribe, value, getSnapshot]);
|
|
3502
|
+
useEffect(function () {
|
|
3503
|
+
// Check for changes right before subscribing. Subsequent changes will be
|
|
3504
|
+
// detected in the subscription handler.
|
|
3505
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
3506
|
+
// Force a re-render.
|
|
3507
|
+
forceUpdate({
|
|
3508
|
+
inst: inst
|
|
3509
|
+
});
|
|
3510
|
+
}
|
|
3511
|
+
|
|
3512
|
+
var handleStoreChange = function () {
|
|
3513
|
+
// TODO: Because there is no cross-renderer API for batching updates, it's
|
|
3514
|
+
// up to the consumer of this library to wrap their subscription event
|
|
3515
|
+
// with unstable_batchedUpdates. Should we try to detect when this isn't
|
|
3516
|
+
// the case and print a warning in development?
|
|
3517
|
+
// The store changed. Check if the snapshot changed since the last time we
|
|
3518
|
+
// read from the store.
|
|
3519
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
3520
|
+
// Force a re-render.
|
|
3521
|
+
forceUpdate({
|
|
3522
|
+
inst: inst
|
|
3523
|
+
});
|
|
3524
|
+
}
|
|
3525
|
+
}; // Subscribe to the store and return a clean-up function.
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
return subscribe(handleStoreChange);
|
|
3529
|
+
}, [subscribe]);
|
|
3530
|
+
useDebugValue(value);
|
|
3531
|
+
return value;
|
|
3532
|
+
}
|
|
3533
|
+
|
|
3534
|
+
function checkIfSnapshotChanged(inst) {
|
|
3535
|
+
var latestGetSnapshot = inst.getSnapshot;
|
|
3536
|
+
var prevValue = inst.value;
|
|
3537
|
+
|
|
3538
|
+
try {
|
|
3539
|
+
var nextValue = latestGetSnapshot();
|
|
3540
|
+
return !objectIs(prevValue, nextValue);
|
|
3541
|
+
} catch (error) {
|
|
3542
|
+
return true;
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
3545
|
+
|
|
3546
|
+
function useSyncExternalStore$1(subscribe, getSnapshot, getServerSnapshot) {
|
|
3547
|
+
// Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
|
3548
|
+
// React do not expose a way to check if we're hydrating. So users of the shim
|
|
3549
|
+
// will need to track that themselves and return the correct value
|
|
3550
|
+
// from `getSnapshot`.
|
|
3551
|
+
return getSnapshot();
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3554
|
+
var canUseDOM = !!(typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined');
|
|
3555
|
+
|
|
3556
|
+
var isServerEnvironment = !canUseDOM;
|
|
3557
|
+
|
|
3558
|
+
var shim = isServerEnvironment ? useSyncExternalStore$1 : useSyncExternalStore;
|
|
3559
|
+
var useSyncExternalStore$2 = React.useSyncExternalStore !== undefined ? React.useSyncExternalStore : shim;
|
|
3560
|
+
|
|
3561
|
+
useSyncExternalStoreShim_development.useSyncExternalStore = useSyncExternalStore$2;
|
|
3562
|
+
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
|
|
3563
|
+
if (
|
|
3564
|
+
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
|
|
3565
|
+
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
|
|
3566
|
+
'function'
|
|
3567
|
+
) {
|
|
3568
|
+
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
|
|
3569
|
+
}
|
|
3570
|
+
|
|
3571
|
+
})();
|
|
3572
|
+
}
|
|
3573
|
+
return useSyncExternalStoreShim_development;
|
|
3574
|
+
}
|
|
3575
|
+
|
|
3576
|
+
if (process.env.NODE_ENV === 'production') {
|
|
3577
|
+
shim.exports = requireUseSyncExternalStoreShim_production_min();
|
|
3578
|
+
} else {
|
|
3579
|
+
shim.exports = requireUseSyncExternalStoreShim_development();
|
|
3580
|
+
}
|
|
3581
|
+
|
|
3582
|
+
var shimExports = shim.exports;
|
|
3583
|
+
|
|
3584
|
+
const useSyncExternalStore = shimExports.useSyncExternalStore;
|
|
3585
|
+
|
|
3586
|
+
const defaultContext = /*#__PURE__*/React__namespace.createContext(undefined);
|
|
3587
|
+
const QueryClientSharingContext = /*#__PURE__*/React__namespace.createContext(false); // If we are given a context, we will use it.
|
|
3588
|
+
// Otherwise, if contextSharing is on, we share the first and at least one
|
|
3589
|
+
// instance of the context across the window
|
|
3590
|
+
// to ensure that if React Query is used across
|
|
3591
|
+
// different bundles or microfrontends they will
|
|
3592
|
+
// all use the same **instance** of context, regardless
|
|
3593
|
+
// of module scoping.
|
|
3594
|
+
|
|
3595
|
+
function getQueryClientContext(context, contextSharing) {
|
|
3596
|
+
if (context) {
|
|
3597
|
+
return context;
|
|
3598
|
+
}
|
|
3599
|
+
|
|
3600
|
+
if (contextSharing && typeof window !== 'undefined') {
|
|
3601
|
+
if (!window.ReactQueryClientContext) {
|
|
3602
|
+
window.ReactQueryClientContext = defaultContext;
|
|
3603
|
+
}
|
|
3604
|
+
|
|
3605
|
+
return window.ReactQueryClientContext;
|
|
3606
|
+
}
|
|
3607
|
+
|
|
3608
|
+
return defaultContext;
|
|
3609
|
+
}
|
|
3610
|
+
|
|
3611
|
+
const useQueryClient = ({
|
|
3612
|
+
context
|
|
3613
|
+
} = {}) => {
|
|
3614
|
+
const queryClient = React__namespace.useContext(getQueryClientContext(context, React__namespace.useContext(QueryClientSharingContext)));
|
|
3615
|
+
|
|
3616
|
+
if (!queryClient) {
|
|
3617
|
+
throw new Error('No QueryClient set, use QueryClientProvider to set one');
|
|
3618
|
+
}
|
|
3619
|
+
|
|
3620
|
+
return queryClient;
|
|
3621
|
+
};
|
|
3622
|
+
const QueryClientProvider = ({
|
|
3623
|
+
client,
|
|
3624
|
+
children,
|
|
3625
|
+
context,
|
|
3626
|
+
contextSharing = false
|
|
3627
|
+
}) => {
|
|
3628
|
+
React__namespace.useEffect(() => {
|
|
3629
|
+
client.mount();
|
|
3630
|
+
return () => {
|
|
3631
|
+
client.unmount();
|
|
3632
|
+
};
|
|
3633
|
+
}, [client]);
|
|
3634
|
+
|
|
3635
|
+
if (process.env.NODE_ENV !== 'production' && contextSharing) {
|
|
3636
|
+
client.getLogger().error("The contextSharing option has been deprecated and will be removed in the next major version");
|
|
3637
|
+
}
|
|
3638
|
+
|
|
3639
|
+
const Context = getQueryClientContext(context, contextSharing);
|
|
3640
|
+
return /*#__PURE__*/React__namespace.createElement(QueryClientSharingContext.Provider, {
|
|
3641
|
+
value: !context && contextSharing
|
|
3642
|
+
}, /*#__PURE__*/React__namespace.createElement(Context.Provider, {
|
|
3643
|
+
value: client
|
|
3644
|
+
}, children));
|
|
3645
|
+
};
|
|
3646
|
+
|
|
3647
|
+
const IsRestoringContext = /*#__PURE__*/React__namespace.createContext(false);
|
|
3648
|
+
const useIsRestoring = () => React__namespace.useContext(IsRestoringContext);
|
|
3649
|
+
IsRestoringContext.Provider;
|
|
3650
|
+
|
|
3651
|
+
function createValue() {
|
|
3652
|
+
let isReset = false;
|
|
3653
|
+
return {
|
|
3654
|
+
clearReset: () => {
|
|
3655
|
+
isReset = false;
|
|
3656
|
+
},
|
|
3657
|
+
reset: () => {
|
|
3658
|
+
isReset = true;
|
|
3659
|
+
},
|
|
3660
|
+
isReset: () => {
|
|
3661
|
+
return isReset;
|
|
3662
|
+
}
|
|
3663
|
+
};
|
|
3664
|
+
}
|
|
3665
|
+
|
|
3666
|
+
const QueryErrorResetBoundaryContext = /*#__PURE__*/React__namespace.createContext(createValue()); // HOOK
|
|
3667
|
+
|
|
3668
|
+
const useQueryErrorResetBoundary = () => React__namespace.useContext(QueryErrorResetBoundaryContext); // COMPONENT
|
|
3669
|
+
|
|
3670
|
+
function shouldThrowError(_useErrorBoundary, params) {
|
|
3671
|
+
// Allow useErrorBoundary function to override throwing behavior on a per-error basis
|
|
3672
|
+
if (typeof _useErrorBoundary === 'function') {
|
|
3673
|
+
return _useErrorBoundary(...params);
|
|
3674
|
+
}
|
|
3675
|
+
|
|
3676
|
+
return !!_useErrorBoundary;
|
|
3677
|
+
}
|
|
3678
|
+
|
|
3679
|
+
const ensurePreventErrorBoundaryRetry = (options, errorResetBoundary) => {
|
|
3680
|
+
if (options.suspense || options.useErrorBoundary) {
|
|
3681
|
+
// Prevent retrying failed query if the error boundary has not been reset yet
|
|
3682
|
+
if (!errorResetBoundary.isReset()) {
|
|
3683
|
+
options.retryOnMount = false;
|
|
3684
|
+
}
|
|
3685
|
+
}
|
|
3686
|
+
};
|
|
3687
|
+
const useClearResetErrorBoundary = errorResetBoundary => {
|
|
3688
|
+
React__namespace.useEffect(() => {
|
|
3689
|
+
errorResetBoundary.clearReset();
|
|
3690
|
+
}, [errorResetBoundary]);
|
|
3691
|
+
};
|
|
3692
|
+
const getHasError = ({
|
|
3693
|
+
result,
|
|
3694
|
+
errorResetBoundary,
|
|
3695
|
+
useErrorBoundary,
|
|
3696
|
+
query
|
|
3697
|
+
}) => {
|
|
3698
|
+
return result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(useErrorBoundary, [result.error, query]);
|
|
3699
|
+
};
|
|
3700
|
+
|
|
3701
|
+
const ensureStaleTime = defaultedOptions => {
|
|
3702
|
+
if (defaultedOptions.suspense) {
|
|
3703
|
+
// Always set stale time when using suspense to prevent
|
|
3704
|
+
// fetching again when directly mounting after suspending
|
|
3705
|
+
if (typeof defaultedOptions.staleTime !== 'number') {
|
|
3706
|
+
defaultedOptions.staleTime = 1000;
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
};
|
|
3710
|
+
const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
|
|
3711
|
+
const shouldSuspend = (defaultedOptions, result, isRestoring) => (defaultedOptions == null ? void 0 : defaultedOptions.suspense) && willFetch(result, isRestoring);
|
|
3712
|
+
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).then(({
|
|
3713
|
+
data
|
|
3714
|
+
}) => {
|
|
3715
|
+
defaultedOptions.onSuccess == null ? void 0 : defaultedOptions.onSuccess(data);
|
|
3716
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(data, null);
|
|
3717
|
+
}).catch(error => {
|
|
3718
|
+
errorResetBoundary.clearReset();
|
|
3719
|
+
defaultedOptions.onError == null ? void 0 : defaultedOptions.onError(error);
|
|
3720
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(undefined, error);
|
|
3721
|
+
});
|
|
3722
|
+
|
|
3723
|
+
function useQueries({
|
|
3724
|
+
queries,
|
|
3725
|
+
context
|
|
3726
|
+
}) {
|
|
3727
|
+
const queryClient = useQueryClient({
|
|
3728
|
+
context
|
|
3729
|
+
});
|
|
3730
|
+
const isRestoring = useIsRestoring();
|
|
3731
|
+
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
3732
|
+
const defaultedQueries = React__namespace.useMemo(() => queries.map(options => {
|
|
3733
|
+
const defaultedOptions = queryClient.defaultQueryOptions(options); // Make sure the results are already in fetching state before subscribing or updating options
|
|
3734
|
+
|
|
3735
|
+
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
|
|
3736
|
+
return defaultedOptions;
|
|
3737
|
+
}), [queries, queryClient, isRestoring]);
|
|
3738
|
+
defaultedQueries.forEach(query => {
|
|
3739
|
+
ensureStaleTime(query);
|
|
3740
|
+
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
|
3741
|
+
});
|
|
3742
|
+
useClearResetErrorBoundary(errorResetBoundary);
|
|
3743
|
+
const [observer] = React__namespace.useState(() => new QueriesObserver(queryClient, defaultedQueries));
|
|
3744
|
+
const optimisticResult = observer.getOptimisticResult(defaultedQueries);
|
|
3745
|
+
useSyncExternalStore(React__namespace.useCallback(onStoreChange => isRestoring ? () => undefined : observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
3746
|
+
React__namespace.useEffect(() => {
|
|
3747
|
+
// Do not notify on updates because of changes in the options because
|
|
3748
|
+
// these changes should already be reflected in the optimistic result.
|
|
3749
|
+
observer.setQueries(defaultedQueries, {
|
|
3750
|
+
listeners: false
|
|
3751
|
+
});
|
|
3752
|
+
}, [defaultedQueries, observer]);
|
|
3753
|
+
const shouldAtLeastOneSuspend = optimisticResult.some((result, index) => shouldSuspend(defaultedQueries[index], result, isRestoring));
|
|
3754
|
+
const suspensePromises = shouldAtLeastOneSuspend ? optimisticResult.flatMap((result, index) => {
|
|
3755
|
+
const options = defaultedQueries[index];
|
|
3756
|
+
const queryObserver = observer.getObservers()[index];
|
|
3757
|
+
|
|
3758
|
+
if (options && queryObserver) {
|
|
3759
|
+
if (shouldSuspend(options, result, isRestoring)) {
|
|
3760
|
+
return fetchOptimistic(options, queryObserver, errorResetBoundary);
|
|
3761
|
+
} else if (willFetch(result, isRestoring)) {
|
|
3762
|
+
void fetchOptimistic(options, queryObserver, errorResetBoundary);
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
|
|
3766
|
+
return [];
|
|
3767
|
+
}) : [];
|
|
3768
|
+
|
|
3769
|
+
if (suspensePromises.length > 0) {
|
|
3770
|
+
throw Promise.all(suspensePromises);
|
|
3771
|
+
}
|
|
3772
|
+
|
|
3773
|
+
const observerQueries = observer.getQueries();
|
|
3774
|
+
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => {
|
|
3775
|
+
var _defaultedQueries$ind, _defaultedQueries$ind2;
|
|
3776
|
+
|
|
3777
|
+
return getHasError({
|
|
3778
|
+
result,
|
|
3779
|
+
errorResetBoundary,
|
|
3780
|
+
useErrorBoundary: (_defaultedQueries$ind = (_defaultedQueries$ind2 = defaultedQueries[index]) == null ? void 0 : _defaultedQueries$ind2.useErrorBoundary) != null ? _defaultedQueries$ind : false,
|
|
3781
|
+
query: observerQueries[index]
|
|
3782
|
+
});
|
|
3783
|
+
});
|
|
3784
|
+
|
|
3785
|
+
if (firstSingleResultWhichShouldThrow != null && firstSingleResultWhichShouldThrow.error) {
|
|
3786
|
+
throw firstSingleResultWhichShouldThrow.error;
|
|
3787
|
+
}
|
|
3788
|
+
|
|
3789
|
+
return optimisticResult;
|
|
3790
|
+
}
|
|
3791
|
+
|
|
3792
|
+
function useBaseQuery(options, Observer) {
|
|
3793
|
+
const queryClient = useQueryClient({
|
|
3794
|
+
context: options.context
|
|
3795
|
+
});
|
|
3796
|
+
const isRestoring = useIsRestoring();
|
|
3797
|
+
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
3798
|
+
const defaultedOptions = queryClient.defaultQueryOptions(options); // Make sure results are optimistically set in fetching state before subscribing or updating options
|
|
3799
|
+
|
|
3800
|
+
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic'; // Include callbacks in batch renders
|
|
3801
|
+
|
|
3802
|
+
if (defaultedOptions.onError) {
|
|
3803
|
+
defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
|
|
3804
|
+
}
|
|
3805
|
+
|
|
3806
|
+
if (defaultedOptions.onSuccess) {
|
|
3807
|
+
defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
|
|
3808
|
+
}
|
|
3809
|
+
|
|
3810
|
+
if (defaultedOptions.onSettled) {
|
|
3811
|
+
defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
|
|
3812
|
+
}
|
|
3813
|
+
|
|
3814
|
+
ensureStaleTime(defaultedOptions);
|
|
3815
|
+
ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary);
|
|
3816
|
+
useClearResetErrorBoundary(errorResetBoundary);
|
|
3817
|
+
const [observer] = React__namespace.useState(() => new Observer(queryClient, defaultedOptions));
|
|
3818
|
+
const result = observer.getOptimisticResult(defaultedOptions);
|
|
3819
|
+
useSyncExternalStore(React__namespace.useCallback(onStoreChange => {
|
|
3820
|
+
const unsubscribe = isRestoring ? () => undefined : observer.subscribe(notifyManager.batchCalls(onStoreChange)); // Update result to make sure we did not miss any query updates
|
|
3821
|
+
// between creating the observer and subscribing to it.
|
|
3822
|
+
|
|
3823
|
+
observer.updateResult();
|
|
3824
|
+
return unsubscribe;
|
|
3825
|
+
}, [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
3826
|
+
React__namespace.useEffect(() => {
|
|
3827
|
+
// Do not notify on updates because of changes in the options because
|
|
3828
|
+
// these changes should already be reflected in the optimistic result.
|
|
3829
|
+
observer.setOptions(defaultedOptions, {
|
|
3830
|
+
listeners: false
|
|
3831
|
+
});
|
|
3832
|
+
}, [defaultedOptions, observer]); // Handle suspense
|
|
3833
|
+
|
|
3834
|
+
if (shouldSuspend(defaultedOptions, result, isRestoring)) {
|
|
3835
|
+
throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary);
|
|
3836
|
+
} // Handle error boundary
|
|
3837
|
+
|
|
3838
|
+
|
|
3839
|
+
if (getHasError({
|
|
3840
|
+
result,
|
|
3841
|
+
errorResetBoundary,
|
|
3842
|
+
useErrorBoundary: defaultedOptions.useErrorBoundary,
|
|
3843
|
+
query: observer.getCurrentQuery()
|
|
3844
|
+
})) {
|
|
3845
|
+
throw result.error;
|
|
3846
|
+
} // Handle result property usage tracking
|
|
3847
|
+
|
|
3848
|
+
|
|
3849
|
+
return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result;
|
|
3850
|
+
}
|
|
3851
|
+
|
|
3852
|
+
function useQuery(arg1, arg2, arg3) {
|
|
3853
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
3854
|
+
return useBaseQuery(parsedOptions, QueryObserver);
|
|
3855
|
+
}
|
|
3856
|
+
|
|
3857
|
+
function useMutation(arg1, arg2, arg3) {
|
|
3858
|
+
const options = parseMutationArgs(arg1, arg2, arg3);
|
|
3859
|
+
const queryClient = useQueryClient({
|
|
3860
|
+
context: options.context
|
|
3861
|
+
});
|
|
3862
|
+
const [observer] = React__namespace.useState(() => new MutationObserver(queryClient, options));
|
|
3863
|
+
React__namespace.useEffect(() => {
|
|
3864
|
+
observer.setOptions(options);
|
|
3865
|
+
}, [observer, options]);
|
|
3866
|
+
const result = useSyncExternalStore(React__namespace.useCallback(onStoreChange => observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
3867
|
+
const mutate = React__namespace.useCallback((variables, mutateOptions) => {
|
|
3868
|
+
observer.mutate(variables, mutateOptions).catch(noop);
|
|
3869
|
+
}, [observer]);
|
|
3870
|
+
|
|
3871
|
+
if (result.error && shouldThrowError(observer.options.useErrorBoundary, [result.error])) {
|
|
3872
|
+
throw result.error;
|
|
3873
|
+
}
|
|
3874
|
+
|
|
3875
|
+
return { ...result,
|
|
3876
|
+
mutate,
|
|
3877
|
+
mutateAsync: result.mutate
|
|
3878
|
+
};
|
|
3879
|
+
} // eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
3880
|
+
|
|
3881
|
+
function noop() {}
|
|
3882
|
+
|
|
3883
|
+
exports.TransactionErrorType = void 0;
|
|
3884
|
+
(function (TransactionErrorType) {
|
|
3885
|
+
TransactionErrorType[TransactionErrorType["REJECTED_BY_USER"] = 0] = "REJECTED_BY_USER";
|
|
3886
|
+
TransactionErrorType[TransactionErrorType["CALL_EXCEPTION"] = 1] = "CALL_EXCEPTION";
|
|
3887
|
+
TransactionErrorType[TransactionErrorType["UNKNOWN"] = 2] = "UNKNOWN";
|
|
3888
|
+
TransactionErrorType[TransactionErrorType["WARNING"] = 3] = "WARNING";
|
|
3889
|
+
})(exports.TransactionErrorType || (exports.TransactionErrorType = {}));
|
|
3890
|
+
exports.SquidStatusErrorType = void 0;
|
|
3891
|
+
(function (SquidStatusErrorType) {
|
|
3892
|
+
SquidStatusErrorType["NotFoundError"] = "NotFoundError";
|
|
3893
|
+
})(exports.SquidStatusErrorType || (exports.SquidStatusErrorType = {}));
|
|
3894
|
+
class QrCodeGenerationError extends Error {
|
|
3895
|
+
constructor(message) {
|
|
3896
|
+
super(message);
|
|
3897
|
+
this.name = "QrCodeGenerationError";
|
|
3898
|
+
}
|
|
3899
|
+
}
|
|
3900
|
+
|
|
273
3901
|
/**
|
|
274
3902
|
* The base implementation of `_.slice` without an iteratee call guard.
|
|
275
3903
|
*
|
|
@@ -20117,17 +23745,14 @@ function isXrplAddressValid(address) {
|
|
|
20117
23745
|
return rippleAddressCodec.isValidXAddress(address) || rippleAddressCodec.isValidClassicAddress(address);
|
|
20118
23746
|
}
|
|
20119
23747
|
function buildXrplTrustSetTx({ amount, sourceAddress, token, }) {
|
|
20120
|
-
const
|
|
20121
|
-
if (!asset) {
|
|
20122
|
-
throw new Error("Invalid asset");
|
|
20123
|
-
}
|
|
23748
|
+
const [currency, issuer] = token.address.split(".");
|
|
20124
23749
|
return {
|
|
20125
23750
|
TransactionType: XrplTransactionType.TRUST_SET,
|
|
20126
23751
|
Flags: XrplTransactionFlag.tfSetNoRipple,
|
|
20127
23752
|
Account: sourceAddress,
|
|
20128
23753
|
LimitAmount: {
|
|
20129
|
-
currency
|
|
20130
|
-
issuer
|
|
23754
|
+
currency,
|
|
23755
|
+
issuer,
|
|
20131
23756
|
value: amount,
|
|
20132
23757
|
},
|
|
20133
23758
|
};
|
|
@@ -20156,16 +23781,6 @@ function parseXrplPaymentTx(data) {
|
|
|
20156
23781
|
throw new Error("Could not parse payment transaction");
|
|
20157
23782
|
}
|
|
20158
23783
|
}
|
|
20159
|
-
function parseXrplTokenAddress(address) {
|
|
20160
|
-
const [code, issuer] = address.split(".");
|
|
20161
|
-
if (!code || !issuer) {
|
|
20162
|
-
return null;
|
|
20163
|
-
}
|
|
20164
|
-
return {
|
|
20165
|
-
code,
|
|
20166
|
-
issuer,
|
|
20167
|
-
};
|
|
20168
|
-
}
|
|
20169
23784
|
|
|
20170
23785
|
const chains = [XrplCAIP2ChainId.MAINNET, XrplCAIP2ChainId.TESTNET];
|
|
20171
23786
|
class XrplWalletConnect {
|
|
@@ -21131,28 +24746,6 @@ const executeSolanaTransfer = async ({ amount, target, signer, connection, sourc
|
|
|
21131
24746
|
return signature;
|
|
21132
24747
|
};
|
|
21133
24748
|
|
|
21134
|
-
var StellarHorizonAssetType;
|
|
21135
|
-
(function (StellarHorizonAssetType) {
|
|
21136
|
-
/**
|
|
21137
|
-
* XLM native token
|
|
21138
|
-
*/
|
|
21139
|
-
StellarHorizonAssetType["NATIVE"] = "native";
|
|
21140
|
-
/**
|
|
21141
|
-
* 1-4 char asset code (e.g. USDC)
|
|
21142
|
-
*/
|
|
21143
|
-
StellarHorizonAssetType["ALPHANUM4"] = "credit_alphanum4";
|
|
21144
|
-
/**
|
|
21145
|
-
* 5-12 char asset code (e.g. wstETH)
|
|
21146
|
-
*/
|
|
21147
|
-
StellarHorizonAssetType["ALPHANUM12"] = "credit_alphanum12";
|
|
21148
|
-
})(StellarHorizonAssetType || (StellarHorizonAssetType = {}));
|
|
21149
|
-
var StellarTokenType;
|
|
21150
|
-
(function (StellarTokenType) {
|
|
21151
|
-
StellarTokenType["NATIVE_TOKEN"] = "nativeToken";
|
|
21152
|
-
StellarTokenType["ISSUER_TOKEN"] = "issuerToken";
|
|
21153
|
-
StellarTokenType["CONTRACT_TOKEN"] = "contractToken";
|
|
21154
|
-
})(StellarTokenType || (StellarTokenType = {}));
|
|
21155
|
-
|
|
21156
24749
|
function isStellarAddressValid(address) {
|
|
21157
24750
|
return stellarSdk.StrKey.isValidEd25519PublicKey(address);
|
|
21158
24751
|
}
|
|
@@ -21174,78 +24767,6 @@ function stellarAddressToScVal(addressString) {
|
|
|
21174
24767
|
type: "address",
|
|
21175
24768
|
});
|
|
21176
24769
|
}
|
|
21177
|
-
function getStellarTrustLineAsset(token) {
|
|
21178
|
-
// The address format for issued assets is `CODE-ISSUER`
|
|
21179
|
-
// Example: USDC-GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN
|
|
21180
|
-
const [code, issuer] = token.address.split("-");
|
|
21181
|
-
if (!code || !issuer) {
|
|
21182
|
-
return null;
|
|
21183
|
-
}
|
|
21184
|
-
return {
|
|
21185
|
-
code,
|
|
21186
|
-
issuer,
|
|
21187
|
-
};
|
|
21188
|
-
}
|
|
21189
|
-
function isStellarToken(token) {
|
|
21190
|
-
try {
|
|
21191
|
-
const stellarToken = token;
|
|
21192
|
-
return (Object.values(StellarTokenType).includes(stellarToken.chainAssetConfig.stellar.assetType) &&
|
|
21193
|
-
typeof stellarToken.chainAssetConfig.stellar.contractAddress === "string");
|
|
21194
|
-
}
|
|
21195
|
-
catch {
|
|
21196
|
-
return false;
|
|
21197
|
-
}
|
|
21198
|
-
}
|
|
21199
|
-
function isStellarIssuedToken(token) {
|
|
21200
|
-
try {
|
|
21201
|
-
return (isStellarToken(token) &&
|
|
21202
|
-
token.chainAssetConfig.stellar.assetType === StellarTokenType.ISSUER_TOKEN);
|
|
21203
|
-
}
|
|
21204
|
-
catch {
|
|
21205
|
-
return false;
|
|
21206
|
-
}
|
|
21207
|
-
}
|
|
21208
|
-
function getStellarHorizonApiUrl(chain) {
|
|
21209
|
-
try {
|
|
21210
|
-
const stellarChain = chain;
|
|
21211
|
-
const [horizonApiUrl] = stellarChain.horizonRpcList;
|
|
21212
|
-
if (typeof horizonApiUrl !== "string") {
|
|
21213
|
-
throw new Error("Invalid Horizon API URL");
|
|
21214
|
-
}
|
|
21215
|
-
return horizonApiUrl;
|
|
21216
|
-
}
|
|
21217
|
-
catch {
|
|
21218
|
-
return null;
|
|
21219
|
-
}
|
|
21220
|
-
}
|
|
21221
|
-
const VALID_ASSET_TYPES = new Set(Object.values(StellarHorizonAssetType));
|
|
21222
|
-
function isValidNativeAsset(asset) {
|
|
21223
|
-
return (typeof asset === "object" &&
|
|
21224
|
-
asset !== null &&
|
|
21225
|
-
"balance" in asset &&
|
|
21226
|
-
typeof asset.balance === "string" &&
|
|
21227
|
-
"asset_type" in asset &&
|
|
21228
|
-
asset.asset_type === StellarHorizonAssetType.NATIVE);
|
|
21229
|
-
}
|
|
21230
|
-
function isValidIssuedAsset(asset) {
|
|
21231
|
-
return (typeof asset === "object" &&
|
|
21232
|
-
asset !== null &&
|
|
21233
|
-
"balance" in asset &&
|
|
21234
|
-
typeof asset.balance === "string" &&
|
|
21235
|
-
"asset_type" in asset &&
|
|
21236
|
-
typeof asset.asset_type === "string" &&
|
|
21237
|
-
VALID_ASSET_TYPES.has(asset.asset_type) &&
|
|
21238
|
-
asset.asset_type !== StellarHorizonAssetType.NATIVE &&
|
|
21239
|
-
"asset_code" in asset &&
|
|
21240
|
-
typeof asset.asset_code === "string" &&
|
|
21241
|
-
"asset_issuer" in asset &&
|
|
21242
|
-
typeof asset.asset_issuer === "string" &&
|
|
21243
|
-
"limit" in asset &&
|
|
21244
|
-
typeof asset.limit === "string");
|
|
21245
|
-
}
|
|
21246
|
-
function isValidHorizonAsset(asset) {
|
|
21247
|
-
return isValidNativeAsset(asset) || isValidIssuedAsset(asset);
|
|
21248
|
-
}
|
|
21249
24770
|
|
|
21250
24771
|
const SUI_FEATURES = ["sui:signTransaction"];
|
|
21251
24772
|
function isSuiStandardWallet(wallet) {
|
|
@@ -22324,8 +25845,6 @@ exports.QueryKeys = void 0;
|
|
|
22324
25845
|
QueryKeys["XrplAccountActivatedInfo"] = "xrplAccountActivatedInfo";
|
|
22325
25846
|
QueryKeys["FiatToCryptoPaymentMethods"] = "fiatToCryptoPaymentMethods";
|
|
22326
25847
|
QueryKeys["Stellar"] = "stellar";
|
|
22327
|
-
QueryKeys["StellarTrustLine"] = "stellarTrustLine";
|
|
22328
|
-
QueryKeys["IsStellarTrustLineApproved"] = "isStellarTrustLineApproved";
|
|
22329
25848
|
QueryKeys["StellarAccountActivatedInfo"] = "stellarAccountActivatedInfo";
|
|
22330
25849
|
QueryKeys["Hedera"] = "hedera";
|
|
22331
25850
|
QueryKeys["IsHederaTokenAssociated"] = "isHederaTokenAssociated";
|
|
@@ -22433,7 +25952,7 @@ const keys = () => ({
|
|
|
22433
25952
|
// ============
|
|
22434
25953
|
// Approval
|
|
22435
25954
|
// ============
|
|
22436
|
-
routeApproved: (routeData, allowanceInWei
|
|
25955
|
+
routeApproved: (routeData, allowanceInWei) => [
|
|
22437
25956
|
...keys().transactions(),
|
|
22438
25957
|
exports.QueryKeys.RouteApproved,
|
|
22439
25958
|
routeData?.params.fromAddress,
|
|
@@ -22441,8 +25960,6 @@ const keys = () => ({
|
|
|
22441
25960
|
routeData?.params.fromToken,
|
|
22442
25961
|
routeData?.params.fromAmount,
|
|
22443
25962
|
allowanceInWei?.toString(),
|
|
22444
|
-
isAllowanceQueryEnabled,
|
|
22445
|
-
hasAllowance,
|
|
22446
25963
|
],
|
|
22447
25964
|
sendTransactionGas: (chainId, tokenAddress, from) => [
|
|
22448
25965
|
exports.QueryKeys.All,
|
|
@@ -22515,23 +26032,6 @@ const keys = () => ({
|
|
|
22515
26032
|
// ============
|
|
22516
26033
|
// Stellar
|
|
22517
26034
|
// ============
|
|
22518
|
-
stellarTrustLine: (tokenAddress, chainId, address) => [
|
|
22519
|
-
...keys().stellar(),
|
|
22520
|
-
exports.QueryKeys.StellarTrustLine,
|
|
22521
|
-
tokenAddress,
|
|
22522
|
-
chainId,
|
|
22523
|
-
address,
|
|
22524
|
-
],
|
|
22525
|
-
isStellarTrustLineApproved: (address, chainId, chainType, tokenAddress, trustLineLimit, amountToApprove) => [
|
|
22526
|
-
...keys().stellar(),
|
|
22527
|
-
exports.QueryKeys.IsStellarTrustLineApproved,
|
|
22528
|
-
address,
|
|
22529
|
-
chainId,
|
|
22530
|
-
chainType,
|
|
22531
|
-
tokenAddress,
|
|
22532
|
-
trustLineLimit,
|
|
22533
|
-
amountToApprove?.toString(),
|
|
22534
|
-
],
|
|
22535
26035
|
stellarAccountActivatedInfo: (address, chainId, chainType) => [
|
|
22536
26036
|
...keys().stellar(),
|
|
22537
26037
|
exports.QueryKeys.StellarAccountActivatedInfo,
|
|
@@ -22567,8 +26067,6 @@ const getPrefixKey = (key) => {
|
|
|
22567
26067
|
return [...keys().transactions(), key];
|
|
22568
26068
|
case exports.QueryKeys.XrplTrustLine:
|
|
22569
26069
|
return [...keys().xrpl(), key];
|
|
22570
|
-
case exports.QueryKeys.StellarTrustLine:
|
|
22571
|
-
return [...keys().stellar(), key];
|
|
22572
26070
|
case exports.QueryKeys.IsHederaTokenAssociated:
|
|
22573
26071
|
return [...keys().hedera(), key];
|
|
22574
26072
|
default:
|
|
@@ -23283,7 +26781,7 @@ const filterViewableTokens = (tokens, config, direction) => {
|
|
|
23283
26781
|
};
|
|
23284
26782
|
const getSecretNetworkBalances = async (chainData, cosmosAddress, squidTokens, keplrTypeWallet) => {
|
|
23285
26783
|
const squidSecretTokens = squidTokens.filter((t) => t.chainId === CHAIN_IDS.SECRET);
|
|
23286
|
-
const { fetchAllSecretBalances } = await Promise.resolve().then(function () { return require('./secretService-
|
|
26784
|
+
const { fetchAllSecretBalances } = await Promise.resolve().then(function () { return require('./secretService-CY6elBY4.js'); });
|
|
23287
26785
|
return fetchAllSecretBalances(chainData, cosmosAddress, squidSecretTokens, keplrTypeWallet);
|
|
23288
26786
|
};
|
|
23289
26787
|
function getTokenAssetsKey(token) {
|
|
@@ -25162,7 +28660,7 @@ const useSquid = () => {
|
|
|
25162
28660
|
* Fetch squid info
|
|
25163
28661
|
* Will refetch every minute
|
|
25164
28662
|
*/
|
|
25165
|
-
const squidInfoQuery =
|
|
28663
|
+
const squidInfoQuery = useQuery({
|
|
25166
28664
|
queryKey: keys().squidInfo(),
|
|
25167
28665
|
queryFn: async () => {
|
|
25168
28666
|
if (squid) {
|
|
@@ -25335,7 +28833,7 @@ const createTransactionStatusConfig = ({ transactionId, walletAddress, providerI
|
|
|
25335
28833
|
const useGetFiatQuote = ({ fiatCurrency, cryptoCurrencyID, amount, region, paymentMethod, enabled = true, onSuccess, }) => {
|
|
25336
28834
|
const service = React.useMemo(() => new OnrampService(), []);
|
|
25337
28835
|
const { data: config } = useGetOnRampConfig();
|
|
25338
|
-
return
|
|
28836
|
+
return useQuery({
|
|
25339
28837
|
queryKey: keys().fiatToCryptoQuote(fiatCurrency, cryptoCurrencyID, amount, region, paymentMethod),
|
|
25340
28838
|
queryFn: async () => {
|
|
25341
28839
|
// Dispatch event before getting quote
|
|
@@ -25379,7 +28877,7 @@ const useGetOnRampConfig = () => {
|
|
|
25379
28877
|
const service = React.useMemo(() => new OnrampService(), []);
|
|
25380
28878
|
const { chains } = useSquidChains();
|
|
25381
28879
|
const { tokens } = useSquidTokens();
|
|
25382
|
-
return
|
|
28880
|
+
return useQuery({
|
|
25383
28881
|
queryKey: keys().fiatToCryptoConfig(),
|
|
25384
28882
|
queryFn: () => service.getConfiguration({
|
|
25385
28883
|
chains,
|
|
@@ -25397,7 +28895,7 @@ const useExecuteFiatQuote = () => {
|
|
|
25397
28895
|
const service = React.useMemo(() => new OnrampService(), []);
|
|
25398
28896
|
const persistTransaction = useHistoryStore((state) => state.persistTransaction);
|
|
25399
28897
|
const { findChain } = useSquidChains();
|
|
25400
|
-
return
|
|
28898
|
+
return useMutation({
|
|
25401
28899
|
mutationFn: async (executeQuoteRequest) => {
|
|
25402
28900
|
const chain = findChain(executeQuoteRequest.squidToken?.chainId);
|
|
25403
28901
|
if (!chain) {
|
|
@@ -25448,7 +28946,7 @@ const useExecuteFiatQuote = () => {
|
|
|
25448
28946
|
*/
|
|
25449
28947
|
const useFiatOnRampTxStatus = (transactionId, walletAddress, providerId) => {
|
|
25450
28948
|
const replaceTransactionStatus = useHistoryStore((state) => state.replaceTransactionStatus);
|
|
25451
|
-
return
|
|
28949
|
+
return useQuery({
|
|
25452
28950
|
...createTransactionStatusConfig({
|
|
25453
28951
|
transactionId,
|
|
25454
28952
|
walletAddress,
|
|
@@ -25464,7 +28962,7 @@ const useFiatTransactions = () => {
|
|
|
25464
28962
|
const transactions = useHistoryStore((state) => state.transactions.filter((tx) => tx.txType === exports.HistoryTxType.BUY));
|
|
25465
28963
|
const replaceTransactionStatus = useHistoryStore((state) => state.replaceTransactionStatus);
|
|
25466
28964
|
const pendingTransactions = React.useMemo(() => transactions?.filter((tx) => !FINAL_TRANSACTION_STATUSES.includes(tx.data.status)) ?? [], [transactions]);
|
|
25467
|
-
const queries =
|
|
28965
|
+
const queries = useQueries({
|
|
25468
28966
|
queries: pendingTransactions.map((tx) => ({
|
|
25469
28967
|
...createTransactionStatusConfig({
|
|
25470
28968
|
transactionId: tx.data.orderId,
|
|
@@ -25548,7 +29046,7 @@ const useRecommendedQuote = (quotes, recommendedQuote) => {
|
|
|
25548
29046
|
*/
|
|
25549
29047
|
const useGetOnrampPaymentTypes = ({ fiatCurrency, cryptoCurrencyID, region, enabled = true, }) => {
|
|
25550
29048
|
const service = React.useMemo(() => new OnrampService(), []);
|
|
25551
|
-
return
|
|
29049
|
+
return useQuery({
|
|
25552
29050
|
queryKey: keys().fiatToCryptoPaymentMethods(fiatCurrency, cryptoCurrencyID, region),
|
|
25553
29051
|
queryFn: () => service.getPaymentTypes(fiatCurrency, cryptoCurrencyID, region),
|
|
25554
29052
|
enabled: enabled && !!fiatCurrency && !!cryptoCurrencyID,
|
|
@@ -26084,7 +29582,7 @@ const EU_COUNTRIES = [
|
|
|
26084
29582
|
const useClient = () => {
|
|
26085
29583
|
const isClient = typeof window !== "undefined";
|
|
26086
29584
|
const clientWindow = isClient ? window : undefined;
|
|
26087
|
-
const { data: countryData } =
|
|
29585
|
+
const { data: countryData } = useQuery({
|
|
26088
29586
|
queryKey: keys().country(),
|
|
26089
29587
|
queryFn: getUserCountry,
|
|
26090
29588
|
enabled: isClient,
|
|
@@ -26190,7 +29688,7 @@ function useDebouncedValue(value, delay) {
|
|
|
26190
29688
|
* @returns the ENS data of the given address
|
|
26191
29689
|
*/
|
|
26192
29690
|
function useEnsDataForAddress({ address, options, }) {
|
|
26193
|
-
const ensQuery =
|
|
29691
|
+
const ensQuery = useQuery(keys().ensData(address?.toLowerCase()), () => EnsService.getEnsDataFromAddress(address), {
|
|
26194
29692
|
...options,
|
|
26195
29693
|
enabled: Boolean(address?.trim()) &&
|
|
26196
29694
|
(options?.enabled === undefined ? true : options.enabled),
|
|
@@ -26210,7 +29708,7 @@ function useEnsDataForAddress({ address, options, }) {
|
|
|
26210
29708
|
*/
|
|
26211
29709
|
function useEnsSearch({ name, enabled = true, delayMs = 500, }) {
|
|
26212
29710
|
const debouncedName = useDebouncedValue(enabled ? name : undefined, delayMs);
|
|
26213
|
-
const ensSearchQuery =
|
|
29711
|
+
const ensSearchQuery = useQuery(keys().ensSearch(debouncedName), () => {
|
|
26214
29712
|
const isExactEnsSearch = checkIsExactEns(debouncedName);
|
|
26215
29713
|
return isExactEnsSearch
|
|
26216
29714
|
? EnsService.getExactEns(debouncedName)
|
|
@@ -26238,7 +29736,7 @@ function checkIsExactEns(name = "") {
|
|
|
26238
29736
|
|
|
26239
29737
|
const useCosmosForChain = (chain) => {
|
|
26240
29738
|
const { keplrTypeWallet } = useCosmosSigner({ chain });
|
|
26241
|
-
const cosmosAddressQuery =
|
|
29739
|
+
const cosmosAddressQuery = useQuery(keys().cosmosAddress(chain?.chainId), async () => {
|
|
26242
29740
|
if (!chain || !keplrTypeWallet)
|
|
26243
29741
|
return "";
|
|
26244
29742
|
try {
|
|
@@ -26331,7 +29829,7 @@ const useIsSameAddressAndGnosisContext = () => {
|
|
|
26331
29829
|
|
|
26332
29830
|
function useBitcoin() {
|
|
26333
29831
|
const connectedBitcoinWallet = useWalletStore((store) => store.connectedWalletsByChainType[squidTypes.ChainType.BTC]);
|
|
26334
|
-
const connectBitcoin =
|
|
29832
|
+
const connectBitcoin = useMutation(async ({ wallet, }) => {
|
|
26335
29833
|
const { address } = await wallet.connector.requestAccount();
|
|
26336
29834
|
return {
|
|
26337
29835
|
wallet,
|
|
@@ -26376,7 +29874,7 @@ function useEvm() {
|
|
|
26376
29874
|
const { wallets } = useEvmWallets();
|
|
26377
29875
|
const connectedEvmWallet = useWalletStore((store) => store.connectedWalletsByChainType[squidTypes.ChainType.EVM]);
|
|
26378
29876
|
const recentEvmWalletId = useWalletStore((store) => store.recentConnectorIds[squidTypes.ChainType.EVM]);
|
|
26379
|
-
const connectEvm =
|
|
29877
|
+
const connectEvm = useMutation(async ({ wallet, }) => {
|
|
26380
29878
|
const result = await connectAsync({
|
|
26381
29879
|
connector: wallet.connector,
|
|
26382
29880
|
});
|
|
@@ -26478,7 +29976,7 @@ function useSolana() {
|
|
|
26478
29976
|
const { wallets } = useSolanaWallets();
|
|
26479
29977
|
const connectedSolanaWallet = useWalletStore((store) => store.connectedWalletsByChainType[squidTypes.ChainType.SOLANA]);
|
|
26480
29978
|
const recentSolanaWalletId = useWalletStore((store) => store.recentConnectorIds[squidTypes.ChainType.SOLANA]);
|
|
26481
|
-
const connectSolana =
|
|
29979
|
+
const connectSolana = useMutation(async ({ wallet, }) => {
|
|
26482
29980
|
try {
|
|
26483
29981
|
// try to connect silently first
|
|
26484
29982
|
await wallet.connector.autoConnect();
|
|
@@ -26568,7 +30066,7 @@ function useStellarWallets() {
|
|
|
26568
30066
|
try {
|
|
26569
30067
|
const { allowAllModules: initializeAllModules } = await import('@creit.tech/stellar-wallets-kit');
|
|
26570
30068
|
const { LedgerModule } = await import('@creit.tech/stellar-wallets-kit/modules/ledger.module');
|
|
26571
|
-
const { formatStellarWallet } = await Promise.resolve().then(function () { return require('./stellarService.client-
|
|
30069
|
+
const { formatStellarWallet } = await Promise.resolve().then(function () { return require('./stellarService.client-2CVAOoJl.js'); });
|
|
26572
30070
|
const modules = [...initializeAllModules(), new LedgerModule()];
|
|
26573
30071
|
const promises = modules.map(async (module) => {
|
|
26574
30072
|
const isAvailable = await module.isAvailable();
|
|
@@ -26596,7 +30094,7 @@ function useStellarWallets() {
|
|
|
26596
30094
|
function useStellar() {
|
|
26597
30095
|
const { wallets } = useStellarWallets();
|
|
26598
30096
|
const connectedStellarWallet = useWalletStore((store) => store.connectedWalletsByChainType[squidTypes.ChainType.STELLAR]);
|
|
26599
|
-
const connectStellar =
|
|
30097
|
+
const connectStellar = useMutation(async ({ wallet, }) => {
|
|
26600
30098
|
const { address } = await wallet.connector.getAddress();
|
|
26601
30099
|
return {
|
|
26602
30100
|
wallet,
|
|
@@ -26653,7 +30151,7 @@ function useSui() {
|
|
|
26653
30151
|
const recentSuiWalletId = useWalletStore((store) => store.recentConnectorIds[squidTypes.ChainType.SUI]);
|
|
26654
30152
|
const setConnectedWallet = useWalletStore((store) => store.setConnectedWallet);
|
|
26655
30153
|
const disconnectWallet = useWalletStore((store) => store.disconnectWallet);
|
|
26656
|
-
const connectSui =
|
|
30154
|
+
const connectSui = useMutation(async ({ wallet, }) => {
|
|
26657
30155
|
let account;
|
|
26658
30156
|
try {
|
|
26659
30157
|
// try to connect silently first
|
|
@@ -26977,7 +30475,7 @@ function useXrpl() {
|
|
|
26977
30475
|
const recentXrplWalletId = useWalletStore((store) => store.recentConnectorIds[squidTypes.ChainType.XRPL]);
|
|
26978
30476
|
const setConnectedWallet = useWalletStore((store) => store.setConnectedWallet);
|
|
26979
30477
|
const disconnectWallet = useWalletStore((store) => store.disconnectWallet);
|
|
26980
|
-
const connectXrpl =
|
|
30478
|
+
const connectXrpl = useMutation(async ({ wallet, }) => {
|
|
26981
30479
|
const xrplConnectedAddress = await wallet.connector.connect();
|
|
26982
30480
|
return {
|
|
26983
30481
|
wallet,
|
|
@@ -27091,7 +30589,7 @@ const useWallet = () => {
|
|
|
27091
30589
|
wallet,
|
|
27092
30590
|
});
|
|
27093
30591
|
};
|
|
27094
|
-
const connectWalletMutation =
|
|
30592
|
+
const connectWalletMutation = useMutation({
|
|
27095
30593
|
mutationFn: async ({ wallet, chain, selectedChainTypes, }) => {
|
|
27096
30594
|
if (!wallet.skipInstallCheck && !wallet.isInstalled?.()) {
|
|
27097
30595
|
redirectToExtensionsStore(wallet);
|
|
@@ -27369,7 +30867,7 @@ const useMultiChainWallet = (chain) => {
|
|
|
27369
30867
|
/**
|
|
27370
30868
|
* Change current network for desired chain
|
|
27371
30869
|
*/
|
|
27372
|
-
const changeNetworkIfNeeded =
|
|
30870
|
+
const changeNetworkIfNeeded = useMutation(async () => {
|
|
27373
30871
|
const isNotOnDesiredChain = chain?.chainType === squidTypes.ChainType.EVM &&
|
|
27374
30872
|
currentEvmChain?.id !== Number(chain.chainId);
|
|
27375
30873
|
if (isNotOnDesiredChain) {
|
|
@@ -27470,7 +30968,7 @@ const useSwap = () => {
|
|
|
27470
30968
|
const fromPrice = useTransactionStore((state) => state.fromPrice);
|
|
27471
30969
|
const { swapRoute } = useSwapRoutePersistStore();
|
|
27472
30970
|
const { tokens } = useSquidTokens();
|
|
27473
|
-
const queryClient =
|
|
30971
|
+
const queryClient = useQueryClient();
|
|
27474
30972
|
const { chains, supportedDestinationChains, supportedSourceChains } = useSquidChains();
|
|
27475
30973
|
const sourceChainId = React.useMemo(() => swapRoute?.fromChainId ||
|
|
27476
30974
|
(supportedSourceChains.find((c) => c.chainId === initialAssets?.from?.chainId) &&
|
|
@@ -27740,7 +31238,7 @@ const useCosmos = () => {
|
|
|
27740
31238
|
clientWindow?.removeEventListener("keplr_keystorechange", () => handleKeplrAccountChanged());
|
|
27741
31239
|
};
|
|
27742
31240
|
}, [handleKeplrAccountChanged, clientWindow]);
|
|
27743
|
-
const connectCosmos =
|
|
31241
|
+
const connectCosmos = useMutation(async ({ chain, wallet, approveAllChains = true, }) => {
|
|
27744
31242
|
const chainInfos = getCosmosChainInfosObject(chain);
|
|
27745
31243
|
const cosmosWalletObject = wallet.connector().provider;
|
|
27746
31244
|
if (cosmosWalletObject) {
|
|
@@ -28068,7 +31566,7 @@ function hederaWalletConnect(parameters) {
|
|
|
28068
31566
|
const optionalChains = config.chains.map((x) => x.id);
|
|
28069
31567
|
if (!optionalChains.length)
|
|
28070
31568
|
return;
|
|
28071
|
-
const { EthereumProvider } = await Promise.resolve().then(function () { return require('./index.es-
|
|
31569
|
+
const { EthereumProvider } = await Promise.resolve().then(function () { return require('./index.es-C5DLFXAX.js'); });
|
|
28072
31570
|
const rawProvider = await EthereumProvider.init({
|
|
28073
31571
|
...restParameters,
|
|
28074
31572
|
disableProviderPing: true,
|
|
@@ -28325,11 +31823,11 @@ function useHederaTokenAssociations({ address, chain, token }) {
|
|
|
28325
31823
|
chainId: Number(CHAIN_IDS.HEDERA),
|
|
28326
31824
|
});
|
|
28327
31825
|
const { evmSigner } = useSigner({ chain });
|
|
28328
|
-
const queryClient =
|
|
31826
|
+
const queryClient = useQueryClient();
|
|
28329
31827
|
/**
|
|
28330
31828
|
* Creates a token association transaction where the destination account authorizes to receive the given token
|
|
28331
31829
|
*/
|
|
28332
|
-
const associateToken =
|
|
31830
|
+
const associateToken = useMutation({
|
|
28333
31831
|
mutationFn: async () => {
|
|
28334
31832
|
try {
|
|
28335
31833
|
if (!evmSigner) {
|
|
@@ -28381,7 +31879,7 @@ function useHederaTokenAssociations({ address, chain, token }) {
|
|
|
28381
31879
|
*
|
|
28382
31880
|
* Accounts which have max. associations set to -1 can receive any token without previous association
|
|
28383
31881
|
*/
|
|
28384
|
-
const isTokenAssociated =
|
|
31882
|
+
const isTokenAssociated = useQuery({
|
|
28385
31883
|
queryKey: keys().isHederaTokenAssociated(address, token?.chainId, token?.type, token?.address),
|
|
28386
31884
|
queryFn: async () => {
|
|
28387
31885
|
if (token?.chainId !== CHAIN_IDS.HEDERA) {
|
|
@@ -28429,7 +31927,7 @@ const useKeyboardNavigation = ({ onEscape }) => {
|
|
|
28429
31927
|
};
|
|
28430
31928
|
|
|
28431
31929
|
const useSquidQueryClient = () => {
|
|
28432
|
-
const queryClient =
|
|
31930
|
+
const queryClient = useQueryClient();
|
|
28433
31931
|
const invalidateQueries = (key) => {
|
|
28434
31932
|
const prefixKey = getPrefixKey(key);
|
|
28435
31933
|
queryClient.invalidateQueries(prefixKey);
|
|
@@ -29431,15 +32929,12 @@ const getAllXrplTokensBalance = async (userAddress, xrplTokens, xrplChains) => {
|
|
|
29431
32929
|
};
|
|
29432
32930
|
const getStellarTokenBalance = async (userAddress, token, chain) => {
|
|
29433
32931
|
const stellarClient = await getClient(chain);
|
|
29434
|
-
|
|
29435
|
-
throw new Error("Token must be a Stellar token");
|
|
29436
|
-
}
|
|
29437
|
-
const balance = await stellarClient.getBalance(userAddress, token.chainAssetConfig.stellar.contractAddress, chain.chainId);
|
|
32932
|
+
const balance = await stellarClient.getBalance(userAddress, token.address, chain.chainId);
|
|
29438
32933
|
return BigInt(balance);
|
|
29439
32934
|
};
|
|
29440
32935
|
const getAllStellarTokensBalance = async (userAddress, stellarTokens, stellarChains) => {
|
|
29441
32936
|
const getBalancesForChain = async (chain) => {
|
|
29442
|
-
const tokensForChain = stellarTokens.filter((t) => t.chainId === chain.chainId
|
|
32937
|
+
const tokensForChain = stellarTokens.filter((t) => t.chainId === chain.chainId);
|
|
29443
32938
|
const stellarClient = await getClient(chain);
|
|
29444
32939
|
const allBalances = await stellarClient.getAllBalances(userAddress, tokensForChain);
|
|
29445
32940
|
return allBalances.map((token) => {
|
|
@@ -29494,7 +32989,7 @@ class StellarRpcClient {
|
|
|
29494
32989
|
.build();
|
|
29495
32990
|
const simulateTxResponse = await this.server.simulateTransaction(tx);
|
|
29496
32991
|
if ("error" in simulateTxResponse) {
|
|
29497
|
-
const isNoBalanceError =
|
|
32992
|
+
const isNoBalanceError = simulateTxResponse.error.includes("trying to get non-existing value for contract instance");
|
|
29498
32993
|
// If the error message indicates that the user has no balance just return 0
|
|
29499
32994
|
// We don't want to spam with this error as it's pretty common
|
|
29500
32995
|
if (isNoBalanceError) {
|
|
@@ -29510,7 +33005,7 @@ class StellarRpcClient {
|
|
|
29510
33005
|
}
|
|
29511
33006
|
async getAllBalances(userAddress, tokens) {
|
|
29512
33007
|
const balancePromises = tokens.map((token) => {
|
|
29513
|
-
return this.getBalance(userAddress, token.
|
|
33008
|
+
return this.getBalance(userAddress, token.address, token.chainId);
|
|
29514
33009
|
});
|
|
29515
33010
|
const results = await Promise.allSettled(balancePromises);
|
|
29516
33011
|
const balances = results.map((result) => {
|
|
@@ -29610,9 +33105,9 @@ class XrplRpcClient {
|
|
|
29610
33105
|
},
|
|
29611
33106
|
]);
|
|
29612
33107
|
}
|
|
29613
|
-
async getTrustLine(address,
|
|
29614
|
-
const response = await this.getTrustLines(address,
|
|
29615
|
-
const trustLine = response.lines.find((line) => line.currency ===
|
|
33108
|
+
async getTrustLine(address, issuer, currency) {
|
|
33109
|
+
const response = await this.getTrustLines(address, issuer);
|
|
33110
|
+
const trustLine = response.lines.find((line) => line.currency === currency);
|
|
29616
33111
|
return trustLine ?? null;
|
|
29617
33112
|
}
|
|
29618
33113
|
async accountActivatedInfo(address) {
|
|
@@ -29788,32 +33283,15 @@ class StellarApiClient {
|
|
|
29788
33283
|
const baseReserveBn = BigInt(latestLedger.base_reserve_in_stroops);
|
|
29789
33284
|
return baseReserveBn;
|
|
29790
33285
|
}
|
|
29791
|
-
async getTrustLines(userAddress) {
|
|
29792
|
-
const response = await fetch(`${this.apiUrl}/accounts/${userAddress}`);
|
|
29793
|
-
if (!response.ok) {
|
|
29794
|
-
throw new Error(`Failed to fetch account data: ${response.statusText}`);
|
|
29795
|
-
}
|
|
29796
|
-
const data = await response.json();
|
|
29797
|
-
if (!Array.isArray(data?.balances)) {
|
|
29798
|
-
throw new Error("Invalid response from Horizon API");
|
|
29799
|
-
}
|
|
29800
|
-
const assets = data.balances.filter(isValidHorizonAsset);
|
|
29801
|
-
return assets.filter(isValidIssuedAsset);
|
|
29802
|
-
}
|
|
29803
|
-
async getTrustLine(address, asset) {
|
|
29804
|
-
const trustLines = await this.getTrustLines(address);
|
|
29805
|
-
const trustLine = trustLines.find((line) => line.asset_code === asset.code && line.asset_issuer === asset.issuer);
|
|
29806
|
-
return trustLine ?? null;
|
|
29807
|
-
}
|
|
29808
33286
|
}
|
|
29809
33287
|
|
|
29810
|
-
const DEFAULT_REFETCH_INTERVAL$
|
|
33288
|
+
const DEFAULT_REFETCH_INTERVAL$1 = 20_000;
|
|
29811
33289
|
function useStellarAccountActivation({ address, chain, token, }) {
|
|
29812
33290
|
/**
|
|
29813
33291
|
* Checks if the destination account exists on the Stellar network
|
|
29814
33292
|
* Stellar accounts need to have a minimum balance before they can receive payments
|
|
29815
33293
|
*/
|
|
29816
|
-
const accountActivatedInfo =
|
|
33294
|
+
const accountActivatedInfo = useQuery({
|
|
29817
33295
|
queryKey: keys().stellarAccountActivatedInfo(address, chain?.chainId, chain?.chainType),
|
|
29818
33296
|
queryFn: async () => {
|
|
29819
33297
|
if (chain?.chainType !== squidTypes.ChainType.STELLAR ||
|
|
@@ -29823,8 +33301,9 @@ function useStellarAccountActivation({ address, chain, token, }) {
|
|
|
29823
33301
|
if (!address) {
|
|
29824
33302
|
throw new Error("Destination address is required");
|
|
29825
33303
|
}
|
|
29826
|
-
|
|
29827
|
-
|
|
33304
|
+
// TODO: update types
|
|
33305
|
+
const [horizonApiUrl] = chain?.horizonRpcList;
|
|
33306
|
+
if (typeof horizonApiUrl !== "string") {
|
|
29828
33307
|
throw new Error("Invalid Horizon API URL");
|
|
29829
33308
|
}
|
|
29830
33309
|
const stellarApiClient = new StellarApiClient(horizonApiUrl);
|
|
@@ -29840,7 +33319,7 @@ function useStellarAccountActivation({ address, chain, token, }) {
|
|
|
29840
33319
|
};
|
|
29841
33320
|
},
|
|
29842
33321
|
enabled: !!address && chain?.chainType === squidTypes.ChainType.STELLAR,
|
|
29843
|
-
refetchInterval: DEFAULT_REFETCH_INTERVAL$
|
|
33322
|
+
refetchInterval: DEFAULT_REFETCH_INTERVAL$1,
|
|
29844
33323
|
refetchOnWindowFocus: true,
|
|
29845
33324
|
});
|
|
29846
33325
|
return {
|
|
@@ -29848,149 +33327,6 @@ function useStellarAccountActivation({ address, chain, token, }) {
|
|
|
29848
33327
|
};
|
|
29849
33328
|
}
|
|
29850
33329
|
|
|
29851
|
-
/**
|
|
29852
|
-
* Maximum asset amount on Stellar
|
|
29853
|
-
* @see https://developers.stellar.org/docs/learn/fundamentals/stellar-data-structures/assets#amount-precision
|
|
29854
|
-
*/
|
|
29855
|
-
const MAX_ASSET_AMOUNT = "922337203685.4775807";
|
|
29856
|
-
const DEFAULT_REFETCH_INTERVAL$1 = 20_000;
|
|
29857
|
-
function useStellarTrustLine({ address, chain, token, amount }) {
|
|
29858
|
-
const { stellarSigner } = useSigner({ chain });
|
|
29859
|
-
const queryClient = reactQuery.useQueryClient();
|
|
29860
|
-
/**
|
|
29861
|
-
* Retrieves the destination account's trust line data for the given token
|
|
29862
|
-
*/
|
|
29863
|
-
const trustLineQuery = reactQuery.useQuery({
|
|
29864
|
-
queryKey: keys().stellarTrustLine(token?.address, chain?.chainId, address),
|
|
29865
|
-
queryFn: async () => {
|
|
29866
|
-
if (chain?.chainType !== squidTypes.ChainType.STELLAR ||
|
|
29867
|
-
token?.type !== squidTypes.ChainType.STELLAR) {
|
|
29868
|
-
return null;
|
|
29869
|
-
}
|
|
29870
|
-
if (!address || !isStellarAddressValid(address)) {
|
|
29871
|
-
return null;
|
|
29872
|
-
}
|
|
29873
|
-
// Only issued tokens require trust lines
|
|
29874
|
-
// Other token types like contract tokens don't need trust lines
|
|
29875
|
-
if (!isStellarIssuedToken(token)) {
|
|
29876
|
-
return null;
|
|
29877
|
-
}
|
|
29878
|
-
const asset = getStellarTrustLineAsset(token);
|
|
29879
|
-
if (!asset) {
|
|
29880
|
-
throw new Error("Invalid asset");
|
|
29881
|
-
}
|
|
29882
|
-
const horizonApiUrl = getStellarHorizonApiUrl(chain);
|
|
29883
|
-
if (!horizonApiUrl) {
|
|
29884
|
-
throw new Error("Invalid Horizon API URL");
|
|
29885
|
-
}
|
|
29886
|
-
const stellarApiClient = new StellarApiClient(horizonApiUrl);
|
|
29887
|
-
return stellarApiClient.getTrustLine(address, asset);
|
|
29888
|
-
},
|
|
29889
|
-
enabled: !!address &&
|
|
29890
|
-
chain?.chainType === squidTypes.ChainType.STELLAR &&
|
|
29891
|
-
token?.type === squidTypes.ChainType.STELLAR,
|
|
29892
|
-
refetchInterval: DEFAULT_REFETCH_INTERVAL$1,
|
|
29893
|
-
});
|
|
29894
|
-
/**
|
|
29895
|
-
* Creates a trust line where the destination account authorizes to receive the given token
|
|
29896
|
-
*/
|
|
29897
|
-
const createTrustLine = reactQuery.useMutation({
|
|
29898
|
-
mutationFn: async () => {
|
|
29899
|
-
try {
|
|
29900
|
-
if (!stellarSigner) {
|
|
29901
|
-
throw new Error("Stellar signer not found");
|
|
29902
|
-
}
|
|
29903
|
-
if (!address) {
|
|
29904
|
-
throw new Error("Destination address is required");
|
|
29905
|
-
}
|
|
29906
|
-
if (chain?.chainType !== squidTypes.ChainType.STELLAR ||
|
|
29907
|
-
token?.type !== squidTypes.ChainType.STELLAR) {
|
|
29908
|
-
throw new Error("Chain and token to approve must be a Stellar token");
|
|
29909
|
-
}
|
|
29910
|
-
if (!isStellarIssuedToken(token)) {
|
|
29911
|
-
throw new Error("Token must be a Stellar issued token");
|
|
29912
|
-
}
|
|
29913
|
-
const assetInfo = getStellarTrustLineAsset(token);
|
|
29914
|
-
if (!assetInfo) {
|
|
29915
|
-
throw new Error("Invalid asset");
|
|
29916
|
-
}
|
|
29917
|
-
const network = getStellarNetwork(chain.chainId);
|
|
29918
|
-
if (network == null) {
|
|
29919
|
-
throw new Error(`Stellar network not found for chain ${chain.chainId}`);
|
|
29920
|
-
}
|
|
29921
|
-
const client = await getClient(chain);
|
|
29922
|
-
const account = await client.getAccount(address);
|
|
29923
|
-
const asset = new stellarSdk.Asset(assetInfo.code, assetInfo.issuer);
|
|
29924
|
-
const changeTrustOperation = stellarSdk.Operation.changeTrust({
|
|
29925
|
-
asset,
|
|
29926
|
-
limit: MAX_ASSET_AMOUNT,
|
|
29927
|
-
});
|
|
29928
|
-
const builtTransaction = new stellarSdk.TransactionBuilder(account, {
|
|
29929
|
-
fee: (BigInt(stellarSdk.BASE_FEE) * BigInt(2)).toString(),
|
|
29930
|
-
networkPassphrase: network,
|
|
29931
|
-
})
|
|
29932
|
-
.addOperation(changeTrustOperation)
|
|
29933
|
-
.setTimeout(300)
|
|
29934
|
-
.build();
|
|
29935
|
-
const { signedTxXdr } = await stellarSigner.signTransaction(builtTransaction.toXDR(), {
|
|
29936
|
-
networkPassphrase: network,
|
|
29937
|
-
});
|
|
29938
|
-
const signedTransaction = new stellarSdk.Transaction(signedTxXdr, network);
|
|
29939
|
-
const sentTransaction = await client.sendTransaction(signedTransaction);
|
|
29940
|
-
const txStatus = await client.waitForTransaction(sentTransaction.hash, {
|
|
29941
|
-
interval: 1_000,
|
|
29942
|
-
});
|
|
29943
|
-
if (txStatus !== stellarSdk.rpc.Api.GetTransactionStatus.SUCCESS) {
|
|
29944
|
-
throw new Error(`Transaction failed with status: ${txStatus}`);
|
|
29945
|
-
}
|
|
29946
|
-
return true;
|
|
29947
|
-
}
|
|
29948
|
-
catch (error) {
|
|
29949
|
-
console.error("Error creating trust line:", error);
|
|
29950
|
-
return false;
|
|
29951
|
-
}
|
|
29952
|
-
},
|
|
29953
|
-
async onSuccess() {
|
|
29954
|
-
queryClient.invalidateQueries({
|
|
29955
|
-
queryKey: getPrefixKey(exports.QueryKeys.StellarTrustLine),
|
|
29956
|
-
});
|
|
29957
|
-
},
|
|
29958
|
-
});
|
|
29959
|
-
/**
|
|
29960
|
-
* Checks if the destination account has created a trust line to receive the given token.
|
|
29961
|
-
*/
|
|
29962
|
-
const isTrustLineApproved = reactQuery.useQuery({
|
|
29963
|
-
queryKey: keys().isStellarTrustLineApproved(address, token?.chainId, token?.type, token?.address, trustLineQuery.data?.limit, amount),
|
|
29964
|
-
queryFn: async () => {
|
|
29965
|
-
if (token?.type !== squidTypes.ChainType.STELLAR) {
|
|
29966
|
-
return true;
|
|
29967
|
-
}
|
|
29968
|
-
// The native Stellar token doesn't need a trust line
|
|
29969
|
-
if (token.address.toLowerCase() === nativeStellarTokenAddress.toLowerCase()) {
|
|
29970
|
-
return true;
|
|
29971
|
-
}
|
|
29972
|
-
if (!amount) {
|
|
29973
|
-
throw new Error("Amount is required");
|
|
29974
|
-
}
|
|
29975
|
-
const limitBn = BigNumber(trustLineQuery.data?.limit || "0");
|
|
29976
|
-
const balanceBn = BigNumber(trustLineQuery.data?.balance || "0");
|
|
29977
|
-
const availableAllowanceBn = limitBn.minus(balanceBn);
|
|
29978
|
-
const amountBn = BigNumber(formatBNToReadable(amount, token.decimals));
|
|
29979
|
-
return availableAllowanceBn.gte(amountBn);
|
|
29980
|
-
},
|
|
29981
|
-
enabled: !!address &&
|
|
29982
|
-
!!amount &&
|
|
29983
|
-
!trustLineQuery?.isLoading &&
|
|
29984
|
-
trustLineQuery?.isFetched &&
|
|
29985
|
-
token?.type === squidTypes.ChainType.STELLAR,
|
|
29986
|
-
});
|
|
29987
|
-
return {
|
|
29988
|
-
createTrustLine,
|
|
29989
|
-
trustLineQuery,
|
|
29990
|
-
isTrustLineApproved,
|
|
29991
|
-
};
|
|
29992
|
-
}
|
|
29993
|
-
|
|
29994
33330
|
const useAddressBookStore = zustand.create(middleware.persist((set) => ({
|
|
29995
33331
|
addressBook: [],
|
|
29996
33332
|
add: (newAddressData) => {
|
|
@@ -30106,7 +33442,7 @@ function useDepositAddress(squidRoute) {
|
|
|
30106
33442
|
toggleDepositFlow(false);
|
|
30107
33443
|
setDeposit(null);
|
|
30108
33444
|
}, [toggleDepositFlow, setDeposit]);
|
|
30109
|
-
const getRouteWithDeposit =
|
|
33445
|
+
const getRouteWithDeposit = useMutation(async ({ route }) => {
|
|
30110
33446
|
if (!squid)
|
|
30111
33447
|
throw new Error("Squid SDK not initialized");
|
|
30112
33448
|
const depositAddressResponse = (await squid.executeRoute({
|
|
@@ -30200,7 +33536,7 @@ const useAllTokensWithBalanceForChainType = ({ chainType, address, direction, qu
|
|
|
30200
33536
|
xrplTokens.length,
|
|
30201
33537
|
stellarTokens.length,
|
|
30202
33538
|
]);
|
|
30203
|
-
const query =
|
|
33539
|
+
const query = useQuery(keys().allTokensBalance(address, chainType, direction), async () => {
|
|
30204
33540
|
// Return zero balances if no address
|
|
30205
33541
|
if (!address) {
|
|
30206
33542
|
const defaultTokens = placeholderData.tokens;
|
|
@@ -30433,7 +33769,7 @@ const useCosmosNativeBalance = ({ address, chain, }) => {
|
|
|
30433
33769
|
};
|
|
30434
33770
|
const useBitcoinNativeBalance = ({ chain, address, }) => {
|
|
30435
33771
|
const { nativeToken } = useNativeTokenForChain(chain);
|
|
30436
|
-
const { data: rawBalance = "0", isLoading } =
|
|
33772
|
+
const { data: rawBalance = "0", isLoading } = useQuery(keys().balance(chain?.chainId, nativeToken?.address, address), async () => {
|
|
30437
33773
|
if (!address || !nativeToken)
|
|
30438
33774
|
return "0";
|
|
30439
33775
|
const balance = await getBitcoinNativeBalance(address);
|
|
@@ -30462,7 +33798,7 @@ const useBitcoinNativeBalance = ({ chain, address, }) => {
|
|
|
30462
33798
|
};
|
|
30463
33799
|
const useSolanaNativeBalance = ({ chain, address, }) => {
|
|
30464
33800
|
const { nativeToken } = useNativeTokenForChain(chain);
|
|
30465
|
-
const { data: rawBalance, isLoading } =
|
|
33801
|
+
const { data: rawBalance, isLoading } = useQuery(keys().balance(chain?.chainId, nativeToken?.address, address), async () => {
|
|
30466
33802
|
const balance = await getSolanaNativeBalance(address);
|
|
30467
33803
|
return formatBNToReadable(balance, nativeToken.decimals);
|
|
30468
33804
|
}, {
|
|
@@ -30706,7 +34042,7 @@ const useEvmBalance = ({ chain, token, userAddress, enabled = true, refreshInter
|
|
|
30706
34042
|
};
|
|
30707
34043
|
const useCosmosBalance = ({ chain, token, userAddress, enabled = true, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, }) => {
|
|
30708
34044
|
const { isConnected } = useCosmosContext();
|
|
30709
|
-
const { data: balance = "0", isLoading } =
|
|
34045
|
+
const { data: balance = "0", isLoading } = useQuery({
|
|
30710
34046
|
queryKey: keys().balance(chain?.chainId, token?.address, userAddress),
|
|
30711
34047
|
queryFn: async () => {
|
|
30712
34048
|
if (!userAddress || !token || chain?.chainType !== squidTypes.ChainType.COSMOS) {
|
|
@@ -30729,7 +34065,7 @@ const useCosmosBalance = ({ chain, token, userAddress, enabled = true, refreshIn
|
|
|
30729
34065
|
return { balance, isLoading };
|
|
30730
34066
|
};
|
|
30731
34067
|
const useSolanaBalance = ({ chain, token, userAddress, enabled = true, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, }) => {
|
|
30732
|
-
const { data: balance = "0", isLoading } =
|
|
34068
|
+
const { data: balance = "0", isLoading } = useQuery({
|
|
30733
34069
|
queryKey: keys().balance(chain?.chainId, token?.address, userAddress),
|
|
30734
34070
|
queryFn: async () => {
|
|
30735
34071
|
if (!userAddress || !token)
|
|
@@ -30760,7 +34096,7 @@ const useBitcoinBalance = ({ userAddress, chain, }) => {
|
|
|
30760
34096
|
return { balance, isLoading };
|
|
30761
34097
|
};
|
|
30762
34098
|
const useSuiBalance = ({ chain, token, userAddress, enabled = true, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, }) => {
|
|
30763
|
-
const { data: balance = "0", isLoading } =
|
|
34099
|
+
const { data: balance = "0", isLoading } = useQuery({
|
|
30764
34100
|
queryKey: keys().balance(chain?.chainId, token?.address, userAddress),
|
|
30765
34101
|
queryFn: async () => {
|
|
30766
34102
|
if (!userAddress ||
|
|
@@ -30783,7 +34119,7 @@ const useSuiBalance = ({ chain, token, userAddress, enabled = true, refreshInter
|
|
|
30783
34119
|
return { balance, isLoading };
|
|
30784
34120
|
};
|
|
30785
34121
|
const useXrplBalance = ({ userAddress, chain, enabled, token, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, }) => {
|
|
30786
|
-
const { data: balance = "0", isLoading } =
|
|
34122
|
+
const { data: balance = "0", isLoading } = useQuery({
|
|
30787
34123
|
queryKey: keys().balance(chain?.chainId, token?.address, userAddress),
|
|
30788
34124
|
queryFn: async () => {
|
|
30789
34125
|
if (!userAddress || !token || !chain)
|
|
@@ -30803,7 +34139,7 @@ const useXrplBalance = ({ userAddress, chain, enabled, token, refreshIntervalMs
|
|
|
30803
34139
|
return { balance, isLoading };
|
|
30804
34140
|
};
|
|
30805
34141
|
const useStellarBalance = ({ userAddress, chain, enabled, token, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, }) => {
|
|
30806
|
-
const { data: balance = "0", isLoading } =
|
|
34142
|
+
const { data: balance = "0", isLoading } = useQuery({
|
|
30807
34143
|
queryKey: keys().balance(chain?.chainId, token?.address, userAddress),
|
|
30808
34144
|
queryFn: async () => {
|
|
30809
34145
|
if (!userAddress || !token || !chain)
|
|
@@ -30907,7 +34243,7 @@ const useMultiChainBalance = ({ chain, token, userAddress, enabled = true, }) =>
|
|
|
30907
34243
|
|
|
30908
34244
|
const useMultipleTokenPrices = (tokens) => {
|
|
30909
34245
|
const coingeckoIds = Array.from(new Set(tokens.map((token) => token.coingeckoId).filter(Boolean)));
|
|
30910
|
-
const coinGeckoQuery =
|
|
34246
|
+
const coinGeckoQuery = useQuery({
|
|
30911
34247
|
queryKey: keys().coinGeckoPrices(coingeckoIds),
|
|
30912
34248
|
queryFn: async () => {
|
|
30913
34249
|
if (coingeckoIds.length === 0)
|
|
@@ -30954,7 +34290,7 @@ const useMultipleTokenPrices = (tokens) => {
|
|
|
30954
34290
|
* @returns
|
|
30955
34291
|
*/
|
|
30956
34292
|
const useSingleTokenPrice = (tokenData) => {
|
|
30957
|
-
const queryClient =
|
|
34293
|
+
const queryClient = useQueryClient();
|
|
30958
34294
|
const [tokenPrice, setTokenPrice] = React.useState(tokenData?.usdPrice);
|
|
30959
34295
|
React.useEffect(() => {
|
|
30960
34296
|
const updatePrice = (data) => {
|
|
@@ -31090,13 +34426,13 @@ const fetchTokensData = async (tokens) => {
|
|
|
31090
34426
|
|
|
31091
34427
|
// Coingecko has a limit for some queries for multiple tokens, not sure what it is, but let's use 100 for now
|
|
31092
34428
|
const useHistoricalData = (coingeckoId, timeFrame) => {
|
|
31093
|
-
return
|
|
34429
|
+
return useQuery(keys().historicalData(coingeckoId, timeFrame), () => fetchHistoricalData(coingeckoId, timeFrame), {
|
|
31094
34430
|
staleTime: 5 * 60 * 1000,
|
|
31095
34431
|
enabled: !!coingeckoId,
|
|
31096
34432
|
});
|
|
31097
34433
|
};
|
|
31098
34434
|
const useTokensData = (tokens) => {
|
|
31099
|
-
return
|
|
34435
|
+
return useQuery(keys().tokensData(tokens), () => fetchTokensData(tokens), {
|
|
31100
34436
|
staleTime: 60 * 1000,
|
|
31101
34437
|
enabled: !!tokens && tokens.length > 0,
|
|
31102
34438
|
});
|
|
@@ -31324,7 +34660,7 @@ const getProposedGasDestinationAmount = (destChainNativeToken) => {
|
|
|
31324
34660
|
};
|
|
31325
34661
|
|
|
31326
34662
|
function useSendTransactionGas({ chain, token, from, }) {
|
|
31327
|
-
return
|
|
34663
|
+
return useQuery({
|
|
31328
34664
|
queryKey: keys().sendTransactionGas(chain?.chainId, token?.address, from),
|
|
31329
34665
|
queryFn: async () => {
|
|
31330
34666
|
if (!chain || !token)
|
|
@@ -31613,18 +34949,15 @@ async function sendTransactionXrpl({ amount, signer, to, token, from, }) {
|
|
|
31613
34949
|
txHash: hash,
|
|
31614
34950
|
};
|
|
31615
34951
|
}
|
|
31616
|
-
const
|
|
31617
|
-
if (!asset) {
|
|
31618
|
-
throw new Error("Invalid asset");
|
|
31619
|
-
}
|
|
34952
|
+
const [currency, issuer] = token.address.split(".");
|
|
31620
34953
|
const amountFormatted = formatBNToReadable(amount, token.decimals);
|
|
31621
34954
|
const { hash } = await signer.signAndSubmit({
|
|
31622
34955
|
network: xrplNetwork,
|
|
31623
34956
|
tx: {
|
|
31624
34957
|
...baseTransaction,
|
|
31625
34958
|
Amount: {
|
|
31626
|
-
currency
|
|
31627
|
-
issuer
|
|
34959
|
+
currency,
|
|
34960
|
+
issuer,
|
|
31628
34961
|
value: amountFormatted,
|
|
31629
34962
|
},
|
|
31630
34963
|
},
|
|
@@ -31702,7 +35035,7 @@ function useSendTransaction({ to, amount, token, chain, }) {
|
|
|
31702
35035
|
chain,
|
|
31703
35036
|
});
|
|
31704
35037
|
const solanaClient = useSolanaConnection();
|
|
31705
|
-
const queryClient =
|
|
35038
|
+
const queryClient = useQueryClient();
|
|
31706
35039
|
const { nativeToken } = useNativeTokenForChain(chain);
|
|
31707
35040
|
const getTransaction = useSendTransactionStore((state) => state.getTransaction);
|
|
31708
35041
|
const setTransactionState = useSendTransactionStore((state) => state.setTransactionState);
|
|
@@ -31755,7 +35088,7 @@ function useSendTransaction({ to, amount, token, chain, }) {
|
|
|
31755
35088
|
connectedAddress.address,
|
|
31756
35089
|
to,
|
|
31757
35090
|
]);
|
|
31758
|
-
const sendTransactionMutation =
|
|
35091
|
+
const sendTransactionMutation = useMutation({
|
|
31759
35092
|
mutationFn: async () => {
|
|
31760
35093
|
const allParamsDefined = token && to && amount && chain;
|
|
31761
35094
|
if (!allParamsDefined)
|
|
@@ -32273,7 +35606,7 @@ const useAllTransactionsStatus = ({ enabled }) => {
|
|
|
32273
35606
|
};
|
|
32274
35607
|
}
|
|
32275
35608
|
});
|
|
32276
|
-
const queries =
|
|
35609
|
+
const queries = useQueries({
|
|
32277
35610
|
queries: enabled && config.apiUrl ? statusQueries : [],
|
|
32278
35611
|
});
|
|
32279
35612
|
return {
|
|
@@ -32325,7 +35658,7 @@ const useErc20Allowance = ({ tokenAddress, ownerAddress, spenderAddress, amount
|
|
|
32325
35658
|
const useApproval = ({ squidRoute, }) => {
|
|
32326
35659
|
const { data: walletClient } = wagmi.useWalletClient();
|
|
32327
35660
|
const publicClient = wagmi.usePublicClient();
|
|
32328
|
-
const queryClient =
|
|
35661
|
+
const queryClient = useQueryClient();
|
|
32329
35662
|
const squid = useSquidStore((state) => state.squid);
|
|
32330
35663
|
const { fromChain, fromToken, fromPrice, isSameChain } = useSwap();
|
|
32331
35664
|
const { evmSigner } = useSigner({ chain: fromChain });
|
|
@@ -32357,7 +35690,7 @@ const useApproval = ({ squidRoute, }) => {
|
|
|
32357
35690
|
* On Error: Showing the error message if any
|
|
32358
35691
|
* @returns {boolean} approved
|
|
32359
35692
|
*/
|
|
32360
|
-
const routeApproved =
|
|
35693
|
+
const routeApproved = useQuery(keys().routeApproved(squidRoute, allowanceInWei), async () => {
|
|
32361
35694
|
if (erc20AllowanceQueryEnabled) {
|
|
32362
35695
|
return hasAllowance;
|
|
32363
35696
|
}
|
|
@@ -32455,7 +35788,7 @@ const useApproval = ({ squidRoute, }) => {
|
|
|
32455
35788
|
/**
|
|
32456
35789
|
* Manually approve route if necessary
|
|
32457
35790
|
*/
|
|
32458
|
-
const approveRoute =
|
|
35791
|
+
const approveRoute = useMutation(async () => {
|
|
32459
35792
|
try {
|
|
32460
35793
|
if (fromToken?.address.toLowerCase() ===
|
|
32461
35794
|
nativeEvmTokenAddress.toLowerCase()) {
|
|
@@ -35556,7 +38889,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35556
38889
|
const { findToken } = useSquidTokens();
|
|
35557
38890
|
const solanaConnection = useSolanaConnection();
|
|
35558
38891
|
const { findChain } = useSquidChains();
|
|
35559
|
-
const queryClient =
|
|
38892
|
+
const queryClient = useQueryClient();
|
|
35560
38893
|
const { connectedWalletsByChainType } = useWallet();
|
|
35561
38894
|
const squid = useSquidStore((state) => state.squid);
|
|
35562
38895
|
const getTransaction = useTransactionStore((state) => state.getTransaction);
|
|
@@ -35636,7 +38969,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35636
38969
|
txHash,
|
|
35637
38970
|
}, findToken);
|
|
35638
38971
|
}, [findToken]);
|
|
35639
|
-
const swapMutationCosmos =
|
|
38972
|
+
const swapMutationCosmos = useMutation(async ({ id, route }) => {
|
|
35640
38973
|
const fromChainId = route?.params.fromChain;
|
|
35641
38974
|
if (cosmosSigner && fromChainId) {
|
|
35642
38975
|
try {
|
|
@@ -35744,7 +39077,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35744
39077
|
throw error;
|
|
35745
39078
|
}
|
|
35746
39079
|
}, [replaceSwapTransactionNonce, setTransactionState, sourceUserAddress]);
|
|
35747
|
-
const swapMutationEvm =
|
|
39080
|
+
const swapMutationEvm = useMutation(async ({ id, route }) => {
|
|
35748
39081
|
await changeNetworkIfNeeded.mutateAsync();
|
|
35749
39082
|
if (!route || !squid || !evmSigner) {
|
|
35750
39083
|
throw new Error("Need all parameters");
|
|
@@ -35801,7 +39134,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35801
39134
|
});
|
|
35802
39135
|
}
|
|
35803
39136
|
});
|
|
35804
|
-
const swapMutationSolana =
|
|
39137
|
+
const swapMutationSolana = useMutation(async ({ id, route }) => {
|
|
35805
39138
|
try {
|
|
35806
39139
|
if (!route) {
|
|
35807
39140
|
throw new Error("Route is required");
|
|
@@ -35920,7 +39253,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35920
39253
|
});
|
|
35921
39254
|
},
|
|
35922
39255
|
});
|
|
35923
|
-
const swapMutationBitcoin =
|
|
39256
|
+
const swapMutationBitcoin = useMutation(async ({ id, route }) => {
|
|
35924
39257
|
const { depositAddress, amount: sendAmount, chainflipStatusTrackingId, } = useDepositAddressStore.getState().deposit ?? {};
|
|
35925
39258
|
if (!depositAddress) {
|
|
35926
39259
|
throw new Error(`Invalid deposit address: ${depositAddress}`);
|
|
@@ -35964,7 +39297,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
35964
39297
|
throw new Error("Need all parameters");
|
|
35965
39298
|
}
|
|
35966
39299
|
});
|
|
35967
|
-
const swapMutationXrpl =
|
|
39300
|
+
const swapMutationXrpl = useMutation(async ({ id, route }) => {
|
|
35968
39301
|
if (!route?.transactionRequest || !xrplSigner) {
|
|
35969
39302
|
throw new Error("Need all parameters");
|
|
35970
39303
|
}
|
|
@@ -36003,7 +39336,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
36003
39336
|
throw new Error(`Transaction failed with status: ${txRes.status}`);
|
|
36004
39337
|
}
|
|
36005
39338
|
}, {});
|
|
36006
|
-
const swapMutationSui =
|
|
39339
|
+
const swapMutationSui = useMutation(async ({ id, route }) => {
|
|
36007
39340
|
if (!route || !suiSigner || !fromChain) {
|
|
36008
39341
|
throw new Error("Need all parameters");
|
|
36009
39342
|
}
|
|
@@ -36057,7 +39390,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
36057
39390
|
digest: txHash,
|
|
36058
39391
|
});
|
|
36059
39392
|
}, {});
|
|
36060
|
-
const swapMutationStellar =
|
|
39393
|
+
const swapMutationStellar = useMutation(async ({ id, route }) => {
|
|
36061
39394
|
if (!stellarSigner || !route || !squid || !fromChain) {
|
|
36062
39395
|
throw new Error("Need all parameters");
|
|
36063
39396
|
}
|
|
@@ -36162,7 +39495,7 @@ const useExecuteTransaction = (squidRoute) => {
|
|
|
36162
39495
|
setTransactionStoreState,
|
|
36163
39496
|
squidRoute,
|
|
36164
39497
|
]);
|
|
36165
|
-
const swapMutation =
|
|
39498
|
+
const swapMutation = useMutation(async (mutationParams) => {
|
|
36166
39499
|
if (!mutationParams.route)
|
|
36167
39500
|
throw new Error("route is required");
|
|
36168
39501
|
const sourceChain = findChain(mutationParams.route.params?.fromChain);
|
|
@@ -36252,7 +39585,7 @@ const useGetRoute = () => {
|
|
|
36252
39585
|
const config = useConfigStore((state) => state.config);
|
|
36253
39586
|
const squid = useSquidStore((state) => state.squid);
|
|
36254
39587
|
const { swapRoute } = useSwapRoutePersistStore();
|
|
36255
|
-
const queryClient =
|
|
39588
|
+
const queryClient = useQueryClient();
|
|
36256
39589
|
const previousParamsHashRef = React.useRef();
|
|
36257
39590
|
/**
|
|
36258
39591
|
* Dispatch event when requesting a quote for swap
|
|
@@ -36303,7 +39636,7 @@ const useGetRoute = () => {
|
|
|
36303
39636
|
* These data will be used to trigger the transaction
|
|
36304
39637
|
* @returns {Route} Route data
|
|
36305
39638
|
*/
|
|
36306
|
-
return
|
|
39639
|
+
return useMutation(async ({ fromChain, toChain, fromToken, toToken, sourceUserAddress, destinationAddress, fromPrice, bypassGuardrails, quoteOnly, fromChainType, postHook, preHook, overrideGasRefundAddress, }) => {
|
|
36307
39640
|
if (!fromChain || !toChain || !fromToken || !toToken || !fromPrice) {
|
|
36308
39641
|
return undefined;
|
|
36309
39642
|
}
|
|
@@ -36398,14 +39731,14 @@ refetchIntervalInBackground = false, refetchInterval = 30000, quoteOnly = true,
|
|
|
36398
39731
|
fromPrice !== "0" &&
|
|
36399
39732
|
toChain?.chainId !== undefined &&
|
|
36400
39733
|
toToken?.address !== undefined;
|
|
36401
|
-
const queryClient =
|
|
39734
|
+
const queryClient = useQueryClient();
|
|
36402
39735
|
const cachedRouteData = queryClient.getQueryData(squidRouteQueryKeys);
|
|
36403
39736
|
/**
|
|
36404
39737
|
* Fetching route data from the API
|
|
36405
39738
|
* These data will be used to trigger the transaction
|
|
36406
39739
|
* @returns {Route} Route data
|
|
36407
39740
|
*/
|
|
36408
|
-
const squidRoute =
|
|
39741
|
+
const squidRoute = useQuery(squidRouteQueryKeys, async () => {
|
|
36409
39742
|
const route = await getRouteMutation.mutateAsync({
|
|
36410
39743
|
fromChain: fromChain?.chainId,
|
|
36411
39744
|
toChain: toChain?.chainId,
|
|
@@ -36465,7 +39798,7 @@ function useSendTransactionStatus({ chain, txHash, }) {
|
|
|
36465
39798
|
transactionId: txHash,
|
|
36466
39799
|
txType: exports.HistoryTxType.SEND,
|
|
36467
39800
|
});
|
|
36468
|
-
const { data: status } =
|
|
39801
|
+
const { data: status } = useQuery({
|
|
36469
39802
|
queryKey: keys().sendTransactionStatus(txHash, chain?.chainId),
|
|
36470
39803
|
queryFn: async () => {
|
|
36471
39804
|
if (!chain || !txHash)
|
|
@@ -36540,7 +39873,7 @@ const useSwapTransactionStatus = ({ transaction, retry = 25, refetchOnWindowFocu
|
|
|
36540
39873
|
apiUrl: latestConfig.apiUrl,
|
|
36541
39874
|
});
|
|
36542
39875
|
}, [transaction]);
|
|
36543
|
-
const transactionStatusQuery =
|
|
39876
|
+
const transactionStatusQuery = useQuery(keys().swapTransactionStatus(transaction?.transactionId), fetchTransactionStatusWithLatestConfig, {
|
|
36544
39877
|
enabled: enabled &&
|
|
36545
39878
|
transaction?.transactionId !== "0" &&
|
|
36546
39879
|
!!transaction?.transactionId &&
|
|
@@ -36638,7 +39971,7 @@ const hashStringToIndex = (str, max) => {
|
|
|
36638
39971
|
* @returns {string} - The selected avatar image URL.
|
|
36639
39972
|
*/
|
|
36640
39973
|
const useAvatar = (seed = zeroAddress) => {
|
|
36641
|
-
const { data: avatar } =
|
|
39974
|
+
const { data: avatar } = useQuery({
|
|
36642
39975
|
queryKey: ["avatar", seed],
|
|
36643
39976
|
queryFn: () => {
|
|
36644
39977
|
const index = hashStringToIndex(seed, avatarImages.length);
|
|
@@ -36688,7 +40021,7 @@ const useAddToken = (chainToCompare, tokenToCompare) => {
|
|
|
36688
40021
|
/**
|
|
36689
40022
|
* Add token to wallet
|
|
36690
40023
|
*/
|
|
36691
|
-
const addToken =
|
|
40024
|
+
const addToken = useMutation(async ({ chain: _chain, token: _token, }) => {
|
|
36692
40025
|
const token = _token ?? tokenToCompare;
|
|
36693
40026
|
const chain = _chain ?? chainToCompare;
|
|
36694
40027
|
if (token && chain?.chainType === squidTypes.ChainType.EVM) {
|
|
@@ -36851,11 +40184,11 @@ const MAX_IOU_AMOUNT = "9999999999999999e80";
|
|
|
36851
40184
|
const DEFAULT_REFETCH_INTERVAL = 20_000;
|
|
36852
40185
|
function useXrplTrustLine({ address, chain, token, amount }) {
|
|
36853
40186
|
const { xrplSigner } = useSigner({ chain });
|
|
36854
|
-
const queryClient =
|
|
40187
|
+
const queryClient = useQueryClient();
|
|
36855
40188
|
/**
|
|
36856
40189
|
* Retrieves the destination account's trust line data for the given token
|
|
36857
40190
|
*/
|
|
36858
|
-
const trustLineQuery =
|
|
40191
|
+
const trustLineQuery = useQuery({
|
|
36859
40192
|
queryKey: keys().xrplTrustLine(token?.address, chain?.chainId, address),
|
|
36860
40193
|
queryFn: async () => {
|
|
36861
40194
|
if (chain?.chainType !== squidTypes.ChainType.XRPL ||
|
|
@@ -36865,12 +40198,12 @@ function useXrplTrustLine({ address, chain, token, amount }) {
|
|
|
36865
40198
|
if (!address || !isXrplAddressValid(address)) {
|
|
36866
40199
|
return null;
|
|
36867
40200
|
}
|
|
36868
|
-
const
|
|
36869
|
-
if (!
|
|
40201
|
+
const [currency, issuer] = token.address.split(".");
|
|
40202
|
+
if (!currency || !issuer) {
|
|
36870
40203
|
return null;
|
|
36871
40204
|
}
|
|
36872
40205
|
const xrplClient = await getClient(chain);
|
|
36873
|
-
const trustLine = await xrplClient.getTrustLine(address,
|
|
40206
|
+
const trustLine = await xrplClient.getTrustLine(address, issuer, currency);
|
|
36874
40207
|
return trustLine;
|
|
36875
40208
|
},
|
|
36876
40209
|
enabled: !!address &&
|
|
@@ -36881,7 +40214,7 @@ function useXrplTrustLine({ address, chain, token, amount }) {
|
|
|
36881
40214
|
/**
|
|
36882
40215
|
* Creates a trust line where the destination account authorizes to receive the given token
|
|
36883
40216
|
*/
|
|
36884
|
-
const createTrustLine =
|
|
40217
|
+
const createTrustLine = useMutation({
|
|
36885
40218
|
mutationFn: async () => {
|
|
36886
40219
|
try {
|
|
36887
40220
|
if (!xrplSigner) {
|
|
@@ -36930,7 +40263,7 @@ function useXrplTrustLine({ address, chain, token, amount }) {
|
|
|
36930
40263
|
/**
|
|
36931
40264
|
* Checks if the destination account has created a trust line to receive the given token.
|
|
36932
40265
|
*/
|
|
36933
|
-
const isTrustLineApproved =
|
|
40266
|
+
const isTrustLineApproved = useQuery({
|
|
36934
40267
|
queryKey: keys().isXrplTrustLineApproved(address, token?.chainId, token?.type, token?.address, trustLineQuery.data?.limit, amount),
|
|
36935
40268
|
queryFn: async () => {
|
|
36936
40269
|
if (token?.type !== squidTypes.ChainType.XRPL) {
|
|
@@ -36958,7 +40291,7 @@ function useXrplTrustLine({ address, chain, token, amount }) {
|
|
|
36958
40291
|
* Checks if the destination account is activated (holds at least the minimum XRP reserve balance)
|
|
36959
40292
|
* XRPL accounts need to have a minimum balance before they can receive payments
|
|
36960
40293
|
*/
|
|
36961
|
-
const accountActivatedInfo =
|
|
40294
|
+
const accountActivatedInfo = useQuery({
|
|
36962
40295
|
queryKey: keys().xrplAccountActivatedInfo(address, chain?.chainId, chain?.chainType),
|
|
36963
40296
|
queryFn: async () => {
|
|
36964
40297
|
if (chain?.chainType !== squidTypes.ChainType.XRPL ||
|
|
@@ -37002,7 +40335,7 @@ const defaultOptions = {
|
|
|
37002
40335
|
},
|
|
37003
40336
|
};
|
|
37004
40337
|
|
|
37005
|
-
const queryClient = new
|
|
40338
|
+
const queryClient = new QueryClient({ defaultOptions });
|
|
37006
40339
|
const verifyIntegratorIdValidity = (integratorId) => {
|
|
37007
40340
|
if (!integratorId) {
|
|
37008
40341
|
throw new Error("Integrator ID is required");
|
|
@@ -37098,7 +40431,7 @@ const SquidProvider = ({ children, config, placeholder, }) => {
|
|
|
37098
40431
|
initializeSdk();
|
|
37099
40432
|
}, [initializeSdk]);
|
|
37100
40433
|
return wagmiConfig ? (React.createElement(wagmi.WagmiProvider, { reconnectOnMount: false, config: wagmiConfig },
|
|
37101
|
-
React.createElement(
|
|
40434
|
+
React.createElement(QueryClientProvider, { client: queryClient },
|
|
37102
40435
|
React.createElement(StellarProvider, null,
|
|
37103
40436
|
React.createElement(EvmProvider, null,
|
|
37104
40437
|
React.createElement(XrplProvider, null,
|
|
@@ -37196,9 +40529,6 @@ exports.getSecretNetworkBalances = getSecretNetworkBalances;
|
|
|
37196
40529
|
exports.getSendTxStatusRefetchInterval = getSendTxStatusRefetchInterval;
|
|
37197
40530
|
exports.getSourceExplorerTxUrl = getSourceExplorerTxUrl;
|
|
37198
40531
|
exports.getStatusCode = getStatusCode;
|
|
37199
|
-
exports.getStellarHorizonApiUrl = getStellarHorizonApiUrl;
|
|
37200
|
-
exports.getStellarNetwork = getStellarNetwork;
|
|
37201
|
-
exports.getStellarTrustLineAsset = getStellarTrustLineAsset;
|
|
37202
40532
|
exports.getStepStatuses = getStepStatuses;
|
|
37203
40533
|
exports.getStepsInfos = getStepsInfos;
|
|
37204
40534
|
exports.getSuggestedAmountsForCurrency = getSuggestedAmountsForCurrency;
|
|
@@ -37234,13 +40564,8 @@ exports.isHistoryTransactionWarning = isHistoryTransactionWarning;
|
|
|
37234
40564
|
exports.isProblematicConnector = isProblematicConnector;
|
|
37235
40565
|
exports.isSolanaAddressValid = isSolanaAddressValid;
|
|
37236
40566
|
exports.isStatusError = isStatusError;
|
|
37237
|
-
exports.isStellarAddressValid = isStellarAddressValid;
|
|
37238
|
-
exports.isStellarIssuedToken = isStellarIssuedToken;
|
|
37239
|
-
exports.isStellarToken = isStellarToken;
|
|
37240
40567
|
exports.isSwapRouteError = isSwapRouteError;
|
|
37241
40568
|
exports.isUserRejectionError = isUserRejectionError;
|
|
37242
|
-
exports.isValidHorizonAsset = isValidHorizonAsset;
|
|
37243
|
-
exports.isValidIssuedAsset = isValidIssuedAsset;
|
|
37244
40569
|
exports.isWalletAddressValid = isWalletAddressValid;
|
|
37245
40570
|
exports.isXamanXAppContext = isXamanXAppContext;
|
|
37246
40571
|
exports.isXionSmartContractAddress = isXionSmartContractAddress;
|
|
@@ -37259,7 +40584,6 @@ exports.normalizeTokenSymbol = normalizeTokenSymbol;
|
|
|
37259
40584
|
exports.parseEvmAddress = parseEvmAddress;
|
|
37260
40585
|
exports.parseToBigInt = parseToBigInt;
|
|
37261
40586
|
exports.parseXrplPaymentTx = parseXrplPaymentTx;
|
|
37262
|
-
exports.parseXrplTokenAddress = parseXrplTokenAddress;
|
|
37263
40587
|
exports.populateWallets = populateWallets;
|
|
37264
40588
|
exports.randomIntFromInterval = randomIntFromInterval;
|
|
37265
40589
|
exports.redirectToExtensionsStore = redirectToExtensionsStore;
|
|
@@ -37272,7 +40596,6 @@ exports.sortAddressBook = sortAddressBook;
|
|
|
37272
40596
|
exports.sortAllTokens = sortAllTokens;
|
|
37273
40597
|
exports.sortTokensBySharedSubgraphIds = sortTokensBySharedSubgraphIds;
|
|
37274
40598
|
exports.sortWallets = sortWallets;
|
|
37275
|
-
exports.stellarAddressToScVal = stellarAddressToScVal;
|
|
37276
40599
|
exports.suggestChainOrThrow = suggestChainOrThrow;
|
|
37277
40600
|
exports.transactionErrorCode = transactionErrorCode;
|
|
37278
40601
|
exports.trimExtraDecimals = trimExtraDecimals;
|
|
@@ -37338,7 +40661,6 @@ exports.useSquidQueryClient = useSquidQueryClient;
|
|
|
37338
40661
|
exports.useSquidStore = useSquidStore;
|
|
37339
40662
|
exports.useSquidTokens = useSquidTokens;
|
|
37340
40663
|
exports.useStellarAccountActivation = useStellarAccountActivation;
|
|
37341
|
-
exports.useStellarTrustLine = useStellarTrustLine;
|
|
37342
40664
|
exports.useSuggestedFiatAmounts = useSuggestedFiatAmounts;
|
|
37343
40665
|
exports.useSwap = useSwap;
|
|
37344
40666
|
exports.useSwapRoutePersistStore = useSwapRoutePersistStore;
|
|
@@ -37354,4 +40676,4 @@ exports.useXrplTrustLine = useXrplTrustLine;
|
|
|
37354
40676
|
exports.waitForReceiptWithRetry = waitForReceiptWithRetry;
|
|
37355
40677
|
exports.walletIconBaseUrl = walletIconBaseUrl;
|
|
37356
40678
|
exports.walletSupportsChainType = walletSupportsChainType;
|
|
37357
|
-
//# sourceMappingURL=index-
|
|
40679
|
+
//# sourceMappingURL=index-DOoP75Yp.js.map
|