@tanstack/react-query 5.0.0-alpha.3 → 5.0.0-alpha.31
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/build/codemods/utils/index.js +205 -0
- package/build/codemods/utils/transformers/query-cache-transformer.js +116 -0
- package/build/codemods/utils/transformers/query-client-transformer.js +48 -0
- package/build/codemods/utils/transformers/use-query-like-transformer.js +32 -0
- package/build/codemods/v4/__testfixtures__/default-import.input.tsx +94 -0
- package/build/codemods/v4/__testfixtures__/default-import.output.tsx +96 -0
- package/build/codemods/v4/__testfixtures__/named-import.input.tsx +96 -0
- package/build/codemods/v4/__testfixtures__/named-import.output.tsx +98 -0
- package/build/codemods/v4/__testfixtures__/namespaced-import.input.tsx +86 -0
- package/build/codemods/v4/__testfixtures__/namespaced-import.output.tsx +88 -0
- package/build/codemods/v4/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
- package/build/codemods/v4/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
- package/build/codemods/v4/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
- package/build/codemods/v4/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
- package/build/codemods/v4/__testfixtures__/replace-import-specifier.input.tsx +10 -0
- package/build/codemods/v4/__testfixtures__/replace-import-specifier.output.tsx +10 -0
- package/build/codemods/v4/__testfixtures__/type-arguments.input.tsx +25 -0
- package/build/codemods/v4/__testfixtures__/type-arguments.output.tsx +31 -0
- package/build/codemods/v4/__tests__/key-transformation.test.js +32 -0
- package/build/codemods/v4/__tests__/replace-import-specifier.test.js +12 -0
- package/build/codemods/v4/key-transformation.js +138 -0
- package/build/codemods/v4/replace-import-specifier.js +25 -0
- package/build/codemods/v4/utils/replacers/key-replacer.js +164 -0
- package/build/codemods/v5/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
- package/build/codemods/v5/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
- package/build/codemods/v5/remove-overloads/__tests__/remove-overloads.test.js +6 -0
- package/build/codemods/v5/remove-overloads/remove-overloads.js +59 -0
- package/build/codemods/v5/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
- package/build/codemods/v5/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
- package/build/codemods/v5/remove-overloads/utils/index.js +124 -0
- package/build/codemods/v5/remove-overloads/utils/unknown-usage-error.js +26 -0
- package/build/lib/HydrationBoundary.d.ts +1 -0
- package/build/lib/HydrationBoundary.d.ts.map +1 -0
- package/build/lib/HydrationBoundary.esm.js +1 -0
- package/build/lib/HydrationBoundary.esm.js.map +1 -1
- package/build/lib/HydrationBoundary.js +1 -0
- package/build/lib/HydrationBoundary.js.map +1 -1
- package/build/lib/HydrationBoundary.mjs +1 -0
- package/build/lib/HydrationBoundary.mjs.map +1 -1
- package/build/lib/QueryClientProvider.d.ts +1 -0
- package/build/lib/QueryClientProvider.d.ts.map +1 -0
- package/build/lib/QueryClientProvider.esm.js +1 -0
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +1 -0
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +1 -0
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.d.ts +1 -0
- package/build/lib/QueryErrorResetBoundary.d.ts.map +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +1 -0
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +1 -0
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/HydrationBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/HydrationBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr.test.d.ts +1 -3
- package/build/lib/__tests__/ssr.test.d.ts.map +1 -0
- package/build/lib/__tests__/suspense.test.d.ts +1 -0
- package/build/lib/__tests__/suspense.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.d.ts +3 -3
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.d.ts +4 -3
- package/build/lib/errorBoundaryUtils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.esm.js +4 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +4 -3
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +4 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +2 -1
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +1 -0
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/isRestoring.d.ts +1 -0
- package/build/lib/isRestoring.d.ts.map +1 -0
- package/build/lib/isRestoring.esm.js +1 -0
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +1 -0
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs +1 -0
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.d.ts +2 -1
- package/build/lib/suspense.d.ts.map +1 -0
- package/build/lib/suspense.esm.js +2 -9
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +2 -9
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +1 -8
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +1 -0
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/useBaseQuery.d.ts +1 -0
- package/build/lib/useBaseQuery.d.ts.map +1 -0
- package/build/lib/useBaseQuery.esm.js +2 -12
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +2 -12
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +2 -12
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +1 -0
- package/build/lib/useInfiniteQuery.d.ts.map +1 -0
- package/build/lib/useInfiniteQuery.esm.js +1 -0
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +1 -0
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +1 -0
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +1 -0
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsFetching.esm.js +1 -0
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +1 -0
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +1 -0
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +1 -0
- package/build/lib/useMutation.d.ts.map +1 -0
- package/build/lib/useMutation.esm.js +2 -1
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +2 -1
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +2 -1
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +4 -3
- package/build/lib/useMutationState.d.ts.map +1 -0
- package/build/lib/useMutationState.esm.js +1 -0
- package/build/lib/useMutationState.esm.js.map +1 -1
- package/build/lib/useMutationState.js +1 -0
- package/build/lib/useMutationState.js.map +1 -1
- package/build/lib/useMutationState.mjs +1 -0
- package/build/lib/useMutationState.mjs.map +1 -1
- package/build/lib/useQueries.d.ts +3 -3
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.esm.js +20 -16
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +20 -16
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +12 -11
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +3 -0
- package/build/lib/useQuery.d.ts.map +1 -0
- package/build/lib/useQuery.esm.js +6 -1
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +6 -0
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +6 -1
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/umd/index.development.js +124 -155
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +9 -4
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +753 -620
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +17 -98
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +94 -13
- package/src/__tests__/useMutation.test.tsx +25 -24
- package/src/__tests__/useMutationState.test.tsx +24 -58
- package/src/__tests__/useQueries.test.tsx +34 -163
- package/src/__tests__/useQuery.test.tsx +234 -365
- package/src/__tests__/useQuery.types.test.tsx +21 -1
- package/src/__tests__/utils.tsx +3 -2
- package/src/errorBoundaryUtils.ts +6 -5
- package/src/index.ts +1 -1
- package/src/suspense.ts +3 -11
- package/src/useBaseQuery.ts +2 -20
- package/src/useInfiniteQuery.ts +1 -0
- package/src/useIsFetching.ts +1 -0
- package/src/useMutation.ts +2 -1
- package/src/useMutationState.ts +4 -3
- package/src/useQueries.ts +20 -19
- package/src/useQuery.ts +23 -0
|
@@ -926,7 +926,7 @@
|
|
|
926
926
|
{
|
|
927
927
|
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}`);
|
|
928
928
|
}
|
|
929
|
-
onError(new Error(
|
|
929
|
+
onError(new Error(`${this.queryHash} data is undefined`));
|
|
930
930
|
return;
|
|
931
931
|
}
|
|
932
932
|
this.setData(data);
|
|
@@ -1043,7 +1043,7 @@
|
|
|
1043
1043
|
this.state = reducer(this.state);
|
|
1044
1044
|
notifyManager.batch(() => {
|
|
1045
1045
|
this.#observers.forEach(observer => {
|
|
1046
|
-
observer.onQueryUpdate(
|
|
1046
|
+
observer.onQueryUpdate();
|
|
1047
1047
|
});
|
|
1048
1048
|
this.#cache.notify({
|
|
1049
1049
|
query: this,
|
|
@@ -1080,7 +1080,7 @@
|
|
|
1080
1080
|
constructor(config = {}) {
|
|
1081
1081
|
super();
|
|
1082
1082
|
this.config = config;
|
|
1083
|
-
this.#queries =
|
|
1083
|
+
this.#queries = new Map();
|
|
1084
1084
|
}
|
|
1085
1085
|
build(client, options, state) {
|
|
1086
1086
|
const queryKey = options.queryKey;
|
|
@@ -1135,10 +1135,11 @@
|
|
|
1135
1135
|
return [...this.#queries.values()];
|
|
1136
1136
|
}
|
|
1137
1137
|
find(filters) {
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1138
|
+
const defaultedFilters = {
|
|
1139
|
+
exact: true,
|
|
1140
|
+
...filters
|
|
1141
|
+
};
|
|
1142
|
+
return this.getAll().find(query => matchQuery(defaultedFilters, query));
|
|
1142
1143
|
}
|
|
1143
1144
|
findAll(filters = {}) {
|
|
1144
1145
|
const queries = this.getAll();
|
|
@@ -1444,10 +1445,11 @@
|
|
|
1444
1445
|
return this.#mutations;
|
|
1445
1446
|
}
|
|
1446
1447
|
find(filters) {
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1448
|
+
const defaultedFilters = {
|
|
1449
|
+
exact: true,
|
|
1450
|
+
...filters
|
|
1451
|
+
};
|
|
1452
|
+
return this.#mutations.find(mutation => matchMutation(defaultedFilters, mutation));
|
|
1451
1453
|
}
|
|
1452
1454
|
findAll(filters = {}) {
|
|
1453
1455
|
return this.#mutations.filter(mutation => matchMutation(filters, mutation));
|
|
@@ -1473,12 +1475,15 @@
|
|
|
1473
1475
|
function infiniteQueryBehavior() {
|
|
1474
1476
|
return {
|
|
1475
1477
|
onFetch: context => {
|
|
1476
|
-
context.fetchFn = () => {
|
|
1478
|
+
context.fetchFn = async () => {
|
|
1477
1479
|
const options = context.options;
|
|
1478
1480
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction;
|
|
1479
1481
|
const oldPages = context.state.data?.pages || [];
|
|
1480
1482
|
const oldPageParams = context.state.data?.pageParams || [];
|
|
1481
|
-
|
|
1483
|
+
const empty = {
|
|
1484
|
+
pages: [],
|
|
1485
|
+
pageParams: []
|
|
1486
|
+
};
|
|
1482
1487
|
let cancelled = false;
|
|
1483
1488
|
const addSignalProperty = object => {
|
|
1484
1489
|
Object.defineProperty(object, 'signal', {
|
|
@@ -1498,95 +1503,95 @@
|
|
|
1498
1503
|
|
|
1499
1504
|
// Get query function
|
|
1500
1505
|
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
1506
|
|
|
1513
1507
|
// Create function to fetch a page
|
|
1514
|
-
const fetchPage = (
|
|
1508
|
+
const fetchPage = async (data, param, previous) => {
|
|
1515
1509
|
if (cancelled) {
|
|
1516
1510
|
return Promise.reject();
|
|
1517
1511
|
}
|
|
1518
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
1519
|
-
return Promise.resolve(
|
|
1512
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
1513
|
+
return Promise.resolve(data);
|
|
1520
1514
|
}
|
|
1521
1515
|
const queryFnContext = {
|
|
1522
1516
|
queryKey: context.queryKey,
|
|
1523
1517
|
pageParam: param,
|
|
1518
|
+
direction: previous ? 'backward' : 'forward',
|
|
1524
1519
|
meta: context.options.meta
|
|
1525
1520
|
};
|
|
1526
1521
|
addSignalProperty(queryFnContext);
|
|
1527
|
-
const
|
|
1528
|
-
const
|
|
1529
|
-
|
|
1522
|
+
const page = await queryFn(queryFnContext);
|
|
1523
|
+
const {
|
|
1524
|
+
maxPages
|
|
1525
|
+
} = context.options;
|
|
1526
|
+
const addTo = previous ? addToStart : addToEnd;
|
|
1527
|
+
return {
|
|
1528
|
+
pages: addTo(data.pages, page, maxPages),
|
|
1529
|
+
pageParams: addTo(data.pageParams, param, maxPages)
|
|
1530
|
+
};
|
|
1530
1531
|
};
|
|
1531
|
-
let
|
|
1532
|
+
let result;
|
|
1532
1533
|
|
|
1533
1534
|
// Fetch first page?
|
|
1534
1535
|
if (!oldPages.length) {
|
|
1535
|
-
|
|
1536
|
+
result = await fetchPage(empty, options.defaultPageParam);
|
|
1536
1537
|
}
|
|
1537
1538
|
|
|
1538
1539
|
// fetch next / previous page?
|
|
1539
1540
|
else if (direction) {
|
|
1540
1541
|
const previous = direction === 'backward';
|
|
1541
|
-
const
|
|
1542
|
-
|
|
1542
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
|
|
1543
|
+
const oldData = {
|
|
1544
|
+
pages: oldPages,
|
|
1545
|
+
pageParams: oldPageParams
|
|
1546
|
+
};
|
|
1547
|
+
const param = pageParamFn(options, oldData);
|
|
1548
|
+
result = await fetchPage(oldData, param, previous);
|
|
1543
1549
|
}
|
|
1544
1550
|
|
|
1545
1551
|
// Refetch pages
|
|
1546
1552
|
else {
|
|
1547
|
-
newPageParams = [];
|
|
1548
|
-
|
|
1549
1553
|
// Fetch first page
|
|
1550
|
-
|
|
1554
|
+
result = await fetchPage(empty, oldPageParams[0]);
|
|
1551
1555
|
|
|
1552
1556
|
// Fetch remaining pages
|
|
1553
1557
|
for (let i = 1; i < oldPages.length; i++) {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
return fetchPage(pages, param);
|
|
1557
|
-
});
|
|
1558
|
+
const param = getNextPageParam(options, result);
|
|
1559
|
+
result = await fetchPage(result, param);
|
|
1558
1560
|
}
|
|
1559
1561
|
}
|
|
1560
|
-
|
|
1561
|
-
pages,
|
|
1562
|
-
pageParams: newPageParams
|
|
1563
|
-
}));
|
|
1564
|
-
return finalPromise;
|
|
1562
|
+
return result;
|
|
1565
1563
|
};
|
|
1566
1564
|
}
|
|
1567
1565
|
};
|
|
1568
1566
|
}
|
|
1569
|
-
function getNextPageParam(options,
|
|
1570
|
-
|
|
1567
|
+
function getNextPageParam(options, {
|
|
1568
|
+
pages,
|
|
1569
|
+
pageParams
|
|
1570
|
+
}) {
|
|
1571
|
+
const lastIndex = pages.length - 1;
|
|
1572
|
+
return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
|
|
1571
1573
|
}
|
|
1572
|
-
function getPreviousPageParam(options,
|
|
1573
|
-
|
|
1574
|
+
function getPreviousPageParam(options, {
|
|
1575
|
+
pages,
|
|
1576
|
+
pageParams
|
|
1577
|
+
}) {
|
|
1578
|
+
return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
|
|
1574
1579
|
}
|
|
1575
1580
|
|
|
1576
1581
|
/**
|
|
1577
1582
|
* Checks if there is a next page.
|
|
1578
1583
|
*/
|
|
1579
|
-
function hasNextPage(options,
|
|
1580
|
-
if (!
|
|
1581
|
-
return typeof getNextPageParam(options,
|
|
1584
|
+
function hasNextPage(options, data) {
|
|
1585
|
+
if (!data) return false;
|
|
1586
|
+
return typeof getNextPageParam(options, data) !== 'undefined';
|
|
1582
1587
|
}
|
|
1583
1588
|
|
|
1584
1589
|
/**
|
|
1585
1590
|
* Checks if there is a previous page.
|
|
1586
1591
|
*/
|
|
1587
|
-
function hasPreviousPage(options,
|
|
1588
|
-
if (!
|
|
1589
|
-
return typeof getPreviousPageParam(options,
|
|
1592
|
+
function hasPreviousPage(options, data) {
|
|
1593
|
+
if (!data || !options.getPreviousPageParam) return false;
|
|
1594
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined';
|
|
1590
1595
|
}
|
|
1591
1596
|
|
|
1592
1597
|
// CLASS
|
|
@@ -1711,10 +1716,11 @@
|
|
|
1711
1716
|
});
|
|
1712
1717
|
}
|
|
1713
1718
|
cancelQueries(filters = {}, cancelOptions = {}) {
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1719
|
+
const defaultedCancelOptions = {
|
|
1720
|
+
revert: true,
|
|
1721
|
+
...cancelOptions
|
|
1722
|
+
};
|
|
1723
|
+
const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map(query => query.cancel(defaultedCancelOptions)));
|
|
1718
1724
|
return Promise.all(promises).then(noop$1).catch(noop$1);
|
|
1719
1725
|
}
|
|
1720
1726
|
invalidateQueries(filters = {}, options = {}) {
|
|
@@ -1733,15 +1739,18 @@
|
|
|
1733
1739
|
});
|
|
1734
1740
|
}
|
|
1735
1741
|
refetchQueries(filters = {}, options) {
|
|
1736
|
-
const
|
|
1742
|
+
const fetchOptions = {
|
|
1737
1743
|
...options,
|
|
1738
1744
|
cancelRefetch: options?.cancelRefetch ?? true
|
|
1739
|
-
}
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
+
};
|
|
1746
|
+
const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
|
|
1747
|
+
let promise = query.fetch(undefined, fetchOptions);
|
|
1748
|
+
if (!fetchOptions.throwOnError) {
|
|
1749
|
+
promise = promise.catch(noop$1);
|
|
1750
|
+
}
|
|
1751
|
+
return query.state.fetchStatus === 'paused' ? Promise.resolve() : promise;
|
|
1752
|
+
}));
|
|
1753
|
+
return Promise.all(promises).then(noop$1);
|
|
1745
1754
|
}
|
|
1746
1755
|
fetchQuery(options) {
|
|
1747
1756
|
const defaultedOptions = this.defaultQueryOptions(options);
|
|
@@ -1834,8 +1843,8 @@
|
|
|
1834
1843
|
if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
|
|
1835
1844
|
defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
|
|
1836
1845
|
}
|
|
1837
|
-
if (typeof defaultedOptions.
|
|
1838
|
-
defaultedOptions.
|
|
1846
|
+
if (typeof defaultedOptions.throwOnError === 'undefined') {
|
|
1847
|
+
defaultedOptions.throwOnError = !!defaultedOptions.suspense;
|
|
1839
1848
|
}
|
|
1840
1849
|
return defaultedOptions;
|
|
1841
1850
|
}
|
|
@@ -1863,10 +1872,12 @@
|
|
|
1863
1872
|
#currentResult = undefined;
|
|
1864
1873
|
#currentResultState;
|
|
1865
1874
|
#currentResultOptions;
|
|
1866
|
-
#previousQueryResult;
|
|
1867
1875
|
#selectError;
|
|
1868
1876
|
#selectFn;
|
|
1869
1877
|
#selectResult;
|
|
1878
|
+
// This property keeps track of the last query with defined data.
|
|
1879
|
+
// It will be used to pass the previous data and query to the placeholder function between renders.
|
|
1880
|
+
#lastQueryWithDefinedData;
|
|
1870
1881
|
#staleTimeoutId;
|
|
1871
1882
|
#refetchIntervalId;
|
|
1872
1883
|
#currentRefetchInterval;
|
|
@@ -2023,7 +2034,7 @@
|
|
|
2023
2034
|
}, timeout);
|
|
2024
2035
|
}
|
|
2025
2036
|
#computeRefetchInterval() {
|
|
2026
|
-
return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval ?? false;
|
|
2037
|
+
return (typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval) ?? false;
|
|
2027
2038
|
}
|
|
2028
2039
|
#updateRefetchInterval(nextInterval) {
|
|
2029
2040
|
this.#clearRefetchInterval();
|
|
@@ -2061,7 +2072,6 @@
|
|
|
2061
2072
|
const prevResultOptions = this.#currentResultOptions;
|
|
2062
2073
|
const queryChange = query !== prevQuery;
|
|
2063
2074
|
const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
|
|
2064
|
-
const prevQueryResult = queryChange ? this.#currentResult : this.#previousQueryResult;
|
|
2065
2075
|
const {
|
|
2066
2076
|
state
|
|
2067
2077
|
} = query;
|
|
@@ -2120,7 +2130,7 @@
|
|
|
2120
2130
|
if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
|
|
2121
2131
|
placeholderData = prevResult.data;
|
|
2122
2132
|
} else {
|
|
2123
|
-
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(
|
|
2133
|
+
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
|
|
2124
2134
|
if (options.select && typeof placeholderData !== 'undefined') {
|
|
2125
2135
|
try {
|
|
2126
2136
|
placeholderData = options.select(placeholderData);
|
|
@@ -2184,6 +2194,9 @@
|
|
|
2184
2194
|
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
2185
2195
|
return;
|
|
2186
2196
|
}
|
|
2197
|
+
if (this.#currentResultState.data !== undefined) {
|
|
2198
|
+
this.#lastQueryWithDefinedData = this.#currentQuery;
|
|
2199
|
+
}
|
|
2187
2200
|
this.#currentResult = nextResult;
|
|
2188
2201
|
|
|
2189
2202
|
// Determine which callbacks to trigger
|
|
@@ -2199,7 +2212,7 @@
|
|
|
2199
2212
|
return true;
|
|
2200
2213
|
}
|
|
2201
2214
|
const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
|
|
2202
|
-
if (this.options.
|
|
2215
|
+
if (this.options.throwOnError) {
|
|
2203
2216
|
includedProps.add('error');
|
|
2204
2217
|
}
|
|
2205
2218
|
return Object.keys(this.#currentResult).some(key => {
|
|
@@ -2224,36 +2237,20 @@
|
|
|
2224
2237
|
const prevQuery = this.#currentQuery;
|
|
2225
2238
|
this.#currentQuery = query;
|
|
2226
2239
|
this.#currentQueryInitialState = query.state;
|
|
2227
|
-
this.#previousQueryResult = this.#currentResult;
|
|
2228
2240
|
if (this.hasListeners()) {
|
|
2229
2241
|
prevQuery?.removeObserver(this);
|
|
2230
2242
|
query.addObserver(this);
|
|
2231
2243
|
}
|
|
2232
2244
|
}
|
|
2233
|
-
onQueryUpdate(
|
|
2234
|
-
|
|
2235
|
-
if (action.type === 'success') {
|
|
2236
|
-
notifyOptions.onSuccess = !action.manual;
|
|
2237
|
-
} else if (action.type === 'error' && !isCancelledError(action.error)) {
|
|
2238
|
-
notifyOptions.onError = true;
|
|
2239
|
-
}
|
|
2240
|
-
this.#updateResult(notifyOptions);
|
|
2245
|
+
onQueryUpdate() {
|
|
2246
|
+
this.#updateResult();
|
|
2241
2247
|
if (this.hasListeners()) {
|
|
2242
2248
|
this.#updateTimers();
|
|
2243
2249
|
}
|
|
2244
2250
|
}
|
|
2245
2251
|
#notify(notifyOptions) {
|
|
2246
2252
|
notifyManager.batch(() => {
|
|
2247
|
-
// First trigger the
|
|
2248
|
-
if (notifyOptions.onSuccess) {
|
|
2249
|
-
this.options.onSuccess?.(this.#currentResult.data);
|
|
2250
|
-
this.options.onSettled?.(this.#currentResult.data, null);
|
|
2251
|
-
} else if (notifyOptions.onError) {
|
|
2252
|
-
this.options.onError?.(this.#currentResult.error);
|
|
2253
|
-
this.options.onSettled?.(undefined, this.#currentResult.error);
|
|
2254
|
-
}
|
|
2255
|
-
|
|
2256
|
-
// Then trigger the listeners
|
|
2253
|
+
// First, trigger the listeners
|
|
2257
2254
|
if (notifyOptions.listeners) {
|
|
2258
2255
|
this.listeners.forEach(listener => {
|
|
2259
2256
|
listener(this.#currentResult);
|
|
@@ -2375,9 +2372,10 @@
|
|
|
2375
2372
|
}
|
|
2376
2373
|
#findMatchingObservers(queries) {
|
|
2377
2374
|
const prevObservers = this.#observers;
|
|
2375
|
+
const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
|
|
2378
2376
|
const defaultedQueryOptions = queries.map(options => this.#client.defaultQueryOptions(options));
|
|
2379
2377
|
const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
|
|
2380
|
-
const match =
|
|
2378
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash);
|
|
2381
2379
|
if (match != null) {
|
|
2382
2380
|
return [{
|
|
2383
2381
|
defaultedQueryOptions: defaultedOptions,
|
|
@@ -2386,8 +2384,8 @@
|
|
|
2386
2384
|
}
|
|
2387
2385
|
return [];
|
|
2388
2386
|
});
|
|
2389
|
-
const matchedQueryHashes = matchingObservers.map(match => match.defaultedQueryOptions.queryHash);
|
|
2390
|
-
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.
|
|
2387
|
+
const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
|
|
2388
|
+
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
|
|
2391
2389
|
const getObserver = options => {
|
|
2392
2390
|
const defaultedOptions = this.#client.defaultQueryOptions(options);
|
|
2393
2391
|
const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
|
|
@@ -2444,7 +2442,7 @@
|
|
|
2444
2442
|
options.behavior = infiniteQueryBehavior();
|
|
2445
2443
|
return super.getOptimisticResult(options);
|
|
2446
2444
|
}
|
|
2447
|
-
fetchNextPage(options
|
|
2445
|
+
fetchNextPage(options) {
|
|
2448
2446
|
return this.fetch({
|
|
2449
2447
|
...options,
|
|
2450
2448
|
meta: {
|
|
@@ -2454,9 +2452,7 @@
|
|
|
2454
2452
|
}
|
|
2455
2453
|
});
|
|
2456
2454
|
}
|
|
2457
|
-
fetchPreviousPage({
|
|
2458
|
-
...options
|
|
2459
|
-
} = {}) {
|
|
2455
|
+
fetchPreviousPage(options) {
|
|
2460
2456
|
return this.fetch({
|
|
2461
2457
|
...options,
|
|
2462
2458
|
meta: {
|
|
@@ -2481,8 +2477,8 @@
|
|
|
2481
2477
|
...result,
|
|
2482
2478
|
fetchNextPage: this.fetchNextPage,
|
|
2483
2479
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
2484
|
-
hasNextPage: hasNextPage(options, state.data
|
|
2485
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
2480
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
2481
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
2486
2482
|
isFetchingNextPage,
|
|
2487
2483
|
isFetchingPreviousPage,
|
|
2488
2484
|
isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
|
|
@@ -2608,24 +2604,10 @@
|
|
|
2608
2604
|
return query.state.status === 'success';
|
|
2609
2605
|
}
|
|
2610
2606
|
function dehydrate(client, options = {}) {
|
|
2611
|
-
const
|
|
2612
|
-
const
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
client.getMutationCache().getAll().forEach(mutation => {
|
|
2616
|
-
if (shouldDehydrateMutation(mutation)) {
|
|
2617
|
-
mutations.push(dehydrateMutation(mutation));
|
|
2618
|
-
}
|
|
2619
|
-
});
|
|
2620
|
-
}
|
|
2621
|
-
if (options.dehydrateQueries !== false) {
|
|
2622
|
-
const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
|
|
2623
|
-
client.getQueryCache().getAll().forEach(query => {
|
|
2624
|
-
if (shouldDehydrateQuery(query)) {
|
|
2625
|
-
queries.push(dehydrateQuery(query));
|
|
2626
|
-
}
|
|
2627
|
-
});
|
|
2628
|
-
}
|
|
2607
|
+
const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
|
|
2608
|
+
const mutations = client.getMutationCache().getAll().flatMap(mutation => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []);
|
|
2609
|
+
const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
|
|
2610
|
+
const queries = client.getQueryCache().getAll().flatMap(query => filterQuery(query) ? [dehydrateQuery(query)] : []);
|
|
2629
2611
|
return {
|
|
2630
2612
|
mutations,
|
|
2631
2613
|
queries
|
|
@@ -2747,7 +2729,7 @@
|
|
|
2747
2729
|
}
|
|
2748
2730
|
|
|
2749
2731
|
const ensurePreventErrorBoundaryRetry = (options, errorResetBoundary) => {
|
|
2750
|
-
if (options.suspense || options.
|
|
2732
|
+
if (options.suspense || options.throwOnError) {
|
|
2751
2733
|
// Prevent retrying failed query if the error boundary has not been reset yet
|
|
2752
2734
|
if (!errorResetBoundary.isReset()) {
|
|
2753
2735
|
options.retryOnMount = false;
|
|
@@ -2762,10 +2744,10 @@
|
|
|
2762
2744
|
const getHasError = ({
|
|
2763
2745
|
result,
|
|
2764
2746
|
errorResetBoundary,
|
|
2765
|
-
|
|
2747
|
+
throwOnError,
|
|
2766
2748
|
query
|
|
2767
2749
|
}) => {
|
|
2768
|
-
return result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(
|
|
2750
|
+
return result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(throwOnError, [result.error, query]);
|
|
2769
2751
|
};
|
|
2770
2752
|
|
|
2771
2753
|
const ensureStaleTime = defaultedOptions => {
|
|
@@ -2779,26 +2761,19 @@
|
|
|
2779
2761
|
};
|
|
2780
2762
|
const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
|
|
2781
2763
|
const shouldSuspend = (defaultedOptions, result, isRestoring) => defaultedOptions?.suspense && willFetch(result, isRestoring);
|
|
2782
|
-
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).
|
|
2783
|
-
data
|
|
2784
|
-
}) => {
|
|
2785
|
-
defaultedOptions.onSuccess?.(data);
|
|
2786
|
-
defaultedOptions.onSettled?.(data, null);
|
|
2787
|
-
}).catch(error => {
|
|
2764
|
+
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).catch(() => {
|
|
2788
2765
|
errorResetBoundary.clearReset();
|
|
2789
|
-
defaultedOptions.onError?.(error);
|
|
2790
|
-
defaultedOptions.onSettled?.(undefined, error);
|
|
2791
2766
|
});
|
|
2792
2767
|
|
|
2793
2768
|
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
2794
2769
|
// `placeholderData` function does not have a parameter
|
|
2795
2770
|
|
|
2796
2771
|
function useQueries({
|
|
2797
|
-
queries
|
|
2798
|
-
|
|
2799
|
-
}) {
|
|
2772
|
+
queries
|
|
2773
|
+
}, queryClient) {
|
|
2800
2774
|
const client = useQueryClient(queryClient);
|
|
2801
2775
|
const isRestoring = useIsRestoring();
|
|
2776
|
+
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
2802
2777
|
const defaultedQueries = React__namespace.useMemo(() => queries.map(options => {
|
|
2803
2778
|
const defaultedOptions = client.defaultQueryOptions(options);
|
|
2804
2779
|
|
|
@@ -2806,6 +2781,11 @@
|
|
|
2806
2781
|
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
|
|
2807
2782
|
return defaultedOptions;
|
|
2808
2783
|
}), [queries, client, isRestoring]);
|
|
2784
|
+
defaultedQueries.forEach(query => {
|
|
2785
|
+
ensureStaleTime(query);
|
|
2786
|
+
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
|
2787
|
+
});
|
|
2788
|
+
useClearResetErrorBoundary(errorResetBoundary);
|
|
2809
2789
|
const [observer] = React__namespace.useState(() => new QueriesObserver(client, defaultedQueries));
|
|
2810
2790
|
const optimisticResult = observer.getOptimisticResult(defaultedQueries);
|
|
2811
2791
|
React__namespace.useSyncExternalStore(React__namespace.useCallback(onStoreChange => isRestoring ? () => undefined : observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
@@ -2816,12 +2796,6 @@
|
|
|
2816
2796
|
listeners: false
|
|
2817
2797
|
});
|
|
2818
2798
|
}, [defaultedQueries, observer]);
|
|
2819
|
-
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
2820
|
-
defaultedQueries.forEach(query => {
|
|
2821
|
-
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
|
2822
|
-
ensureStaleTime(query);
|
|
2823
|
-
});
|
|
2824
|
-
useClearResetErrorBoundary(errorResetBoundary);
|
|
2825
2799
|
const shouldAtLeastOneSuspend = optimisticResult.some((result, index) => shouldSuspend(defaultedQueries[index], result, isRestoring));
|
|
2826
2800
|
const suspensePromises = shouldAtLeastOneSuspend ? optimisticResult.flatMap((result, index) => {
|
|
2827
2801
|
const options = defaultedQueries[index];
|
|
@@ -2838,11 +2812,12 @@
|
|
|
2838
2812
|
if (suspensePromises.length > 0) {
|
|
2839
2813
|
throw Promise.all(suspensePromises);
|
|
2840
2814
|
}
|
|
2815
|
+
const observerQueries = observer.getQueries();
|
|
2841
2816
|
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => getHasError({
|
|
2842
2817
|
result,
|
|
2843
2818
|
errorResetBoundary,
|
|
2844
|
-
|
|
2845
|
-
query:
|
|
2819
|
+
throwOnError: defaultedQueries[index]?.throwOnError ?? false,
|
|
2820
|
+
query: observerQueries[index]
|
|
2846
2821
|
}));
|
|
2847
2822
|
if (firstSingleResultWhichShouldThrow?.error) {
|
|
2848
2823
|
throw firstSingleResultWhichShouldThrow.error;
|
|
@@ -2858,17 +2833,6 @@
|
|
|
2858
2833
|
|
|
2859
2834
|
// Make sure results are optimistically set in fetching state before subscribing or updating options
|
|
2860
2835
|
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
|
|
2861
|
-
|
|
2862
|
-
// Include callbacks in batch renders
|
|
2863
|
-
if (defaultedOptions.onError) {
|
|
2864
|
-
defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
|
|
2865
|
-
}
|
|
2866
|
-
if (defaultedOptions.onSuccess) {
|
|
2867
|
-
defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
|
|
2868
|
-
}
|
|
2869
|
-
if (defaultedOptions.onSettled) {
|
|
2870
|
-
defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
|
|
2871
|
-
}
|
|
2872
2836
|
ensureStaleTime(defaultedOptions);
|
|
2873
2837
|
ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary);
|
|
2874
2838
|
useClearResetErrorBoundary(errorResetBoundary);
|
|
@@ -2892,7 +2856,7 @@
|
|
|
2892
2856
|
if (getHasError({
|
|
2893
2857
|
result,
|
|
2894
2858
|
errorResetBoundary,
|
|
2895
|
-
|
|
2859
|
+
throwOnError: defaultedOptions.throwOnError,
|
|
2896
2860
|
query: observer.getCurrentQuery()
|
|
2897
2861
|
})) {
|
|
2898
2862
|
throw result.error;
|
|
@@ -2902,6 +2866,10 @@
|
|
|
2902
2866
|
return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result;
|
|
2903
2867
|
}
|
|
2904
2868
|
|
|
2869
|
+
function queryOptions(options) {
|
|
2870
|
+
return options;
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2905
2873
|
// HOOK
|
|
2906
2874
|
|
|
2907
2875
|
function useQuery(options, queryClient) {
|
|
@@ -2979,7 +2947,7 @@
|
|
|
2979
2947
|
const mutate = React__namespace.useCallback((variables, mutateOptions) => {
|
|
2980
2948
|
observer.mutate(variables, mutateOptions).catch(noop);
|
|
2981
2949
|
}, [observer]);
|
|
2982
|
-
if (result.error && shouldThrowError(observer.options.
|
|
2950
|
+
if (result.error && shouldThrowError(observer.options.throwOnError, [result.error])) {
|
|
2983
2951
|
throw result.error;
|
|
2984
2952
|
}
|
|
2985
2953
|
return {
|
|
@@ -3024,6 +2992,7 @@
|
|
|
3024
2992
|
exports.matchQuery = matchQuery;
|
|
3025
2993
|
exports.notifyManager = notifyManager;
|
|
3026
2994
|
exports.onlineManager = onlineManager;
|
|
2995
|
+
exports.queryOptions = queryOptions;
|
|
3027
2996
|
exports.replaceEqualDeep = replaceEqualDeep;
|
|
3028
2997
|
exports.useInfiniteQuery = useInfiniteQuery;
|
|
3029
2998
|
exports.useIsFetching = useIsFetching;
|