@tanstack/query-core 5.0.0-alpha.3 → 5.0.0-alpha.32

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 (200) hide show
  1. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
  2. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
  3. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
  4. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  5. package/build/lib/focusManager.d.ts +1 -0
  6. package/build/lib/focusManager.d.ts.map +1 -0
  7. package/build/lib/focusManager.esm.js +31 -15
  8. package/build/lib/focusManager.esm.js.map +1 -1
  9. package/build/lib/focusManager.js +31 -15
  10. package/build/lib/focusManager.js.map +1 -1
  11. package/build/lib/hydration.d.ts +3 -6
  12. package/build/lib/hydration.d.ts.map +1 -0
  13. package/build/lib/hydration.esm.js +9 -20
  14. package/build/lib/hydration.esm.js.map +1 -1
  15. package/build/lib/hydration.js +9 -20
  16. package/build/lib/hydration.js.map +1 -1
  17. package/build/lib/hydration.mjs +4 -18
  18. package/build/lib/hydration.mjs.map +1 -1
  19. package/build/lib/index.d.ts +4 -3
  20. package/build/lib/index.d.ts.map +1 -0
  21. package/build/lib/infiniteQueryBehavior.d.ts +3 -2
  22. package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
  23. package/build/lib/infiniteQueryBehavior.esm.js +52 -48
  24. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  25. package/build/lib/infiniteQueryBehavior.js +52 -48
  26. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  27. package/build/lib/infiniteQueryBehavior.mjs +48 -45
  28. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  29. package/build/lib/infiniteQueryObserver.d.ts +7 -6
  30. package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
  31. package/build/lib/infiniteQueryObserver.esm.js +7 -8
  32. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  33. package/build/lib/infiniteQueryObserver.js +7 -8
  34. package/build/lib/infiniteQueryObserver.js.map +1 -1
  35. package/build/lib/infiniteQueryObserver.mjs +4 -6
  36. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  37. package/build/lib/mutation.d.ts +1 -0
  38. package/build/lib/mutation.d.ts.map +1 -0
  39. package/build/lib/mutation.esm.js +127 -102
  40. package/build/lib/mutation.esm.js.map +1 -1
  41. package/build/lib/mutation.js +127 -102
  42. package/build/lib/mutation.js.map +1 -1
  43. package/build/lib/mutationCache.d.ts +1 -0
  44. package/build/lib/mutationCache.d.ts.map +1 -0
  45. package/build/lib/mutationCache.esm.js +34 -20
  46. package/build/lib/mutationCache.esm.js.map +1 -1
  47. package/build/lib/mutationCache.js +34 -20
  48. package/build/lib/mutationCache.js.map +1 -1
  49. package/build/lib/mutationCache.mjs +5 -4
  50. package/build/lib/mutationCache.mjs.map +1 -1
  51. package/build/lib/mutationObserver.d.ts +1 -0
  52. package/build/lib/mutationObserver.d.ts.map +1 -0
  53. package/build/lib/mutationObserver.esm.js +82 -52
  54. package/build/lib/mutationObserver.esm.js.map +1 -1
  55. package/build/lib/mutationObserver.js +82 -52
  56. package/build/lib/mutationObserver.js.map +1 -1
  57. package/build/lib/mutationObserver.mjs +1 -1
  58. package/build/lib/mutationObserver.mjs.map +1 -1
  59. package/build/lib/notifyManager.d.ts +1 -0
  60. package/build/lib/notifyManager.d.ts.map +1 -0
  61. package/build/lib/onlineManager.d.ts +1 -0
  62. package/build/lib/onlineManager.d.ts.map +1 -0
  63. package/build/lib/onlineManager.esm.js +29 -14
  64. package/build/lib/onlineManager.esm.js.map +1 -1
  65. package/build/lib/onlineManager.js +29 -14
  66. package/build/lib/onlineManager.js.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +1 -0
  68. package/build/lib/queriesObserver.d.ts.map +1 -0
  69. package/build/lib/queriesObserver.esm.js +98 -68
  70. package/build/lib/queriesObserver.esm.js.map +1 -1
  71. package/build/lib/queriesObserver.js +98 -68
  72. package/build/lib/queriesObserver.js.map +1 -1
  73. package/build/lib/queriesObserver.mjs +7 -6
  74. package/build/lib/queriesObserver.mjs.map +1 -1
  75. package/build/lib/query.d.ts +5 -3
  76. package/build/lib/query.d.ts.map +1 -0
  77. package/build/lib/query.esm.js +211 -162
  78. package/build/lib/query.esm.js.map +1 -1
  79. package/build/lib/query.js +211 -162
  80. package/build/lib/query.js.map +1 -1
  81. package/build/lib/query.mjs +2 -2
  82. package/build/lib/query.mjs.map +1 -1
  83. package/build/lib/queryCache.d.ts +1 -1
  84. package/build/lib/queryCache.d.ts.map +1 -0
  85. package/build/lib/queryCache.esm.js +20 -13
  86. package/build/lib/queryCache.esm.js.map +1 -1
  87. package/build/lib/queryCache.js +20 -13
  88. package/build/lib/queryCache.js.map +1 -1
  89. package/build/lib/queryCache.mjs +6 -5
  90. package/build/lib/queryCache.mjs.map +1 -1
  91. package/build/lib/queryClient.d.ts +1 -0
  92. package/build/lib/queryClient.d.ts.map +1 -0
  93. package/build/lib/queryClient.esm.js +113 -71
  94. package/build/lib/queryClient.esm.js.map +1 -1
  95. package/build/lib/queryClient.js +113 -71
  96. package/build/lib/queryClient.js.map +1 -1
  97. package/build/lib/queryClient.mjs +17 -13
  98. package/build/lib/queryClient.mjs.map +1 -1
  99. package/build/lib/queryObserver.d.ts +3 -4
  100. package/build/lib/queryObserver.d.ts.map +1 -0
  101. package/build/lib/queryObserver.esm.js +303 -214
  102. package/build/lib/queryObserver.esm.js.map +1 -1
  103. package/build/lib/queryObserver.js +301 -212
  104. package/build/lib/queryObserver.js.map +1 -1
  105. package/build/lib/queryObserver.mjs +16 -28
  106. package/build/lib/queryObserver.mjs.map +1 -1
  107. package/build/lib/removable.d.ts +1 -0
  108. package/build/lib/removable.d.ts.map +1 -0
  109. package/build/lib/removable.esm.js +13 -6
  110. package/build/lib/removable.esm.js.map +1 -1
  111. package/build/lib/removable.js +13 -6
  112. package/build/lib/removable.js.map +1 -1
  113. package/build/lib/retryer.d.ts +1 -0
  114. package/build/lib/retryer.d.ts.map +1 -0
  115. package/build/lib/retryer.esm.js +15 -14
  116. package/build/lib/retryer.esm.js.map +1 -1
  117. package/build/lib/retryer.js +15 -14
  118. package/build/lib/retryer.js.map +1 -1
  119. package/build/lib/subscribable.d.ts +2 -1
  120. package/build/lib/subscribable.d.ts.map +1 -0
  121. package/build/lib/subscribable.esm.js +4 -4
  122. package/build/lib/subscribable.esm.js.map +1 -1
  123. package/build/lib/subscribable.js +4 -4
  124. package/build/lib/subscribable.js.map +1 -1
  125. package/build/lib/subscribable.mjs +4 -4
  126. package/build/lib/subscribable.mjs.map +1 -1
  127. package/build/lib/tests/focusManager.test.d.ts +1 -0
  128. package/build/lib/tests/focusManager.test.d.ts.map +1 -0
  129. package/build/lib/tests/hydration.test.d.ts +1 -0
  130. package/build/lib/tests/hydration.test.d.ts.map +1 -0
  131. package/build/lib/tests/infiniteQueryBehavior.test.d.ts +1 -0
  132. package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
  133. package/build/lib/tests/infiniteQueryObserver.test.d.ts +1 -0
  134. package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
  135. package/build/lib/tests/mutationCache.test.d.ts +1 -0
  136. package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
  137. package/build/lib/tests/mutationObserver.test.d.ts +1 -0
  138. package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
  139. package/build/lib/tests/mutations.test.d.ts +1 -0
  140. package/build/lib/tests/mutations.test.d.ts.map +1 -0
  141. package/build/lib/tests/notifyManager.test.d.ts +1 -0
  142. package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
  143. package/build/lib/tests/onlineManager.test.d.ts +1 -0
  144. package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
  145. package/build/lib/tests/queriesObserver.test.d.ts +1 -0
  146. package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
  147. package/build/lib/tests/query.test.d.ts +1 -0
  148. package/build/lib/tests/query.test.d.ts.map +1 -0
  149. package/build/lib/tests/queryCache.test.d.ts +1 -0
  150. package/build/lib/tests/queryCache.test.d.ts.map +1 -0
  151. package/build/lib/tests/queryClient.test.d.ts +1 -0
  152. package/build/lib/tests/queryClient.test.d.ts.map +1 -0
  153. package/build/lib/tests/queryObserver.test.d.ts +1 -0
  154. package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
  155. package/build/lib/tests/utils.d.ts +5 -5
  156. package/build/lib/tests/utils.d.ts.map +1 -0
  157. package/build/lib/tests/utils.test.d.ts +1 -0
  158. package/build/lib/tests/utils.test.d.ts.map +1 -0
  159. package/build/lib/types.d.ts +12 -21
  160. package/build/lib/types.d.ts.map +1 -0
  161. package/build/lib/utils.d.ts +1 -0
  162. package/build/lib/utils.d.ts.map +1 -0
  163. package/build/lib/utils.esm.js +1 -1
  164. package/build/lib/utils.esm.js.map +1 -1
  165. package/build/lib/utils.js +1 -1
  166. package/build/lib/utils.js.map +1 -1
  167. package/build/umd/index.development.js +113 -131
  168. package/build/umd/index.development.js.map +1 -1
  169. package/build/umd/index.production.js +1 -1
  170. package/build/umd/index.production.js.map +1 -1
  171. package/package.json +2 -2
  172. package/src/hydration.ts +18 -37
  173. package/src/index.ts +2 -4
  174. package/src/infiniteQueryBehavior.ts +52 -60
  175. package/src/infiniteQueryObserver.ts +15 -10
  176. package/src/mutationCache.ts +4 -4
  177. package/src/mutationObserver.ts +1 -1
  178. package/src/queriesObserver.ts +11 -11
  179. package/src/query.ts +8 -6
  180. package/src/queryCache.ts +5 -8
  181. package/src/queryClient.ts +18 -19
  182. package/src/queryObserver.ts +32 -41
  183. package/src/subscribable.ts +5 -5
  184. package/src/tests/focusManager.test.tsx +12 -14
  185. package/src/tests/hydration.test.tsx +22 -17
  186. package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
  187. package/src/tests/infiniteQueryObserver.test.tsx +62 -1
  188. package/src/tests/mutationCache.test.tsx +11 -10
  189. package/src/tests/mutationObserver.test.tsx +3 -2
  190. package/src/tests/mutations.test.tsx +11 -10
  191. package/src/tests/notifyManager.test.tsx +7 -6
  192. package/src/tests/onlineManager.test.tsx +12 -17
  193. package/src/tests/queriesObserver.test.tsx +18 -17
  194. package/src/tests/query.test.tsx +19 -18
  195. package/src/tests/queryCache.test.tsx +12 -28
  196. package/src/tests/queryClient.test.tsx +82 -49
  197. package/src/tests/queryObserver.test.tsx +131 -9
  198. package/src/tests/utils.test.tsx +2 -1
  199. package/src/tests/utils.ts +5 -4
  200. package/src/types.ts +22 -20
