@tanstack/query-core 5.0.0-alpha.2 → 5.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/build/lib/infiniteQueryBehavior.d.ts +2 -2
  2. package/build/lib/infiniteQueryBehavior.esm.js +47 -45
  3. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  4. package/build/lib/infiniteQueryBehavior.js +47 -45
  5. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  6. package/build/lib/infiniteQueryBehavior.mjs +47 -45
  7. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  8. package/build/lib/infiniteQueryObserver.d.ts +6 -6
  9. package/build/lib/infiniteQueryObserver.esm.js +4 -6
  10. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  11. package/build/lib/infiniteQueryObserver.js +4 -6
  12. package/build/lib/infiniteQueryObserver.js.map +1 -1
  13. package/build/lib/infiniteQueryObserver.mjs +4 -6
  14. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  15. package/build/lib/mutation.d.ts +2 -1
  16. package/build/lib/mutation.esm.js +10 -2
  17. package/build/lib/mutation.esm.js.map +1 -1
  18. package/build/lib/mutation.js +10 -2
  19. package/build/lib/mutation.js.map +1 -1
  20. package/build/lib/mutation.mjs +10 -2
  21. package/build/lib/mutation.mjs.map +1 -1
  22. package/build/lib/mutationCache.d.ts +2 -2
  23. package/build/lib/mutationCache.esm.js.map +1 -1
  24. package/build/lib/mutationCache.js.map +1 -1
  25. package/build/lib/mutationCache.mjs.map +1 -1
  26. package/build/lib/mutationObserver.esm.js +1 -0
  27. package/build/lib/mutationObserver.esm.js.map +1 -1
  28. package/build/lib/mutationObserver.js +1 -0
  29. package/build/lib/mutationObserver.js.map +1 -1
  30. package/build/lib/mutationObserver.mjs +1 -0
  31. package/build/lib/mutationObserver.mjs.map +1 -1
  32. package/build/lib/query.esm.js.map +1 -1
  33. package/build/lib/query.js.map +1 -1
  34. package/build/lib/query.mjs.map +1 -1
  35. package/build/lib/queryCache.d.ts +2 -2
  36. package/build/lib/queryCache.esm.js.map +1 -1
  37. package/build/lib/queryCache.js.map +1 -1
  38. package/build/lib/queryCache.mjs.map +1 -1
  39. package/build/lib/tests/utils.d.ts +4 -5
  40. package/build/lib/types.d.ts +3 -3
  41. package/build/umd/index.development.js +62 -53
  42. package/build/umd/index.development.js.map +1 -1
  43. package/build/umd/index.production.js +1 -1
  44. package/build/umd/index.production.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/infiniteQueryBehavior.ts +51 -60
  47. package/src/infiniteQueryObserver.ts +15 -10
  48. package/src/mutation.ts +14 -5
  49. package/src/mutationCache.ts +2 -2
  50. package/src/mutationObserver.ts +1 -0
  51. package/src/query.ts +7 -4
  52. package/src/queryCache.ts +5 -2
  53. package/src/tests/focusManager.test.tsx +12 -14
  54. package/src/tests/hydration.test.tsx +15 -14
  55. package/src/tests/infiniteQueryBehavior.test.tsx +7 -9
  56. package/src/tests/infiniteQueryObserver.test.tsx +62 -1
  57. package/src/tests/mutationCache.test.tsx +11 -10
  58. package/src/tests/mutationObserver.test.tsx +3 -2
  59. package/src/tests/mutations.test.tsx +41 -9
  60. package/src/tests/notifyManager.test.tsx +7 -6
  61. package/src/tests/onlineManager.test.tsx +12 -17
  62. package/src/tests/queriesObserver.test.tsx +18 -17
  63. package/src/tests/query.test.tsx +18 -17
  64. package/src/tests/queryCache.test.tsx +14 -13
  65. package/src/tests/queryClient.test.tsx +49 -48
  66. package/src/tests/queryObserver.test.tsx +10 -9
  67. package/src/tests/utils.test.tsx +2 -1
  68. package/src/tests/utils.ts +5 -4
  69. package/src/types.ts +7 -1
@@ -1154,18 +1154,26 @@
1154
1154
 
