@tanstack/query-core 5.0.0-alpha.1 → 5.0.0-alpha.12

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 (140) 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.esm.js +31 -15
  6. package/build/lib/focusManager.esm.js.map +1 -1
  7. package/build/lib/focusManager.js +31 -15
  8. package/build/lib/focusManager.js.map +1 -1
  9. package/build/lib/hydration.d.ts +2 -6
  10. package/build/lib/hydration.esm.js +9 -20
  11. package/build/lib/hydration.esm.js.map +1 -1
  12. package/build/lib/hydration.js +9 -20
  13. package/build/lib/hydration.js.map +1 -1
  14. package/build/lib/hydration.mjs +4 -18
  15. package/build/lib/hydration.mjs.map +1 -1
  16. package/build/lib/index.d.ts +4 -4
  17. package/build/lib/index.esm.js +1 -1
  18. package/build/lib/index.js +1 -0
  19. package/build/lib/index.js.map +1 -1
  20. package/build/lib/index.mjs +1 -1
  21. package/build/lib/infiniteQueryBehavior.d.ts +2 -2
  22. package/build/lib/infiniteQueryBehavior.esm.js +52 -48
  23. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.js +52 -48
  25. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  26. package/build/lib/infiniteQueryBehavior.mjs +48 -45
  27. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  28. package/build/lib/infiniteQueryObserver.d.ts +7 -7
  29. package/build/lib/infiniteQueryObserver.esm.js +7 -8
  30. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.js +7 -8
  32. package/build/lib/infiniteQueryObserver.js.map +1 -1
  33. package/build/lib/infiniteQueryObserver.mjs +4 -6
  34. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  35. package/build/lib/mutation.d.ts +5 -4
  36. package/build/lib/mutation.esm.js +138 -99
  37. package/build/lib/mutation.esm.js.map +1 -1
  38. package/build/lib/mutation.js +138 -99
  39. package/build/lib/mutation.js.map +1 -1
  40. package/build/lib/mutation.mjs +16 -2
  41. package/build/lib/mutation.mjs.map +1 -1
  42. package/build/lib/mutationCache.d.ts +5 -4
  43. package/build/lib/mutationCache.esm.js +30 -17
  44. package/build/lib/mutationCache.esm.js.map +1 -1
  45. package/build/lib/mutationCache.js +30 -17
  46. package/build/lib/mutationCache.js.map +1 -1
  47. package/build/lib/mutationCache.mjs.map +1 -1
  48. package/build/lib/mutationObserver.d.ts +2 -2
  49. package/build/lib/mutationObserver.esm.js +81 -50
  50. package/build/lib/mutationObserver.esm.js.map +1 -1
  51. package/build/lib/mutationObserver.js +81 -50
  52. package/build/lib/mutationObserver.js.map +1 -1
  53. package/build/lib/mutationObserver.mjs +1 -0
  54. package/build/lib/mutationObserver.mjs.map +1 -1
  55. package/build/lib/onlineManager.esm.js +29 -14
  56. package/build/lib/onlineManager.esm.js.map +1 -1
  57. package/build/lib/onlineManager.js +29 -14
  58. package/build/lib/onlineManager.js.map +1 -1
  59. package/build/lib/queriesObserver.esm.js +94 -69
  60. package/build/lib/queriesObserver.esm.js.map +1 -1
  61. package/build/lib/queriesObserver.js +94 -69
  62. package/build/lib/queriesObserver.js.map +1 -1
  63. package/build/lib/queriesObserver.mjs +1 -5
  64. package/build/lib/queriesObserver.mjs.map +1 -1
  65. package/build/lib/query.d.ts +8 -7
  66. package/build/lib/query.esm.js +210 -159
  67. package/build/lib/query.esm.js.map +1 -1
  68. package/build/lib/query.js +210 -159
  69. package/build/lib/query.js.map +1 -1
  70. package/build/lib/query.mjs +2 -0
  71. package/build/lib/query.mjs.map +1 -1
  72. package/build/lib/queryCache.d.ts +5 -4
  73. package/build/lib/queryCache.esm.js +16 -9
  74. package/build/lib/queryCache.esm.js.map +1 -1
  75. package/build/lib/queryCache.js +16 -9
  76. package/build/lib/queryCache.js.map +1 -1
  77. package/build/lib/queryCache.mjs.map +1 -1
  78. package/build/lib/queryClient.d.ts +7 -7
  79. package/build/lib/queryClient.esm.js +103 -63
  80. package/build/lib/queryClient.esm.js.map +1 -1
  81. package/build/lib/queryClient.js +103 -63
  82. package/build/lib/queryClient.js.map +1 -1
  83. package/build/lib/queryClient.mjs.map +1 -1
  84. package/build/lib/queryObserver.d.ts +2 -2
  85. package/build/lib/queryObserver.esm.js +308 -207
  86. package/build/lib/queryObserver.esm.js.map +1 -1
  87. package/build/lib/queryObserver.js +307 -206
  88. package/build/lib/queryObserver.js.map +1 -1
  89. package/build/lib/queryObserver.mjs.map +1 -1
  90. package/build/lib/removable.esm.js +13 -6
  91. package/build/lib/removable.esm.js.map +1 -1
  92. package/build/lib/removable.js +13 -6
  93. package/build/lib/removable.js.map +1 -1
  94. package/build/lib/retryer.d.ts +5 -5
  95. package/build/lib/retryer.esm.js +15 -14
  96. package/build/lib/retryer.esm.js.map +1 -1
  97. package/build/lib/retryer.js +15 -14
  98. package/build/lib/retryer.js.map +1 -1
  99. package/build/lib/retryer.mjs.map +1 -1
  100. package/build/lib/tests/utils.d.ts +4 -5
  101. package/build/lib/types.d.ts +42 -40
  102. package/build/lib/utils.esm.js +1 -1
  103. package/build/lib/utils.esm.js.map +1 -1
  104. package/build/lib/utils.js +1 -1
  105. package/build/lib/utils.js.map +1 -1
  106. package/build/umd/index.development.js +77 -76
  107. package/build/umd/index.development.js.map +1 -1
  108. package/build/umd/index.production.js +1 -1
  109. package/build/umd/index.production.js.map +1 -1
  110. package/package.json +2 -2
  111. package/src/hydration.ts +18 -37
  112. package/src/index.ts +9 -5
  113. package/src/infiniteQueryBehavior.ts +52 -60
  114. package/src/infiniteQueryObserver.ts +17 -12
  115. package/src/mutation.ts +34 -7
  116. package/src/mutationCache.ts +11 -4
  117. package/src/mutationObserver.ts +3 -2
  118. package/src/queriesObserver.ts +3 -7
  119. package/src/query.ts +24 -9
  120. package/src/queryCache.ts +12 -4
  121. package/src/queryClient.ts +7 -7
  122. package/src/queryObserver.ts +2 -2
  123. package/src/retryer.ts +5 -5
  124. package/src/tests/focusManager.test.tsx +12 -14
  125. package/src/tests/hydration.test.tsx +22 -17
  126. package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
  127. package/src/tests/infiniteQueryObserver.test.tsx +62 -1
  128. package/src/tests/mutationCache.test.tsx +61 -16
  129. package/src/tests/mutationObserver.test.tsx +3 -2
  130. package/src/tests/mutations.test.tsx +41 -9
  131. package/src/tests/notifyManager.test.tsx +7 -6
  132. package/src/tests/onlineManager.test.tsx +12 -17
  133. package/src/tests/queriesObserver.test.tsx +18 -17
  134. package/src/tests/query.test.tsx +18 -17
  135. package/src/tests/queryCache.test.tsx +28 -15
  136. package/src/tests/queryClient.test.tsx +49 -48
  137. package/src/tests/queryObserver.test.tsx +10 -9
  138. package/src/tests/utils.test.tsx +2 -1
  139. package/src/tests/utils.ts +5 -4
  140. package/src/types.ts +53 -37