@@ -6,19 +6,19 @@
6
6
 
7
7
  class Subscribable {
8
8
  constructor() {
9
- this.listeners = [];
9
+ this.listeners = new Set();
10
10
  this.subscribe = this.subscribe.bind(this);
11
11
  }
12
12
  subscribe(listener) {
13
- this.listeners.push(listener);
13
+ this.listeners.add(listener);
14
14
  this.onSubscribe();
15
15
  return () => {
16
- this.listeners = this.listeners.filter(x => x !== listener);
16
+ this.listeners.delete(listener);
17
17
  this.onUnsubscribe();
18
18
  };
19
19
  }
20
20
  hasListeners() {
21
- return this.listeners.length > 0;
21
+ return this.listeners.size > 0;
22
22
  }
23
23
  onSubscribe() {
24
24
  // Do nothing
@@ -907,7 +907,7 @@
907
907
  {
908
908
  console.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}`);
909
909
  }
910
- onError(new Error('undefined'));
910
+ onError(new Error(`${this.queryHash} data is undefined`));
911
911
  return;
912
912
  }
913
913
  this.setData(data);
@@ -1024,7 +1024,7 @@
1024
1024
  this.state = reducer(this.state);
1025
1025
  notifyManager.batch(() => {
1026
1026
  this.#observers.forEach(observer => {
1027
- observer.onQueryUpdate(action);
1027
+ observer.onQueryUpdate();
1028
1028
  });
1029
1029
  this.#cache.notify({
1030
1030
  query: this,
@@ -1061,7 +1061,7 @@
1061
1061
  constructor(config = {}) {
1062
1062
  super();
1063
1063
  this.config = config;
1064
- this.#queries = config.createStore?.() ?? new Map();
1064
+ this.#queries = new Map();
1065
1065
  }
1066
1066
  build(client, options, state) {
1067
1067
  const queryKey = options.queryKey;
@@ -1116,10 +1116,11 @@
1116
1116
  return [...this.#queries.values()];
1117
1117
  }
1118
1118
  find(filters) {
1119
- if (typeof filters.exact === 'undefined') {
1120
- filters.exact = true;
1121
- }
1122
- return this.getAll().find(query => matchQuery(filters, query));
1119
+ const defaultedFilters = {
1120
+ exact: true,
1121
+ ...filters
1122
+ };
1123
+ return this.getAll().find(query => matchQuery(defaultedFilters, query));
1123
1124
  }
1124
1125
  findAll(filters = {}) {
1125
1126
  const queries = this.getAll();
@@ -1425,10 +1426,11 @@
1425
1426
  return this.#mutations;
1426
1427
  }
1427
1428
  find(filters) {
1428
- if (typeof filters.exact === 'undefined') {
1429
- filters.exact = true;
1430
- }
1431
- return this.#mutations.find(mutation => matchMutation(filters, mutation));
1429
+ const defaultedFilters = {
1430
+ exact: true,
1431
+ ...filters
1432
+ };
1433
+ return this.#mutations.find(mutation => matchMutation(defaultedFilters, mutation));
1432
1434
  }
1433
1435
  findAll(filters = {}) {
1434
1436
  return this.#mutations.filter(mutation => matchMutation(filters, mutation));
@@ -1454,12 +1456,15 @@
1454
1456
  function infiniteQueryBehavior() {
1455
1457
  return {
1456
1458
  onFetch: context => {
1457
- context.fetchFn = () => {
1459
+ context.fetchFn = async () => {
1458
1460
  const options = context.options;
1459
1461
  const direction = context.fetchOptions?.meta?.fetchMore?.direction;
1460
1462
  const oldPages = context.state.data?.pages || [];
1461
1463
  const oldPageParams = context.state.data?.pageParams || [];
1462
- let newPageParams = oldPageParams;
1464
+ const empty = {
1465
+ pages: [],
1466
+ pageParams: []
1467
+ };
1463
1468
  let cancelled = false;
1464
1469
  const addSignalProperty = object => {
1465
1470
  Object.defineProperty(object, 'signal', {
@@ -1479,95 +1484,95 @@
1479
1484
 
1480
1485
  // Get query function
1481
1486
  const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
1482
- const buildNewPages = (pages, param, page, previous) => {
1483
- const {
1484
- maxPages
1485
- } = context.options;
1486
- if (previous) {
1487
- newPageParams = addToStart(newPageParams, param, maxPages);
1488
- return addToStart(pages, page, maxPages);
1489
- }
1490
- newPageParams = addToEnd(newPageParams, param, maxPages);
1491
- return addToEnd(pages, page, maxPages);
1492
- };
1493
1487
 
1494
1488
  // Create function to fetch a page
1495
- const fetchPage = (pages, param, previous) => {
1489
+ const fetchPage = async (data, param, previous) => {
1496
1490
  if (cancelled) {
1497
1491
  return Promise.reject();
1498
1492
  }
1499
- if (typeof param === 'undefined' && pages.length) {
1500
- return Promise.resolve(pages);
1493
+ if (typeof param === 'undefined' && data.pages.length) {
1494
+ return Promise.resolve(data);
1501
1495
  }
1502
1496
  const queryFnContext = {
1503
1497
  queryKey: context.queryKey,
1504
1498
  pageParam: param,
1499
+ direction: previous ? 'backward' : 'forward',
1505
1500
  meta: context.options.meta
1506
1501
  };
1507
1502
  addSignalProperty(queryFnContext);
1508
- const queryFnResult = queryFn(queryFnContext);
1509
- const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
1510
- return promise;
1503
+ const page = await queryFn(queryFnContext);
1504
+ const {
1505
+ maxPages
1506
+ } = context.options;
1507
+ const addTo = previous ? addToStart : addToEnd;
1508
+ return {
1509
+ pages: addTo(data.pages, page, maxPages),
1510
+ pageParams: addTo(data.pageParams, param, maxPages)
1511
+ };
1511
1512
  };
1512
- let promise;
1513
+ let result;
1513
1514
 
1514
1515
  // Fetch first page?
1515
1516
  if (!oldPages.length) {
1516
- promise = fetchPage([], options.defaultPageParam);
1517
+ result = await fetchPage(empty, options.defaultPageParam);
1517
1518
  }
1518
1519
 
1519
1520
  // fetch next / previous page?
1520
1521
  else if (direction) {
1521
1522
  const previous = direction === 'backward';
1522
- const param = previous ? getPreviousPageParam(options, oldPages) : getNextPageParam(options, oldPages);
1523
- promise = fetchPage(oldPages, param, previous);
1523
+ const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
1524
+ const oldData = {
1525
+ pages: oldPages,
1526
+ pageParams: oldPageParams
1527
+ };
1528
+ const param = pageParamFn(options, oldData);
1529
+ result = await fetchPage(oldData, param, previous);
1524
1530
  }
1525
1531
 
1526
1532
  // Refetch pages
1527
1533
  else {
1528
- newPageParams = [];
1529
-
1530
1534
  // Fetch first page
1531
- promise = fetchPage([], oldPageParams[0]);
1535
+ result = await fetchPage(empty, oldPageParams[0]);
1532
1536
 
1533
1537
  // Fetch remaining pages
1534
1538
  for (let i = 1; i < oldPages.length; i++) {
1535
- promise = promise.then(pages => {
1536
- const param = getNextPageParam(options, pages);
1537
- return fetchPage(pages, param);
1538
- });
1539
+ const param = getNextPageParam(options, result);
1540
+ result = await fetchPage(result, param);
1539
1541
  }
1540
1542
  }
1541
- const finalPromise = promise.then(pages => ({
1542
- pages,
1543
- pageParams: newPageParams
1544
- }));
1545
- return finalPromise;
1543
+ return result;
1546
1544
  };
1547
1545
  }
1548
1546
  };
1549
1547
  }
1550
- function getNextPageParam(options, pages) {
1551
- return options.getNextPageParam(pages[pages.length - 1], pages);
1548
+ function getNextPageParam(options, {
1549
+ pages,
1550
+ pageParams
1551
+ }) {
1552
+ const lastIndex = pages.length - 1;
1553
+ return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
1552
1554
  }
1553
- function getPreviousPageParam(options, pages) {
1554
- return options.getPreviousPageParam?.(pages[0], pages);
1555
+ function getPreviousPageParam(options, {
1556
+ pages,
1557
+ pageParams
1558
+ }) {
1559
+ return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
1555
1560
  }
1556
1561
 
1557
1562
  /**
1558
1563
  * Checks if there is a next page.
1559
1564
  */
1560
- function hasNextPage(options, pages) {
1561
- if (!pages) return false;
1562
- return typeof getNextPageParam(options, pages) !== 'undefined';
1565
+ function hasNextPage(options, data) {
1566
+ if (!data) return false;
1567
+ return typeof getNextPageParam(options, data) !== 'undefined';
1563
1568
  }
1564
1569
 
1565
1570
  /**
1566
1571
  * Checks if there is a previous page.
1567
1572
  */
1568
- function hasPreviousPage(options, pages) {
1569
- if (!pages || !options.getPreviousPageParam) return false;
1570
- return typeof getPreviousPageParam(options, pages) !== 'undefined';
1573
+ function hasPreviousPage(options, data) {
1574
+ if (!data || !options.getPreviousPageParam) return false;
1575
+ return typeof getPreviousPageParam(options, data) !== 'undefined';
1571
1576
  }
1572
1577
 
1573
1578
  // CLASS
@@ -1692,10 +1697,11 @@
1692
1697
  });
1693
1698
  }
1694
1699
  cancelQueries(filters = {}, cancelOptions = {}) {
1695
- if (typeof cancelOptions.revert === 'undefined') {
1696
- cancelOptions.revert = true;
1697
- }
1698
- const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map(query => query.cancel(cancelOptions)));
1700
+ const defaultedCancelOptions = {
1701
+ revert: true,
1702
+ ...cancelOptions
1703
+ };
1704
+ const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map(query => query.cancel(defaultedCancelOptions)));
1699
1705
  return Promise.all(promises).then(noop).catch(noop);
1700
1706
  }
1701
1707
  invalidateQueries(filters = {}, options = {}) {
@@ -1714,15 +1720,18 @@
1714
1720
  });
1715
1721
  }
1716
1722
  refetchQueries(filters = {}, options) {
1717
- const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => query.fetch(undefined, {
1723
+ const fetchOptions = {
1718
1724
  ...options,
1719
1725
  cancelRefetch: options?.cancelRefetch ?? true
1720
- })));
1721
- let promise = Promise.all(promises).then(noop);
1722
- if (!options?.throwOnError) {
1723
- promise = promise.catch(noop);
1724
- }
1725
- return promise;
1726
+ };
1727
+ const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
1728
+ let promise = query.fetch(undefined, fetchOptions);
1729
+ if (!fetchOptions.throwOnError) {
1730
+ promise = promise.catch(noop);
1731
+ }
1732
+ return query.state.fetchStatus === 'paused' ? Promise.resolve() : promise;
1733
+ }));
1734
+ return Promise.all(promises).then(noop);
1726
1735
  }
1727
1736
  fetchQuery(options) {
1728
1737
  const defaultedOptions = this.defaultQueryOptions(options);
@@ -1815,8 +1824,8 @@
1815
1824
  if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
1816
1825
  defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
1817
1826
  }
1818
- if (typeof defaultedOptions.throwErrors === 'undefined') {
1819
- defaultedOptions.throwErrors = !!defaultedOptions.suspense;
1827
+ if (typeof defaultedOptions.throwOnError === 'undefined') {
1828
+ defaultedOptions.throwOnError = !!defaultedOptions.suspense;
1820
1829
  }
1821
1830
  return defaultedOptions;
1822
1831
  }
@@ -1844,10 +1853,12 @@
1844
1853
  #currentResult = undefined;
1845
1854
  #currentResultState;
1846
1855
  #currentResultOptions;
1847
- #previousQueryResult;
1848
1856
  #selectError;
1849
1857
  #selectFn;
1850
1858
  #selectResult;
1859
+ // This property keeps track of the last query with defined data.
1860
+ // It will be used to pass the previous data and query to the placeholder function between renders.
1861
+ #lastQueryWithDefinedData;
1851
1862
  #staleTimeoutId;
1852
1863
  #refetchIntervalId;
1853
1864
  #currentRefetchInterval;
@@ -1864,7 +1875,7 @@
1864
1875
  this.refetch = this.refetch.bind(this);
1865
1876
  }
1866
1877
  onSubscribe() {
1867
- if (this.listeners.length === 1) {
1878
+ if (this.listeners.size === 1) {
1868
1879
  this.#currentQuery.addObserver(this);
1869
1880
  if (shouldFetchOnMount(this.#currentQuery, this.options)) {
1870
1881
  this.#executeFetch();
@@ -1873,7 +1884,7 @@
1873
1884
  }
1874
1885
  }
1875
1886
  onUnsubscribe() {
1876
- if (!this.listeners.length) {
1887
+ if (!this.hasListeners()) {
1877
1888
  this.destroy();
1878
1889
  }
1879
1890
  }
@@ -1884,7 +1895,7 @@
1884
1895
  return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
1885
1896
  }
1886
1897
  destroy() {
1887
- this.listeners = [];
1898
+ this.listeners = new Set();
1888
1899
  this.#clearStaleTimeout();
1889
1900
  this.#clearRefetchInterval();
1890
1901
  this.#currentQuery.removeObserver(this);
@@ -2004,7 +2015,7 @@
2004
2015
  }, timeout);
2005
2016
  }
2006
2017
  #computeRefetchInterval() {
2007
- return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval ?? false;
2018
+ return (typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval) ?? false;
2008
2019
  }
2009
2020
  #updateRefetchInterval(nextInterval) {
2010
2021
  this.#clearRefetchInterval();
@@ -2042,7 +2053,6 @@
2042
2053
  const prevResultOptions = this.#currentResultOptions;
2043
2054
  const queryChange = query !== prevQuery;
2044
2055
  const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
2045
- const prevQueryResult = queryChange ? this.#currentResult : this.#previousQueryResult;
2046
2056
  const {
2047
2057
  state
2048
2058
  } = query;
@@ -2101,7 +2111,7 @@
2101
2111
  if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
2102
2112
  placeholderData = prevResult.data;
2103
2113
  } else {
2104
- placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(prevQueryResult?.data) : options.placeholderData;
2114
+ placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
2105
2115
  if (options.select && typeof placeholderData !== 'undefined') {
2106
2116
  try {
2107
2117
  placeholderData = options.select(placeholderData);
@@ -2165,6 +2175,9 @@
2165
2175
  if (shallowEqualObjects(nextResult, prevResult)) {
2166
2176
  return;
2167
2177
  }
2178
+ if (this.#currentResultState.data !== undefined) {
2179
+ this.#lastQueryWithDefinedData = this.#currentQuery;
2180
+ }
2168
2181
  this.#currentResult = nextResult;
2169
2182
 
2170
2183
  // Determine which callbacks to trigger
@@ -2180,7 +2193,7 @@
2180
2193
  return true;
2181
2194
  }
2182
2195
  const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
2183
- if (this.options.throwErrors) {
2196
+ if (this.options.throwOnError) {
2184
2197
  includedProps.add('error');
2185
2198
  }
2186
2199
  return Object.keys(this.#currentResult).some(key => {
@@ -2205,36 +2218,20 @@
2205
2218
  const prevQuery = this.#currentQuery;
2206
2219
  this.#currentQuery = query;
2207
2220
  this.#currentQueryInitialState = query.state;
2208
- this.#previousQueryResult = this.#currentResult;
2209
2221
  if (this.hasListeners()) {
2210
2222
  prevQuery?.removeObserver(this);
2211
2223
  query.addObserver(this);
2212
2224
  }
2213
2225
  }
2214
- onQueryUpdate(action) {
2215
- const notifyOptions = {};
2216
- if (action.type === 'success') {
2217
- notifyOptions.onSuccess = !action.manual;
2218
- } else if (action.type === 'error' && !isCancelledError(action.error)) {
2219
- notifyOptions.onError = true;
2220
- }
2221
- this.#updateResult(notifyOptions);
2226
+ onQueryUpdate() {
2227
+ this.#updateResult();
2222
2228
  if (this.hasListeners()) {
2223
2229
  this.#updateTimers();
2224
2230
  }
2225
2231
  }
2226
2232
  #notify(notifyOptions) {
2227
2233
  notifyManager.batch(() => {
2228
- // First trigger the configuration callbacks
2229
- if (notifyOptions.onSuccess) {
2230
- this.options.onSuccess?.(this.#currentResult.data);
2231
- this.options.onSettled?.(this.#currentResult.data, null);
2232
- } else if (notifyOptions.onError) {
2233
- this.options.onError?.(this.#currentResult.error);
2234
- this.options.onSettled?.(undefined, this.#currentResult.error);
2235
- }
2236
-
2237
- // Then trigger the listeners
2234
+ // First, trigger the listeners
2238
2235
  if (notifyOptions.listeners) {
2239
2236
  this.listeners.forEach(listener => {
2240
2237
  listener(this.#currentResult);
@@ -2293,7 +2290,7 @@
2293
2290
  }
2294
2291
  }
2295
2292
  onSubscribe() {
2296
- if (this.listeners.length === 1) {
2293
+ if (this.listeners.size === 1) {
2297
2294
  this.#observers.forEach(observer => {
2298
2295
  observer.subscribe(result => {
2299
2296
  this.#onUpdate(observer, result);
@@ -2302,12 +2299,12 @@
2302
2299
  }
2303
2300
  }
2304
2301
  onUnsubscribe() {
2305
- if (!this.listeners.length) {
2302
+ if (!this.listeners.size) {
2306
2303
  this.destroy();
2307
2304
  }
2308
2305
  }
2309
2306
  destroy() {
2310
- this.listeners = [];
2307
+ this.listeners = new Set();
2311
2308
  this.#observers.forEach(observer => {
2312
2309
  observer.destroy();
2313
2310
  });
@@ -2356,9 +2353,10 @@
2356
2353
  }
2357
2354
  #findMatchingObservers(queries) {
2358
2355
  const prevObservers = this.#observers;
2356
+ const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
2359
2357
  const defaultedQueryOptions = queries.map(options => this.#client.defaultQueryOptions(options));
2360
2358
  const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
2361
- const match = prevObservers.find(observer => observer.options.queryHash === defaultedOptions.queryHash);
2359
+ const match = prevObserversMap.get(defaultedOptions.queryHash);
2362
2360
  if (match != null) {
2363
2361
  return [{
2364
2362
  defaultedQueryOptions: defaultedOptions,
@@ -2367,8 +2365,8 @@
2367
2365
  }
2368
2366
  return [];
2369
2367
  });
2370
- const matchedQueryHashes = matchingObservers.map(match => match.defaultedQueryOptions.queryHash);
2371
- const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
2368
+ const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
2369
+ const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
2372
2370
  const getObserver = options => {
2373
2371
  const defaultedOptions = this.#client.defaultQueryOptions(options);
2374
2372
  const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
@@ -2425,7 +2423,7 @@
2425
2423
  options.behavior = infiniteQueryBehavior();
2426
2424
  return super.getOptimisticResult(options);
2427
2425
  }
2428
- fetchNextPage(options = {}) {
2426
+ fetchNextPage(options) {
2429
2427
  return this.fetch({
2430
2428
  ...options,
2431
2429
  meta: {
@@ -2435,9 +2433,7 @@
2435
2433
  }
2436
2434
  });
2437
2435
  }
2438
- fetchPreviousPage({
2439
- ...options
2440
- } = {}) {
2436
+ fetchPreviousPage(options) {
2441
2437
  return this.fetch({
2442
2438
  ...options,
2443
2439
  meta: {
@@ -2462,8 +2458,8 @@
2462
2458
  ...result,
2463
2459
  fetchNextPage: this.fetchNextPage,
2464
2460
  fetchPreviousPage: this.fetchPreviousPage,
2465
- hasNextPage: hasNextPage(options, state.data?.pages),
2466
- hasPreviousPage: hasPreviousPage(options, state.data?.pages),
2461
+ hasNextPage: hasNextPage(options, state.data),
2462
+ hasPreviousPage: hasPreviousPage(options, state.data),
2467
2463
  isFetchingNextPage,
2468
2464
  isFetchingPreviousPage,
2469
2465
  isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
@@ -2504,7 +2500,7 @@
2504
2500
  this.#currentMutation?.setOptions(this.options);
2505
2501
  }
2506
2502
  onUnsubscribe() {
2507
- if (!this.listeners.length) {
2503
+ if (!this.hasListeners()) {
2508
2504
  this.#currentMutation?.removeObserver(this);
2509
2505
  }
2510
2506
  }
@@ -2589,24 +2585,10 @@
2589
2585
  return query.state.status === 'success';
2590
2586
  }
2591
2587
  function dehydrate(client, options = {}) {
2592
- const mutations = [];
2593
- const queries = [];
2594
- if (options.dehydrateMutations !== false) {
2595
- const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
2596
- client.getMutationCache().getAll().forEach(mutation => {
2597
- if (shouldDehydrateMutation(mutation)) {
2598
- mutations.push(dehydrateMutation(mutation));
2599
- }
2600
- });
2601
- }
2602
- if (options.dehydrateQueries !== false) {
2603
- const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
2604
- client.getQueryCache().getAll().forEach(query => {
2605
- if (shouldDehydrateQuery(query)) {
2606
- queries.push(dehydrateQuery(query));
2607
- }
2608
- });
2609
- }
2588
+ const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
2589
+ const mutations = client.getMutationCache().getAll().flatMap(mutation => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []);
2590
+ const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
2591
+ const queries = client.getQueryCache().getAll().flatMap(query => filterQuery(query) ? [dehydrateQuery(query)] : []);
2610
2592
  return {
2611
2593
  mutations,
2612
2594
  queries