1155
1155
  class Mutation extends Removable {
1156
1156
  #observers;
1157
+ #defaultOptions;
1157
1158
  #mutationCache;
1158
1159
  #retryer;
1159
1160
  constructor(config) {
1160
1161
  super();
1161
- this.options = config.options;
1162
1162
  this.mutationId = config.mutationId;
1163
+ this.#defaultOptions = config.defaultOptions;
1163
1164
  this.#mutationCache = config.mutationCache;
1164
1165
  this.#observers = [];
1165
1166
  this.state = config.state || getDefaultState();
1166
- this.updateGcTime(this.options.gcTime);
1167
+ this.setOptions(config.options);
1167
1168
  this.scheduleGc();
1168
1169
  }
1170
+ setOptions(options) {
1171
+ this.options = {
1172
+ ...this.#defaultOptions,
1173
+ ...options
1174
+ };
1175
+ this.updateGcTime(this.options.gcTime);
1176
+ }
1169
1177
  get meta() {
1170
1178
  return this.options.meta;
1171
1179
  }
@@ -1446,12 +1454,15 @@
1446
1454
  function infiniteQueryBehavior() {
1447
1455
  return {
1448
1456
  onFetch: context => {
1449
- context.fetchFn = () => {
1457
+ context.fetchFn = async () => {
1450
1458
  const options = context.options;
1451
1459
  const direction = context.fetchOptions?.meta?.fetchMore?.direction;
1452
1460
  const oldPages = context.state.data?.pages || [];
1453
1461
  const oldPageParams = context.state.data?.pageParams || [];
1454
- let newPageParams = oldPageParams;
1462
+ const empty = {
1463
+ pages: [],
1464
+ pageParams: []
1465
+ };
1455
1466
  let cancelled = false;
1456
1467
  const addSignalProperty = object => {
1457
1468
  Object.defineProperty(object, 'signal', {
@@ -1471,25 +1482,14 @@
1471
1482
 
1472
1483
  // Get query function
1473
1484
  const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
1474
- const buildNewPages = (pages, param, page, previous) => {
1475
- const {
1476
- maxPages
1477
- } = context.options;
1478
- if (previous) {
1479
- newPageParams = addToStart(newPageParams, param, maxPages);
1480
- return addToStart(pages, page, maxPages);
1481
- }
1482
- newPageParams = addToEnd(newPageParams, param, maxPages);
1483
- return addToEnd(pages, page, maxPages);
1484
- };
1485
1485
 
1486
1486
  // Create function to fetch a page
1487
- const fetchPage = (pages, param, previous) => {
1487
+ const fetchPage = async (data, param, previous) => {
1488
1488
  if (cancelled) {
1489
1489
  return Promise.reject();
1490
1490
  }
1491
- if (typeof param === 'undefined' && pages.length) {
1492
- return Promise.resolve(pages);
1491
+ if (typeof param === 'undefined' && data.pages.length) {
1492
+ return Promise.resolve(data);
1493
1493
  }
1494
1494
  const queryFnContext = {
1495
1495
  queryKey: context.queryKey,
@@ -1497,69 +1497,79 @@
1497
1497
  meta: context.options.meta
1498
1498
  };
1499
1499
  addSignalProperty(queryFnContext);
1500
- const queryFnResult = queryFn(queryFnContext);
1501
- const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
1502
- return promise;
1500
+ const page = await queryFn(queryFnContext);
1501
+ const {
1502
+ maxPages
1503
+ } = context.options;
1504
+ const addTo = previous ? addToStart : addToEnd;
1505
+ return {
1506
+ pages: addTo(data.pages, page, maxPages),
1507
+ pageParams: addTo(data.pageParams, param, maxPages)
1508
+ };
1503
1509
  };
1504
- let promise;
1510
+ let result;
1505
1511
 
1506
1512
  // Fetch first page?
1507
1513
  if (!oldPages.length) {
1508
- promise = fetchPage([], options.defaultPageParam);
1514
+ result = await fetchPage(empty, options.defaultPageParam);
1509
1515
  }
1510
1516
 
1511
1517
  // fetch next / previous page?
1512
1518
  else if (direction) {
1513
1519
  const previous = direction === 'backward';
1514
- const param = previous ? getPreviousPageParam(options, oldPages) : getNextPageParam(options, oldPages);
1515
- promise = fetchPage(oldPages, param, previous);
1520
+ const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
1521
+ const oldData = {
1522
+ pages: oldPages,
1523
+ pageParams: oldPageParams
1524
+ };
1525
+ const param = pageParamFn(options, oldData);
1526
+ result = await fetchPage(oldData, param, previous);
1516
1527
  }
1517
1528
 
1518
1529
  // Refetch pages
1519
1530
  else {
1520
- newPageParams = [];
1521
-
1522
1531
  // Fetch first page
1523
- promise = fetchPage([], oldPageParams[0]);
1532
+ result = await fetchPage(empty, oldPageParams[0]);
1524
1533
 
1525
1534
  // Fetch remaining pages
1526
1535
  for (let i = 1; i < oldPages.length; i++) {
1527
- promise = promise.then(pages => {
1528
- const param = getNextPageParam(options, pages);
1529
- return fetchPage(pages, param);
1530
- });
1536
+ const param = getNextPageParam(options, result);
1537
+ result = await fetchPage(result, param);
1531
1538
  }
1532
1539
  }
1533
- const finalPromise = promise.then(pages => ({
1534
- pages,
1535
- pageParams: newPageParams
1536
- }));
1537
- return finalPromise;
1540
+ return result;
1538
1541
  };
1539
1542
  }
1540
1543
  };
1541
1544
  }
1542
- function getNextPageParam(options, pages) {
1543
- return options.getNextPageParam(pages[pages.length - 1], pages);
1545
+ function getNextPageParam(options, {
1546
+ pages,
1547
+ pageParams
1548
+ }) {
1549
+ const lastIndex = pages.length - 1;
1550
+ return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
1544
1551
  }
1545
- function getPreviousPageParam(options, pages) {
1546
- return options.getPreviousPageParam?.(pages[0], pages);
1552
+ function getPreviousPageParam(options, {
1553
+ pages,
1554
+ pageParams
1555
+ }) {
1556
+ return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
1547
1557
  }
1548
1558
 
1549
1559
  /**
1550
1560
  * Checks if there is a next page.
1551
1561
  */
1552
- function hasNextPage(options, pages) {
1553
- if (!pages) return false;
1554
- return typeof getNextPageParam(options, pages) !== 'undefined';
1562
+ function hasNextPage(options, data) {
1563
+ if (!data) return false;
1564
+ return typeof getNextPageParam(options, data) !== 'undefined';
1555
1565
  }
1556
1566
 
1557
1567
  /**
1558
1568
  * Checks if there is a previous page.
1559
1569
  */
1560
- function hasPreviousPage(options, pages) {
1561
- if (!pages || !options.getPreviousPageParam) return false;
1562
- return typeof getPreviousPageParam(options, pages) !== 'undefined';
1570
+ function hasPreviousPage(options, data) {
1571
+ if (!data || !options.getPreviousPageParam) return false;
1572
+ return typeof getPreviousPageParam(options, data) !== 'undefined';
1563
1573
  }
1564
1574
 
1565
1575
  // CLASS
@@ -2417,7 +2427,7 @@
2417
2427
  options.behavior = infiniteQueryBehavior();
2418
2428
  return super.getOptimisticResult(options);
2419
2429
  }
2420
- fetchNextPage(options = {}) {
2430
+ fetchNextPage(options) {
2421
2431
  return this.fetch({
2422
2432
  ...options,
2423
2433
  meta: {
@@ -2427,9 +2437,7 @@
2427
2437
  }
2428
2438
  });
2429
2439
  }
2430
- fetchPreviousPage({
2431
- ...options
2432
- } = {}) {
2440
+ fetchPreviousPage(options) {
2433
2441
  return this.fetch({
2434
2442
  ...options,
2435
2443
  meta: {
@@ -2454,8 +2462,8 @@
2454
2462
  ...result,
2455
2463
  fetchNextPage: this.fetchNextPage,
2456
2464
  fetchPreviousPage: this.fetchPreviousPage,
2457
- hasNextPage: hasNextPage(options, state.data?.pages),
2458
- hasPreviousPage: hasPreviousPage(options, state.data?.pages),
2465
+ hasNextPage: hasNextPage(options, state.data),
2466
+ hasPreviousPage: hasPreviousPage(options, state.data),
2459
2467
  isFetchingNextPage,
2460
2468
  isFetchingPreviousPage,
2461
2469
  isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
@@ -2493,6 +2501,7 @@
2493
2501
  observer: this
2494
2502
  });
2495
2503
  }
2504
+ this.#currentMutation?.setOptions(this.options);
2496
2505
  }
2497
2506
  onUnsubscribe() {
2498
2507
  if (!this.listeners.length) {