@@ -889,6 +889,7 @@
889
889
  if (!isCancelledError(error)) {
890
890
  // Notify cache callback
891
891
  this.#cache.config.onError?.(error, this);
892
+ this.#cache.config.onSettled?.(this.state.data, error, this);
892
893
  }
893
894
  if (!this.isFetchingOptimistic) {
894
895
  // Schedule query gc after fetching
@@ -913,6 +914,7 @@
913
914
 
914
915
  // Notify cache callback
915
916
  this.#cache.config.onSuccess?.(data, this);
917
+ this.#cache.config.onSettled?.(data, this.state.error, this);
916
918
  if (!this.isFetchingOptimistic) {
917
919
  // Schedule query gc after fetching
918
920
  this.scheduleGc();
@@ -1152,18 +1154,26 @@
1152
1154
 
1153
1155
  class Mutation extends Removable {
1154
1156
  #observers;
1157
+ #defaultOptions;
1155
1158
  #mutationCache;
1156
1159
  #retryer;
1157
1160
  constructor(config) {
1158
1161
  super();
1159
- this.options = config.options;
1160
1162
  this.mutationId = config.mutationId;
1163
+ this.#defaultOptions = config.defaultOptions;
1161
1164
  this.#mutationCache = config.mutationCache;
1162
1165
  this.#observers = [];
1163
1166
  this.state = config.state || getDefaultState();
1164
- this.updateGcTime(this.options.gcTime);
1167
+ this.setOptions(config.options);
1165
1168
  this.scheduleGc();
1166
1169
  }
1170
+ setOptions(options) {
1171
+ this.options = {
1172
+ ...this.#defaultOptions,
1173
+ ...options
1174
+ };
1175
+ this.updateGcTime(this.options.gcTime);
1176
+ }
1167
1177
  get meta() {
1168
1178
  return this.options.meta;
1169
1179
  }
@@ -1258,6 +1268,9 @@
1258
1268
  // Notify cache callback
1259
1269
  await this.#mutationCache.config.onSuccess?.(data, variables, this.state.context, this);
1260
1270
  await this.options.onSuccess?.(data, variables, this.state.context);
1271
+
1272
+ // Notify cache callback
1273
+ await this.#mutationCache.config.onSettled?.(data, null, this.state.variables, this.state.context, this);
1261
1274
  await this.options.onSettled?.(data, null, variables, this.state.context);
1262
1275
  this.#dispatch({
1263
1276
  type: 'success',
@@ -1269,6 +1282,9 @@
1269
1282
  // Notify cache callback
1270
1283
  await this.#mutationCache.config.onError?.(error, variables, this.state.context, this);
1271
1284
  await this.options.onError?.(error, variables, this.state.context);
1285
+
1286
+ // Notify cache callback
1287
+ await this.#mutationCache.config.onSettled?.(undefined, error, this.state.variables, this.state.context, this);
1272
1288
  await this.options.onSettled?.(undefined, error, variables, this.state.context);
1273
1289
  throw error;
1274
1290
  } finally {
@@ -1438,12 +1454,15 @@
1438
1454
  function infiniteQueryBehavior() {
1439
1455
  return {
1440
1456
  onFetch: context => {
1441
- context.fetchFn = () => {
1457
+ context.fetchFn = async () => {
1442
1458
  const options = context.options;
1443
1459
  const direction = context.fetchOptions?.meta?.fetchMore?.direction;
1444
1460
  const oldPages = context.state.data?.pages || [];
1445
1461
  const oldPageParams = context.state.data?.pageParams || [];
1446
- let newPageParams = oldPageParams;
1462
+ const empty = {
1463
+ pages: [],
1464
+ pageParams: []
1465
+ };
1447
1466
  let cancelled = false;
1448
1467
  const addSignalProperty = object => {
1449
1468
  Object.defineProperty(object, 'signal', {
@@ -1463,95 +1482,95 @@
1463
1482
 
1464
1483
  // Get query function
1465
1484
  const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
1466
- const buildNewPages = (pages, param, page, previous) => {
1467
- const {
1468
- maxPages
1469
- } = context.options;
1470
- if (previous) {
1471
- newPageParams = addToStart(newPageParams, param, maxPages);
1472
- return addToStart(pages, page, maxPages);
1473
- }
1474
- newPageParams = addToEnd(newPageParams, param, maxPages);
1475
- return addToEnd(pages, page, maxPages);
1476
- };
1477
1485
 
1478
1486
  // Create function to fetch a page
1479
- const fetchPage = (pages, param, previous) => {
1487
+ const fetchPage = async (data, param, previous) => {
1480
1488
  if (cancelled) {
1481
1489
  return Promise.reject();
1482
1490
  }
1483
- if (typeof param === 'undefined' && pages.length) {
1484
- return Promise.resolve(pages);
1491
+ if (typeof param === 'undefined' && data.pages.length) {
1492
+ return Promise.resolve(data);
1485
1493
  }
1486
1494
  const queryFnContext = {
1487
1495
  queryKey: context.queryKey,
1488
1496
  pageParam: param,
1497
+ direction: previous ? 'backward' : 'forward',
1489
1498
  meta: context.options.meta
1490
1499
  };
1491
1500
  addSignalProperty(queryFnContext);
1492
- const queryFnResult = queryFn(queryFnContext);
1493
- const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
1494
- return promise;
1501
+ const page = await queryFn(queryFnContext);
1502
+ const {
1503
+ maxPages
1504
+ } = context.options;
1505
+ const addTo = previous ? addToStart : addToEnd;
1506
+ return {
1507
+ pages: addTo(data.pages, page, maxPages),
1508
+ pageParams: addTo(data.pageParams, param, maxPages)
1509
+ };
1495
1510
  };
1496
- let promise;
1511
+ let result;
1497
1512
 
1498
1513
  // Fetch first page?
1499
1514
  if (!oldPages.length) {
1500
- promise = fetchPage([], options.defaultPageParam);
1515
+ result = await fetchPage(empty, options.defaultPageParam);
1501
1516
  }
1502
1517
 
1503
1518
  // fetch next / previous page?
1504
1519
  else if (direction) {
1505
1520
  const previous = direction === 'backward';
1506
- const param = previous ? getPreviousPageParam(options, oldPages) : getNextPageParam(options, oldPages);
1507
- promise = fetchPage(oldPages, param, previous);
1521
+ const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
1522
+ const oldData = {
1523
+ pages: oldPages,
1524
+ pageParams: oldPageParams
1525
+ };
1526
+ const param = pageParamFn(options, oldData);
1527
+ result = await fetchPage(oldData, param, previous);
1508
1528
  }
1509
1529
 
1510
1530
  // Refetch pages
1511
1531
  else {
1512
- newPageParams = [];
1513
-
1514
1532
  // Fetch first page
1515
- promise = fetchPage([], oldPageParams[0]);
1533
+ result = await fetchPage(empty, oldPageParams[0]);
1516
1534
 
1517
1535
  // Fetch remaining pages
1518
1536
  for (let i = 1; i < oldPages.length; i++) {
1519
- promise = promise.then(pages => {
1520
- const param = getNextPageParam(options, pages);
1521
- return fetchPage(pages, param);
1522
- });
1537
+ const param = getNextPageParam(options, result);
1538
+ result = await fetchPage(result, param);
1523
1539
  }
1524
1540
  }
1525
- const finalPromise = promise.then(pages => ({
1526
- pages,
1527
- pageParams: newPageParams
1528
- }));
1529
- return finalPromise;
1541
+ return result;
1530
1542
  };
1531
1543
  }
1532
1544
  };
1533
1545
  }
1534
- function getNextPageParam(options, pages) {
1535
- return options.getNextPageParam(pages[pages.length - 1], pages);
1546
+ function getNextPageParam(options, {
1547
+ pages,
1548
+ pageParams
1549
+ }) {
1550
+ const lastIndex = pages.length - 1;
1551
+ return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
1536
1552
  }
1537
- function getPreviousPageParam(options, pages) {
1538
- return options.getPreviousPageParam?.(pages[0], pages);
1553
+ function getPreviousPageParam(options, {
1554
+ pages,
1555
+ pageParams
1556
+ }) {
1557
+ return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
1539
1558
  }
1540
1559
 
1541
1560
  /**
1542
1561
  * Checks if there is a next page.
1543
1562
  */
1544
- function hasNextPage(options, pages) {
1545
- if (!pages) return false;
1546
- return typeof getNextPageParam(options, pages) !== 'undefined';
1563
+ function hasNextPage(options, data) {
1564
+ if (!data) return false;
1565
+ return typeof getNextPageParam(options, data) !== 'undefined';
1547
1566
  }
1548
1567
 
1549
1568
  /**
1550
1569
  * Checks if there is a previous page.
1551
1570
  */
1552
- function hasPreviousPage(options, pages) {
1553
- if (!pages || !options.getPreviousPageParam) return false;
1554
- return typeof getPreviousPageParam(options, pages) !== 'undefined';
1571
+ function hasPreviousPage(options, data) {
1572
+ if (!data || !options.getPreviousPageParam) return false;
1573
+ return typeof getPreviousPageParam(options, data) !== 'undefined';
1555
1574
  }
1556
1575
 
1557
1576
  // CLASS
@@ -2266,14 +2285,12 @@
2266
2285
  #result;
2267
2286
  #queries;
2268
2287
  #observers;
2269
- #observersMap;
2270
2288
  constructor(client, queries) {
2271
2289
  super();
2272
2290
  this.#client = client;
2273
2291
  this.#queries = [];
2274
2292
  this.#result = [];
2275
2293
  this.#observers = [];
2276
- this.#observersMap = {};
2277
2294
  if (queries) {
2278
2295
  this.setQueries(queries);
2279
2296
  }
@@ -2307,14 +2324,12 @@
2307
2324
  // set options for the new observers to notify of changes
2308
2325
  newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
2309
2326
  const newObservers = newObserverMatches.map(match => match.observer);
2310
- const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
2311
2327
  const newResult = newObservers.map(observer => observer.getCurrentResult());
2312
2328
  const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
2313
2329
  if (prevObservers.length === newObservers.length && !hasIndexChange) {
2314
2330
  return;
2315
2331
  }
2316
2332
  this.#observers = newObservers;
2317
- this.#observersMap = newObserversMap;
2318
2333
  this.#result = newResult;
2319
2334
  if (!this.hasListeners()) {
2320
2335
  return;
@@ -2359,7 +2374,7 @@
2359
2374
  const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
2360
2375
  const getObserver = options => {
2361
2376
  const defaultedOptions = this.#client.defaultQueryOptions(options);
2362
- const currentObserver = this.#observersMap[defaultedOptions.queryHash];
2377
+ const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
2363
2378
  return currentObserver ?? new QueryObserver(this.#client, defaultedOptions);
2364
2379
  };
2365
2380
  const newOrReusedObservers = unmatchedQueries.map(options => {
@@ -2413,7 +2428,7 @@
2413
2428
  options.behavior = infiniteQueryBehavior();
2414
2429
  return super.getOptimisticResult(options);
2415
2430
  }
2416
- fetchNextPage(options = {}) {
2431
+ fetchNextPage(options) {
2417
2432
  return this.fetch({
2418
2433
  ...options,
2419
2434
  meta: {
@@ -2423,9 +2438,7 @@
2423
2438
  }
2424
2439
  });
2425
2440
  }
2426
- fetchPreviousPage({
2427
- ...options
2428
- } = {}) {
2441
+ fetchPreviousPage(options) {
2429
2442
  return this.fetch({
2430
2443
  ...options,
2431
2444
  meta: {
@@ -2450,8 +2463,8 @@
2450
2463
  ...result,
2451
2464
  fetchNextPage: this.fetchNextPage,
2452
2465
  fetchPreviousPage: this.fetchPreviousPage,
2453
- hasNextPage: hasNextPage(options, state.data?.pages),
2454
- hasPreviousPage: hasPreviousPage(options, state.data?.pages),
2466
+ hasNextPage: hasNextPage(options, state.data),
2467
+ hasPreviousPage: hasPreviousPage(options, state.data),
2455
2468
  isFetchingNextPage,
2456
2469
  isFetchingPreviousPage,
2457
2470
  isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
@@ -2489,6 +2502,7 @@
2489
2502
  observer: this
2490
2503
  });
2491
2504
  }
2505
+ this.#currentMutation?.setOptions(this.options);
2492
2506
  }
2493
2507
  onUnsubscribe() {
2494
2508
  if (!this.listeners.length) {
@@ -2576,24 +2590,10 @@
2576
2590
  return query.state.status === 'success';
2577
2591
  }
2578
2592
  function dehydrate(client, options = {}) {
2579
- const mutations = [];
2580
- const queries = [];
2581
- if (options.dehydrateMutations !== false) {
2582
- const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
2583
- client.getMutationCache().getAll().forEach(mutation => {
2584
- if (shouldDehydrateMutation(mutation)) {
2585
- mutations.push(dehydrateMutation(mutation));
2586
- }
2587
- });
2588
- }
2589
- if (options.dehydrateQueries !== false) {
2590
- const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
2591
- client.getQueryCache().getAll().forEach(query => {
2592
- if (shouldDehydrateQuery(query)) {
2593
- queries.push(dehydrateQuery(query));
2594
- }
2595
- });
2596
- }
2593
+ const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
2594
+ const mutations = client.getMutationCache().getAll().flatMap(mutation => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []);
2595
+ const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
2596
+ const queries = client.getQueryCache().getAll().flatMap(query => filterQuery(query) ? [dehydrateQuery(query)] : []);
2597
2597
  return {
2598
2598
  mutations,
2599
2599
  queries
@@ -2660,6 +2660,7 @@
2660
2660
  exports.isCancelledError = isCancelledError;
2661
2661
  exports.isServer = isServer;
2662
2662
  exports.keepPreviousData = keepPreviousData;
2663
+ exports.matchQuery = matchQuery;
2663
2664
  exports.notifyManager = notifyManager;
2664
2665
  exports.onlineManager = onlineManager;
2665
2666
  exports.replaceEqualDeep = replaceEqualDeep;