@tanstack/react-query 5.0.0-alpha.4 → 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.
@@ -1,4 +1 @@
1
- /**
2
- * @jest-environment node
3
- */
4
1
  export {};
@@ -1,4 +1,3 @@
1
- /// <reference types="jest" />
2
1
  /// <reference types="node" />
3
2
  import * as React from 'react';
4
3
  import { render } from '@testing-library/react';
@@ -10,8 +9,8 @@ export declare const Blink: ({ duration, children, }: {
10
9
  children: React.ReactNode;
11
10
  }) => JSX.Element;
12
11
  export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
13
- export declare function mockVisibilityState(value: DocumentVisibilityState): jest.SpyInstance<DocumentVisibilityState, [], any>;
14
- export declare function mockNavigatorOnLine(value: boolean): jest.SpyInstance<boolean, [], any>;
12
+ export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
13
+ export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
15
14
  export declare function queryKey(): Array<string>;
16
15
  export declare function sleep(timeout: number): Promise<void>;
17
16
  export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
@@ -1473,12 +1473,15 @@
1473
1473
  function infiniteQueryBehavior() {
1474
1474
  return {
1475
1475
  onFetch: context => {
1476
- context.fetchFn = () => {
1476
+ context.fetchFn = async () => {
1477
1477
  const options = context.options;
1478
1478
  const direction = context.fetchOptions?.meta?.fetchMore?.direction;
1479
1479
  const oldPages = context.state.data?.pages || [];
1480
1480
  const oldPageParams = context.state.data?.pageParams || [];
1481
- let newPageParams = oldPageParams;
1481
+ const empty = {
1482
+ pages: [],
1483
+ pageParams: []
1484
+ };
1482
1485
  let cancelled = false;
1483
1486
  const addSignalProperty = object => {
1484
1487
  Object.defineProperty(object, 'signal', {
@@ -1498,25 +1501,14 @@
1498
1501
 
1499
1502
  // Get query function
1500
1503
  const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
1501
- const buildNewPages = (pages, param, page, previous) => {
1502
- const {
1503
- maxPages
1504
- } = context.options;
1505
- if (previous) {
1506
- newPageParams = addToStart(newPageParams, param, maxPages);
1507
- return addToStart(pages, page, maxPages);
1508
- }
1509
- newPageParams = addToEnd(newPageParams, param, maxPages);
1510
- return addToEnd(pages, page, maxPages);
1511
- };
1512
1504
 
1513
1505
  // Create function to fetch a page
1514
- const fetchPage = (pages, param, previous) => {
1506
+ const fetchPage = async (data, param, previous) => {
1515
1507
  if (cancelled) {
1516
1508
  return Promise.reject();
1517
1509
  }
1518
- if (typeof param === 'undefined' && pages.length) {
1519
- return Promise.resolve(pages);
1510
+ if (typeof param === 'undefined' && data.pages.length) {
1511
+ return Promise.resolve(data);
1520
1512
  }
1521
1513
  const queryFnContext = {
1522
1514
  queryKey: context.queryKey,
@@ -1524,69 +1516,79 @@
1524
1516
  meta: context.options.meta
1525
1517
  };
1526
1518
  addSignalProperty(queryFnContext);
1527
- const queryFnResult = queryFn(queryFnContext);
1528
- const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
1529
- return promise;
1519
+ const page = await queryFn(queryFnContext);
1520
+ const {
1521
+ maxPages
1522
+ } = context.options;
1523
+ const addTo = previous ? addToStart : addToEnd;
1524
+ return {
1525
+ pages: addTo(data.pages, page, maxPages),
1526
+ pageParams: addTo(data.pageParams, param, maxPages)
1527
+ };
1530
1528
  };
1531
- let promise;
1529
+ let result;
1532
1530
 
1533
1531
  // Fetch first page?
1534
1532
  if (!oldPages.length) {
1535
- promise = fetchPage([], options.defaultPageParam);
1533
+ result = await fetchPage(empty, options.defaultPageParam);
1536
1534
  }
1537
1535
 
1538
1536
  // fetch next / previous page?
1539
1537
  else if (direction) {
1540
1538
  const previous = direction === 'backward';
1541
- const param = previous ? getPreviousPageParam(options, oldPages) : getNextPageParam(options, oldPages);
1542
- promise = fetchPage(oldPages, param, previous);
1539
+ const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
1540
+ const oldData = {
1541
+ pages: oldPages,
1542
+ pageParams: oldPageParams
1543
+ };
1544
+ const param = pageParamFn(options, oldData);
1545
+ result = await fetchPage(oldData, param, previous);
1543
1546
  }
1544
1547
 
1545
1548
  // Refetch pages
1546
1549
  else {
1547
- newPageParams = [];
1548
-
1549
1550
  // Fetch first page
1550
- promise = fetchPage([], oldPageParams[0]);
1551
+ result = await fetchPage(empty, oldPageParams[0]);
1551
1552
 
1552
1553
  // Fetch remaining pages
1553
1554
  for (let i = 1; i < oldPages.length; i++) {
1554
- promise = promise.then(pages => {
1555
- const param = getNextPageParam(options, pages);
1556
- return fetchPage(pages, param);
1557
- });
1555
+ const param = getNextPageParam(options, result);
1556
+ result = await fetchPage(result, param);
1558
1557
  }
1559
1558
  }
1560
- const finalPromise = promise.then(pages => ({
1561
- pages,
1562
- pageParams: newPageParams
1563
- }));
1564
- return finalPromise;
1559
+ return result;
1565
1560
  };
1566
1561
  }
1567
1562
  };
1568
1563
  }
1569
- function getNextPageParam(options, pages) {
1570
- return options.getNextPageParam(pages[pages.length - 1], pages);
1564
+ function getNextPageParam(options, {
1565
+ pages,
1566
+ pageParams
1567
+ }) {
1568
+ const lastIndex = pages.length - 1;
1569
+ return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
1571
1570
  }
1572
- function getPreviousPageParam(options, pages) {
1573
- return options.getPreviousPageParam?.(pages[0], pages);
1571
+ function getPreviousPageParam(options, {
1572
+ pages,
1573
+ pageParams
1574
+ }) {
1575
+ return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
1574
1576
  }
1575
1577
 
1576
1578
  /**
1577
1579
  * Checks if there is a next page.
1578
1580
  */
1579
- function hasNextPage(options, pages) {
1580
- if (!pages) return false;
1581
- return typeof getNextPageParam(options, pages) !== 'undefined';
1581
+ function hasNextPage(options, data) {
1582
+ if (!data) return false;
1583
+ return typeof getNextPageParam(options, data) !== 'undefined';
1582
1584
  }
1583
1585
 
1584
1586
  /**
1585
1587
  * Checks if there is a previous page.
1586
1588
  */
1587
- function hasPreviousPage(options, pages) {
1588
- if (!pages || !options.getPreviousPageParam) return false;
1589
- return typeof getPreviousPageParam(options, pages) !== 'undefined';
1589
+ function hasPreviousPage(options, data) {
1590
+ if (!data || !options.getPreviousPageParam) return false;
1591
+ return typeof getPreviousPageParam(options, data) !== 'undefined';
1590
1592
  }
1591
1593
 
1592
1594
  // CLASS
@@ -2444,7 +2446,7 @@
2444
2446
  options.behavior = infiniteQueryBehavior();
2445
2447
  return super.getOptimisticResult(options);
2446
2448
  }
2447
- fetchNextPage(options = {}) {
2449
+ fetchNextPage(options) {
2448
2450
  return this.fetch({
2449
2451
  ...options,
2450
2452
  meta: {
@@ -2454,9 +2456,7 @@
2454
2456
  }
2455
2457
  });
2456
2458
  }
2457
- fetchPreviousPage({
2458
- ...options
2459
- } = {}) {
2459
+ fetchPreviousPage(options) {
2460
2460
  return this.fetch({
2461
2461
  ...options,
2462
2462
  meta: {
@@ -2481,8 +2481,8 @@
2481
2481
  ...result,
2482
2482
  fetchNextPage: this.fetchNextPage,
2483
2483
  fetchPreviousPage: this.fetchPreviousPage,
2484
- hasNextPage: hasNextPage(options, state.data?.pages),
2485
- hasPreviousPage: hasPreviousPage(options, state.data?.pages),
2484
+ hasNextPage: hasNextPage(options, state.data),
2485
+ hasPreviousPage: hasPreviousPage(options, state.data),
2486
2486
  isFetchingNextPage,
2487
2487
  isFetchingPreviousPage,
2488
2488
  isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage