@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.43
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/__testfixtures__/default-import.input.tsx +94 -0
- package/build/codemods/__testfixtures__/default-import.output.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.input.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.output.tsx +98 -0
- package/build/codemods/__testfixtures__/namespaced-import.input.tsx +86 -0
- package/build/codemods/__testfixtures__/namespaced-import.output.tsx +88 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.input.tsx +10 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.output.tsx +10 -0
- package/build/codemods/__testfixtures__/type-arguments.input.tsx +25 -0
- package/build/codemods/__testfixtures__/type-arguments.output.tsx +31 -0
- package/build/codemods/__tests__/key-transformation.test.js +32 -0
- package/build/codemods/__tests__/replace-import-specifier.test.js +12 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
- package/build/codemods/remove-overloads/__tests__/remove-overloads.test.js +6 -0
- package/build/codemods/remove-overloads/remove-overloads.js +59 -0
- package/build/codemods/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
- package/build/codemods/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
- package/build/codemods/remove-overloads/utils/index.js +124 -0
- package/build/codemods/remove-overloads/utils/unknown-usage-error.js +26 -0
- package/build/codemods/src/utils/index.js +205 -0
- package/build/codemods/src/v4/key-transformation.js +138 -0
- package/build/codemods/src/v4/replace-import-specifier.js +25 -0
- package/build/codemods/transformers/query-cache-transformer.js +116 -0
- package/build/codemods/transformers/query-client-transformer.js +48 -0
- package/build/codemods/transformers/use-query-like-transformer.js +32 -0
- package/build/codemods/utils/replacers/key-replacer.js +164 -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 +2 -1
- 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 +2 -1
- 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 +6 -6
- 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 +3 -5
- 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 +11 -10
- 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 +10 -8
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.esm.js +39 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +39 -27
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +31 -22
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +5 -2
- 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/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +191 -187
- 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 +13 -6
- 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 +217 -154
- 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 +9 -15
- 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 +44 -26
- package/src/useQuery.ts +23 -0
|
@@ -25,19 +25,19 @@
|
|
|
25
25
|
|
|
26
26
|
class Subscribable {
|
|
27
27
|
constructor() {
|
|
28
|
-
this.listeners =
|
|
28
|
+
this.listeners = new Set();
|
|
29
29
|
this.subscribe = this.subscribe.bind(this);
|
|
30
30
|
}
|
|
31
31
|
subscribe(listener) {
|
|
32
|
-
this.listeners.
|
|
32
|
+
this.listeners.add(listener);
|
|
33
33
|
this.onSubscribe();
|
|
34
34
|
return () => {
|
|
35
|
-
this.listeners
|
|
35
|
+
this.listeners.delete(listener);
|
|
36
36
|
this.onUnsubscribe();
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
hasListeners() {
|
|
40
|
-
return this.listeners.
|
|
40
|
+
return this.listeners.size > 0;
|
|
41
41
|
}
|
|
42
42
|
onSubscribe() {
|
|
43
43
|
// Do nothing
|
|
@@ -406,6 +406,8 @@
|
|
|
406
406
|
}
|
|
407
407
|
const onlineManager = new OnlineManager();
|
|
408
408
|
|
|
409
|
+
// TYPES
|
|
410
|
+
|
|
409
411
|
function defaultRetryDelay(failureCount) {
|
|
410
412
|
return Math.min(1000 * 2 ** failureCount, 30000);
|
|
411
413
|
}
|
|
@@ -926,7 +928,7 @@
|
|
|
926
928
|
{
|
|
927
929
|
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
930
|
}
|
|
929
|
-
onError(new Error(
|
|
931
|
+
onError(new Error(`${this.queryHash} data is undefined`));
|
|
930
932
|
return;
|
|
931
933
|
}
|
|
932
934
|
this.setData(data);
|
|
@@ -1043,7 +1045,7 @@
|
|
|
1043
1045
|
this.state = reducer(this.state);
|
|
1044
1046
|
notifyManager.batch(() => {
|
|
1045
1047
|
this.#observers.forEach(observer => {
|
|
1046
|
-
observer.onQueryUpdate(
|
|
1048
|
+
observer.onQueryUpdate();
|
|
1047
1049
|
});
|
|
1048
1050
|
this.#cache.notify({
|
|
1049
1051
|
query: this,
|
|
@@ -1073,6 +1075,8 @@
|
|
|
1073
1075
|
};
|
|
1074
1076
|
}
|
|
1075
1077
|
|
|
1078
|
+
// TYPES
|
|
1079
|
+
|
|
1076
1080
|
// CLASS
|
|
1077
1081
|
|
|
1078
1082
|
class QueryCache extends Subscribable {
|
|
@@ -1080,7 +1084,7 @@
|
|
|
1080
1084
|
constructor(config = {}) {
|
|
1081
1085
|
super();
|
|
1082
1086
|
this.config = config;
|
|
1083
|
-
this.#queries =
|
|
1087
|
+
this.#queries = new Map();
|
|
1084
1088
|
}
|
|
1085
1089
|
build(client, options, state) {
|
|
1086
1090
|
const queryKey = options.queryKey;
|
|
@@ -1135,10 +1139,11 @@
|
|
|
1135
1139
|
return [...this.#queries.values()];
|
|
1136
1140
|
}
|
|
1137
1141
|
find(filters) {
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
+
const defaultedFilters = {
|
|
1143
|
+
exact: true,
|
|
1144
|
+
...filters
|
|
1145
|
+
};
|
|
1146
|
+
return this.getAll().find(query => matchQuery(defaultedFilters, query));
|
|
1142
1147
|
}
|
|
1143
1148
|
findAll(filters = {}) {
|
|
1144
1149
|
const queries = this.getAll();
|
|
@@ -1444,10 +1449,11 @@
|
|
|
1444
1449
|
return this.#mutations;
|
|
1445
1450
|
}
|
|
1446
1451
|
find(filters) {
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1452
|
+
const defaultedFilters = {
|
|
1453
|
+
exact: true,
|
|
1454
|
+
...filters
|
|
1455
|
+
};
|
|
1456
|
+
return this.#mutations.find(mutation => matchMutation(defaultedFilters, mutation));
|
|
1451
1457
|
}
|
|
1452
1458
|
findAll(filters = {}) {
|
|
1453
1459
|
return this.#mutations.filter(mutation => matchMutation(filters, mutation));
|
|
@@ -1473,12 +1479,15 @@
|
|
|
1473
1479
|
function infiniteQueryBehavior() {
|
|
1474
1480
|
return {
|
|
1475
1481
|
onFetch: context => {
|
|
1476
|
-
context.fetchFn = () => {
|
|
1482
|
+
context.fetchFn = async () => {
|
|
1477
1483
|
const options = context.options;
|
|
1478
1484
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction;
|
|
1479
1485
|
const oldPages = context.state.data?.pages || [];
|
|
1480
1486
|
const oldPageParams = context.state.data?.pageParams || [];
|
|
1481
|
-
|
|
1487
|
+
const empty = {
|
|
1488
|
+
pages: [],
|
|
1489
|
+
pageParams: []
|
|
1490
|
+
};
|
|
1482
1491
|
let cancelled = false;
|
|
1483
1492
|
const addSignalProperty = object => {
|
|
1484
1493
|
Object.defineProperty(object, 'signal', {
|
|
@@ -1498,97 +1507,99 @@
|
|
|
1498
1507
|
|
|
1499
1508
|
// Get query function
|
|
1500
1509
|
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
1510
|
|
|
1513
1511
|
// Create function to fetch a page
|
|
1514
|
-
const fetchPage = (
|
|
1512
|
+
const fetchPage = async (data, param, previous) => {
|
|
1515
1513
|
if (cancelled) {
|
|
1516
1514
|
return Promise.reject();
|
|
1517
1515
|
}
|
|
1518
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
1519
|
-
return Promise.resolve(
|
|
1516
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
1517
|
+
return Promise.resolve(data);
|
|
1520
1518
|
}
|
|
1521
1519
|
const queryFnContext = {
|
|
1522
1520
|
queryKey: context.queryKey,
|
|
1523
1521
|
pageParam: param,
|
|
1522
|
+
direction: previous ? 'backward' : 'forward',
|
|
1524
1523
|
meta: context.options.meta
|
|
1525
1524
|
};
|
|
1526
1525
|
addSignalProperty(queryFnContext);
|
|
1527
|
-
const
|
|
1528
|
-
const
|
|
1529
|
-
|
|
1526
|
+
const page = await queryFn(queryFnContext);
|
|
1527
|
+
const {
|
|
1528
|
+
maxPages
|
|
1529
|
+
} = context.options;
|
|
1530
|
+
const addTo = previous ? addToStart : addToEnd;
|
|
1531
|
+
return {
|
|
1532
|
+
pages: addTo(data.pages, page, maxPages),
|
|
1533
|
+
pageParams: addTo(data.pageParams, param, maxPages)
|
|
1534
|
+
};
|
|
1530
1535
|
};
|
|
1531
|
-
let
|
|
1536
|
+
let result;
|
|
1532
1537
|
|
|
1533
1538
|
// Fetch first page?
|
|
1534
1539
|
if (!oldPages.length) {
|
|
1535
|
-
|
|
1540
|
+
result = await fetchPage(empty, options.defaultPageParam);
|
|
1536
1541
|
}
|
|
1537
1542
|
|
|
1538
1543
|
// fetch next / previous page?
|
|
1539
1544
|
else if (direction) {
|
|
1540
1545
|
const previous = direction === 'backward';
|
|
1541
|
-
const
|
|
1542
|
-
|
|
1546
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
|
|
1547
|
+
const oldData = {
|
|
1548
|
+
pages: oldPages,
|
|
1549
|
+
pageParams: oldPageParams
|
|
1550
|
+
};
|
|
1551
|
+
const param = pageParamFn(options, oldData);
|
|
1552
|
+
result = await fetchPage(oldData, param, previous);
|
|
1543
1553
|
}
|
|
1544
1554
|
|
|
1545
1555
|
// Refetch pages
|
|
1546
1556
|
else {
|
|
1547
|
-
newPageParams = [];
|
|
1548
|
-
|
|
1549
1557
|
// Fetch first page
|
|
1550
|
-
|
|
1558
|
+
result = await fetchPage(empty, oldPageParams[0]);
|
|
1551
1559
|
|
|
1552
1560
|
// Fetch remaining pages
|
|
1553
1561
|
for (let i = 1; i < oldPages.length; i++) {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
return fetchPage(pages, param);
|
|
1557
|
-
});
|
|
1562
|
+
const param = getNextPageParam(options, result);
|
|
1563
|
+
result = await fetchPage(result, param);
|
|
1558
1564
|
}
|
|
1559
1565
|
}
|
|
1560
|
-
|
|
1561
|
-
pages,
|
|
1562
|
-
pageParams: newPageParams
|
|
1563
|
-
}));
|
|
1564
|
-
return finalPromise;
|
|
1566
|
+
return result;
|
|
1565
1567
|
};
|
|
1566
1568
|
}
|
|
1567
1569
|
};
|
|
1568
1570
|
}
|
|
1569
|
-
function getNextPageParam(options,
|
|
1570
|
-
|
|
1571
|
+
function getNextPageParam(options, {
|
|
1572
|
+
pages,
|
|
1573
|
+
pageParams
|
|
1574
|
+
}) {
|
|
1575
|
+
const lastIndex = pages.length - 1;
|
|
1576
|
+
return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
|
|
1571
1577
|
}
|
|
1572
|
-
function getPreviousPageParam(options,
|
|
1573
|
-
|
|
1578
|
+
function getPreviousPageParam(options, {
|
|
1579
|
+
pages,
|
|
1580
|
+
pageParams
|
|
1581
|
+
}) {
|
|
1582
|
+
return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
|
|
1574
1583
|
}
|
|
1575
1584
|
|
|
1576
1585
|
/**
|
|
1577
1586
|
* Checks if there is a next page.
|
|
1578
1587
|
*/
|
|
1579
|
-
function hasNextPage(options,
|
|
1580
|
-
if (!
|
|
1581
|
-
return typeof getNextPageParam(options,
|
|
1588
|
+
function hasNextPage(options, data) {
|
|
1589
|
+
if (!data) return false;
|
|
1590
|
+
return typeof getNextPageParam(options, data) !== 'undefined';
|
|
1582
1591
|
}
|
|
1583
1592
|
|
|
1584
1593
|
/**
|
|
1585
1594
|
* Checks if there is a previous page.
|
|
1586
1595
|
*/
|
|
1587
|
-
function hasPreviousPage(options,
|
|
1588
|
-
if (!
|
|
1589
|
-
return typeof getPreviousPageParam(options,
|
|
1596
|
+
function hasPreviousPage(options, data) {
|
|
1597
|
+
if (!data || !options.getPreviousPageParam) return false;
|
|
1598
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined';
|
|
1590
1599
|
}
|
|
1591
1600
|
|
|
1601
|
+
// TYPES
|
|
1602
|
+
|
|
1592
1603
|
// CLASS
|
|
1593
1604
|
|
|
1594
1605
|
class QueryClient {
|
|
@@ -1711,10 +1722,11 @@
|
|
|
1711
1722
|
});
|
|
1712
1723
|
}
|
|
1713
1724
|
cancelQueries(filters = {}, cancelOptions = {}) {
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1725
|
+
const defaultedCancelOptions = {
|
|
1726
|
+
revert: true,
|
|
1727
|
+
...cancelOptions
|
|
1728
|
+
};
|
|
1729
|
+
const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map(query => query.cancel(defaultedCancelOptions)));
|
|
1718
1730
|
return Promise.all(promises).then(noop$1).catch(noop$1);
|
|
1719
1731
|
}
|
|
1720
1732
|
invalidateQueries(filters = {}, options = {}) {
|
|
@@ -1733,15 +1745,18 @@
|
|
|
1733
1745
|
});
|
|
1734
1746
|
}
|
|
1735
1747
|
refetchQueries(filters = {}, options) {
|
|
1736
|
-
const
|
|
1748
|
+
const fetchOptions = {
|
|
1737
1749
|
...options,
|
|
1738
1750
|
cancelRefetch: options?.cancelRefetch ?? true
|
|
1739
|
-
}
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1751
|
+
};
|
|
1752
|
+
const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
|
|
1753
|
+
let promise = query.fetch(undefined, fetchOptions);
|
|
1754
|
+
if (!fetchOptions.throwOnError) {
|
|
1755
|
+
promise = promise.catch(noop$1);
|
|
1756
|
+
}
|
|
1757
|
+
return query.state.fetchStatus === 'paused' ? Promise.resolve() : promise;
|
|
1758
|
+
}));
|
|
1759
|
+
return Promise.all(promises).then(noop$1);
|
|
1745
1760
|
}
|
|
1746
1761
|
fetchQuery(options) {
|
|
1747
1762
|
const defaultedOptions = this.defaultQueryOptions(options);
|
|
@@ -1834,8 +1849,8 @@
|
|
|
1834
1849
|
if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
|
|
1835
1850
|
defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
|
|
1836
1851
|
}
|
|
1837
|
-
if (typeof defaultedOptions.
|
|
1838
|
-
defaultedOptions.
|
|
1852
|
+
if (typeof defaultedOptions.throwOnError === 'undefined') {
|
|
1853
|
+
defaultedOptions.throwOnError = !!defaultedOptions.suspense;
|
|
1839
1854
|
}
|
|
1840
1855
|
return defaultedOptions;
|
|
1841
1856
|
}
|
|
@@ -1863,10 +1878,12 @@
|
|
|
1863
1878
|
#currentResult = undefined;
|
|
1864
1879
|
#currentResultState;
|
|
1865
1880
|
#currentResultOptions;
|
|
1866
|
-
#previousQueryResult;
|
|
1867
1881
|
#selectError;
|
|
1868
1882
|
#selectFn;
|
|
1869
1883
|
#selectResult;
|
|
1884
|
+
// This property keeps track of the last query with defined data.
|
|
1885
|
+
// It will be used to pass the previous data and query to the placeholder function between renders.
|
|
1886
|
+
#lastQueryWithDefinedData;
|
|
1870
1887
|
#staleTimeoutId;
|
|
1871
1888
|
#refetchIntervalId;
|
|
1872
1889
|
#currentRefetchInterval;
|
|
@@ -1883,7 +1900,7 @@
|
|
|
1883
1900
|
this.refetch = this.refetch.bind(this);
|
|
1884
1901
|
}
|
|
1885
1902
|
onSubscribe() {
|
|
1886
|
-
if (this.listeners.
|
|
1903
|
+
if (this.listeners.size === 1) {
|
|
1887
1904
|
this.#currentQuery.addObserver(this);
|
|
1888
1905
|
if (shouldFetchOnMount(this.#currentQuery, this.options)) {
|
|
1889
1906
|
this.#executeFetch();
|
|
@@ -1892,7 +1909,7 @@
|
|
|
1892
1909
|
}
|
|
1893
1910
|
}
|
|
1894
1911
|
onUnsubscribe() {
|
|
1895
|
-
if (!this.
|
|
1912
|
+
if (!this.hasListeners()) {
|
|
1896
1913
|
this.destroy();
|
|
1897
1914
|
}
|
|
1898
1915
|
}
|
|
@@ -1903,7 +1920,7 @@
|
|
|
1903
1920
|
return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
1904
1921
|
}
|
|
1905
1922
|
destroy() {
|
|
1906
|
-
this.listeners =
|
|
1923
|
+
this.listeners = new Set();
|
|
1907
1924
|
this.#clearStaleTimeout();
|
|
1908
1925
|
this.#clearRefetchInterval();
|
|
1909
1926
|
this.#currentQuery.removeObserver(this);
|
|
@@ -2023,7 +2040,7 @@
|
|
|
2023
2040
|
}, timeout);
|
|
2024
2041
|
}
|
|
2025
2042
|
#computeRefetchInterval() {
|
|
2026
|
-
return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval ?? false;
|
|
2043
|
+
return (typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval) ?? false;
|
|
2027
2044
|
}
|
|
2028
2045
|
#updateRefetchInterval(nextInterval) {
|
|
2029
2046
|
this.#clearRefetchInterval();
|
|
@@ -2061,7 +2078,6 @@
|
|
|
2061
2078
|
const prevResultOptions = this.#currentResultOptions;
|
|
2062
2079
|
const queryChange = query !== prevQuery;
|
|
2063
2080
|
const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
|
|
2064
|
-
const prevQueryResult = queryChange ? this.#currentResult : this.#previousQueryResult;
|
|
2065
2081
|
const {
|
|
2066
2082
|
state
|
|
2067
2083
|
} = query;
|
|
@@ -2120,7 +2136,7 @@
|
|
|
2120
2136
|
if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
|
|
2121
2137
|
placeholderData = prevResult.data;
|
|
2122
2138
|
} else {
|
|
2123
|
-
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(
|
|
2139
|
+
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
|
|
2124
2140
|
if (options.select && typeof placeholderData !== 'undefined') {
|
|
2125
2141
|
try {
|
|
2126
2142
|
placeholderData = options.select(placeholderData);
|
|
@@ -2184,6 +2200,9 @@
|
|
|
2184
2200
|
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
2185
2201
|
return;
|
|
2186
2202
|
}
|
|
2203
|
+
if (this.#currentResultState.data !== undefined) {
|
|
2204
|
+
this.#lastQueryWithDefinedData = this.#currentQuery;
|
|
2205
|
+
}
|
|
2187
2206
|
this.#currentResult = nextResult;
|
|
2188
2207
|
|
|
2189
2208
|
// Determine which callbacks to trigger
|
|
@@ -2199,7 +2218,7 @@
|
|
|
2199
2218
|
return true;
|
|
2200
2219
|
}
|
|
2201
2220
|
const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
|
|
2202
|
-
if (this.options.
|
|
2221
|
+
if (this.options.throwOnError) {
|
|
2203
2222
|
includedProps.add('error');
|
|
2204
2223
|
}
|
|
2205
2224
|
return Object.keys(this.#currentResult).some(key => {
|
|
@@ -2224,36 +2243,20 @@
|
|
|
2224
2243
|
const prevQuery = this.#currentQuery;
|
|
2225
2244
|
this.#currentQuery = query;
|
|
2226
2245
|
this.#currentQueryInitialState = query.state;
|
|
2227
|
-
this.#previousQueryResult = this.#currentResult;
|
|
2228
2246
|
if (this.hasListeners()) {
|
|
2229
2247
|
prevQuery?.removeObserver(this);
|
|
2230
2248
|
query.addObserver(this);
|
|
2231
2249
|
}
|
|
2232
2250
|
}
|
|
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);
|
|
2251
|
+
onQueryUpdate() {
|
|
2252
|
+
this.#updateResult();
|
|
2241
2253
|
if (this.hasListeners()) {
|
|
2242
2254
|
this.#updateTimers();
|
|
2243
2255
|
}
|
|
2244
2256
|
}
|
|
2245
2257
|
#notify(notifyOptions) {
|
|
2246
2258
|
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
|
|
2259
|
+
// First, trigger the listeners
|
|
2257
2260
|
if (notifyOptions.listeners) {
|
|
2258
2261
|
this.listeners.forEach(listener => {
|
|
2259
2262
|
listener(this.#currentResult);
|
|
@@ -2301,18 +2304,22 @@
|
|
|
2301
2304
|
#result;
|
|
2302
2305
|
#queries;
|
|
2303
2306
|
#observers;
|
|
2304
|
-
|
|
2307
|
+
#options;
|
|
2308
|
+
#combinedResult;
|
|
2309
|
+
constructor(client, queries, options) {
|
|
2305
2310
|
super();
|
|
2306
2311
|
this.#client = client;
|
|
2307
2312
|
this.#queries = [];
|
|
2308
|
-
this.#result = [];
|
|
2309
2313
|
this.#observers = [];
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2314
|
+
this.#setResult([]);
|
|
2315
|
+
this.setQueries(queries, options);
|
|
2316
|
+
}
|
|
2317
|
+
#setResult(value) {
|
|
2318
|
+
this.#result = value;
|
|
2319
|
+
this.#combinedResult = this.#combineResult(value);
|
|
2313
2320
|
}
|
|
2314
2321
|
onSubscribe() {
|
|
2315
|
-
if (this.listeners.
|
|
2322
|
+
if (this.listeners.size === 1) {
|
|
2316
2323
|
this.#observers.forEach(observer => {
|
|
2317
2324
|
observer.subscribe(result => {
|
|
2318
2325
|
this.#onUpdate(observer, result);
|
|
@@ -2321,18 +2328,19 @@
|
|
|
2321
2328
|
}
|
|
2322
2329
|
}
|
|
2323
2330
|
onUnsubscribe() {
|
|
2324
|
-
if (!this.listeners.
|
|
2331
|
+
if (!this.listeners.size) {
|
|
2325
2332
|
this.destroy();
|
|
2326
2333
|
}
|
|
2327
2334
|
}
|
|
2328
2335
|
destroy() {
|
|
2329
|
-
this.listeners =
|
|
2336
|
+
this.listeners = new Set();
|
|
2330
2337
|
this.#observers.forEach(observer => {
|
|
2331
2338
|
observer.destroy();
|
|
2332
2339
|
});
|
|
2333
2340
|
}
|
|
2334
|
-
setQueries(queries, notifyOptions) {
|
|
2341
|
+
setQueries(queries, options, notifyOptions) {
|
|
2335
2342
|
this.#queries = queries;
|
|
2343
|
+
this.#options = options;
|
|
2336
2344
|
notifyManager.batch(() => {
|
|
2337
2345
|
const prevObservers = this.#observers;
|
|
2338
2346
|
const newObserverMatches = this.#findMatchingObservers(this.#queries);
|
|
@@ -2346,7 +2354,7 @@
|
|
|
2346
2354
|
return;
|
|
2347
2355
|
}
|
|
2348
2356
|
this.#observers = newObservers;
|
|
2349
|
-
this.#
|
|
2357
|
+
this.#setResult(newResult);
|
|
2350
2358
|
if (!this.hasListeners()) {
|
|
2351
2359
|
return;
|
|
2352
2360
|
}
|
|
@@ -2362,7 +2370,7 @@
|
|
|
2362
2370
|
});
|
|
2363
2371
|
}
|
|
2364
2372
|
getCurrentResult() {
|
|
2365
|
-
return this.#
|
|
2373
|
+
return this.#combinedResult;
|
|
2366
2374
|
}
|
|
2367
2375
|
getQueries() {
|
|
2368
2376
|
return this.#observers.map(observer => observer.getCurrentQuery());
|
|
@@ -2371,13 +2379,30 @@
|
|
|
2371
2379
|
return this.#observers;
|
|
2372
2380
|
}
|
|
2373
2381
|
getOptimisticResult(queries) {
|
|
2374
|
-
|
|
2382
|
+
const matches = this.#findMatchingObservers(queries);
|
|
2383
|
+
const result = matches.map(match => match.observer.getOptimisticResult(match.defaultedQueryOptions));
|
|
2384
|
+
return [result, r => {
|
|
2385
|
+
return this.#combineResult(r ?? result);
|
|
2386
|
+
}, () => {
|
|
2387
|
+
return matches.map((match, index) => {
|
|
2388
|
+
const observerResult = result[index];
|
|
2389
|
+
return !match.defaultedQueryOptions.notifyOnChangeProps ? match.observer.trackResult(observerResult) : observerResult;
|
|
2390
|
+
});
|
|
2391
|
+
}];
|
|
2392
|
+
}
|
|
2393
|
+
#combineResult(input) {
|
|
2394
|
+
const combine = this.#options?.combine;
|
|
2395
|
+
if (combine) {
|
|
2396
|
+
return replaceEqualDeep(this.#combinedResult, combine(input));
|
|
2397
|
+
}
|
|
2398
|
+
return input;
|
|
2375
2399
|
}
|
|
2376
2400
|
#findMatchingObservers(queries) {
|
|
2377
2401
|
const prevObservers = this.#observers;
|
|
2402
|
+
const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
|
|
2378
2403
|
const defaultedQueryOptions = queries.map(options => this.#client.defaultQueryOptions(options));
|
|
2379
2404
|
const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
|
|
2380
|
-
const match =
|
|
2405
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash);
|
|
2381
2406
|
if (match != null) {
|
|
2382
2407
|
return [{
|
|
2383
2408
|
defaultedQueryOptions: defaultedOptions,
|
|
@@ -2386,8 +2411,8 @@
|
|
|
2386
2411
|
}
|
|
2387
2412
|
return [];
|
|
2388
2413
|
});
|
|
2389
|
-
const matchedQueryHashes = matchingObservers.map(match => match.defaultedQueryOptions.queryHash);
|
|
2390
|
-
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.
|
|
2414
|
+
const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
|
|
2415
|
+
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
|
|
2391
2416
|
const getObserver = options => {
|
|
2392
2417
|
const defaultedOptions = this.#client.defaultQueryOptions(options);
|
|
2393
2418
|
const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
|
|
@@ -2405,7 +2430,7 @@
|
|
|
2405
2430
|
#onUpdate(observer, result) {
|
|
2406
2431
|
const index = this.#observers.indexOf(observer);
|
|
2407
2432
|
if (index !== -1) {
|
|
2408
|
-
this.#
|
|
2433
|
+
this.#setResult(replaceAt(this.#result, index, result));
|
|
2409
2434
|
this.#notify();
|
|
2410
2435
|
}
|
|
2411
2436
|
}
|
|
@@ -2444,7 +2469,7 @@
|
|
|
2444
2469
|
options.behavior = infiniteQueryBehavior();
|
|
2445
2470
|
return super.getOptimisticResult(options);
|
|
2446
2471
|
}
|
|
2447
|
-
fetchNextPage(options
|
|
2472
|
+
fetchNextPage(options) {
|
|
2448
2473
|
return this.fetch({
|
|
2449
2474
|
...options,
|
|
2450
2475
|
meta: {
|
|
@@ -2454,9 +2479,7 @@
|
|
|
2454
2479
|
}
|
|
2455
2480
|
});
|
|
2456
2481
|
}
|
|
2457
|
-
fetchPreviousPage({
|
|
2458
|
-
...options
|
|
2459
|
-
} = {}) {
|
|
2482
|
+
fetchPreviousPage(options) {
|
|
2460
2483
|
return this.fetch({
|
|
2461
2484
|
...options,
|
|
2462
2485
|
meta: {
|
|
@@ -2481,8 +2504,8 @@
|
|
|
2481
2504
|
...result,
|
|
2482
2505
|
fetchNextPage: this.fetchNextPage,
|
|
2483
2506
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
2484
|
-
hasNextPage: hasNextPage(options, state.data
|
|
2485
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
2507
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
2508
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
2486
2509
|
isFetchingNextPage,
|
|
2487
2510
|
isFetchingPreviousPage,
|
|
2488
2511
|
isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
|
|
@@ -2523,7 +2546,7 @@
|
|
|
2523
2546
|
this.#currentMutation?.setOptions(this.options);
|
|
2524
2547
|
}
|
|
2525
2548
|
onUnsubscribe() {
|
|
2526
|
-
if (!this.
|
|
2549
|
+
if (!this.hasListeners()) {
|
|
2527
2550
|
this.#currentMutation?.removeObserver(this);
|
|
2528
2551
|
}
|
|
2529
2552
|
}
|
|
@@ -2608,24 +2631,10 @@
|
|
|
2608
2631
|
return query.state.status === 'success';
|
|
2609
2632
|
}
|
|
2610
2633
|
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
|
-
}
|
|
2634
|
+
const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
|
|
2635
|
+
const mutations = client.getMutationCache().getAll().flatMap(mutation => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []);
|
|
2636
|
+
const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
|
|
2637
|
+
const queries = client.getQueryCache().getAll().flatMap(query => filterQuery(query) ? [dehydrateQuery(query)] : []);
|
|
2629
2638
|
return {
|
|
2630
2639
|
mutations,
|
|
2631
2640
|
queries
|
|
@@ -2747,7 +2756,7 @@
|
|
|
2747
2756
|
}
|
|
2748
2757
|
|
|
2749
2758
|
const ensurePreventErrorBoundaryRetry = (options, errorResetBoundary) => {
|
|
2750
|
-
if (options.suspense || options.
|
|
2759
|
+
if (options.suspense || options.throwOnError) {
|
|
2751
2760
|
// Prevent retrying failed query if the error boundary has not been reset yet
|
|
2752
2761
|
if (!errorResetBoundary.isReset()) {
|
|
2753
2762
|
options.retryOnMount = false;
|
|
@@ -2762,10 +2771,10 @@
|
|
|
2762
2771
|
const getHasError = ({
|
|
2763
2772
|
result,
|
|
2764
2773
|
errorResetBoundary,
|
|
2765
|
-
|
|
2774
|
+
throwOnError,
|
|
2766
2775
|
query
|
|
2767
2776
|
}) => {
|
|
2768
|
-
return result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(
|
|
2777
|
+
return result.isError && !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError(throwOnError, [result.error, query]);
|
|
2769
2778
|
};
|
|
2770
2779
|
|
|
2771
2780
|
const ensureStaleTime = defaultedOptions => {
|
|
@@ -2779,57 +2788,57 @@
|
|
|
2779
2788
|
};
|
|
2780
2789
|
const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
|
|
2781
2790
|
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 => {
|
|
2791
|
+
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).catch(() => {
|
|
2788
2792
|
errorResetBoundary.clearReset();
|
|
2789
|
-
defaultedOptions.onError?.(error);
|
|
2790
|
-
defaultedOptions.onSettled?.(undefined, error);
|
|
2791
2793
|
});
|
|
2792
2794
|
|
|
2793
2795
|
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
2794
2796
|
// `placeholderData` function does not have a parameter
|
|
2795
|
-
|
|
2797
|
+
// Avoid TS depth-limit error in case of large array literal
|
|
2798
|
+
/**
|
|
2799
|
+
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
2800
|
+
*/
|
|
2801
|
+
/**
|
|
2802
|
+
* QueriesResults reducer recursively maps type param to results
|
|
2803
|
+
*/
|
|
2796
2804
|
function useQueries({
|
|
2797
|
-
queries
|
|
2805
|
+
queries,
|
|
2806
|
+
...options
|
|
2798
2807
|
}, queryClient) {
|
|
2799
2808
|
const client = useQueryClient(queryClient);
|
|
2800
2809
|
const isRestoring = useIsRestoring();
|
|
2801
|
-
const
|
|
2802
|
-
|
|
2810
|
+
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
2811
|
+
const defaultedQueries = React__namespace.useMemo(() => queries.map(opts => {
|
|
2812
|
+
const defaultedOptions = client.defaultQueryOptions(opts);
|
|
2803
2813
|
|
|
2804
2814
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
2805
2815
|
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
|
|
2806
2816
|
return defaultedOptions;
|
|
2807
2817
|
}), [queries, client, isRestoring]);
|
|
2808
|
-
|
|
2809
|
-
|
|
2818
|
+
defaultedQueries.forEach(query => {
|
|
2819
|
+
ensureStaleTime(query);
|
|
2820
|
+
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
|
2821
|
+
});
|
|
2822
|
+
useClearResetErrorBoundary(errorResetBoundary);
|
|
2823
|
+
const [observer] = React__namespace.useState(() => new QueriesObserver(client, defaultedQueries, options));
|
|
2824
|
+
const [optimisticResult, getCombinedResult, trackResult] = observer.getOptimisticResult(defaultedQueries);
|
|
2810
2825
|
React__namespace.useSyncExternalStore(React__namespace.useCallback(onStoreChange => isRestoring ? () => undefined : observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
2811
2826
|
React__namespace.useEffect(() => {
|
|
2812
2827
|
// Do not notify on updates because of changes in the options because
|
|
2813
2828
|
// these changes should already be reflected in the optimistic result.
|
|
2814
|
-
observer.setQueries(defaultedQueries, {
|
|
2829
|
+
observer.setQueries(defaultedQueries, options, {
|
|
2815
2830
|
listeners: false
|
|
2816
2831
|
});
|
|
2817
|
-
}, [defaultedQueries, observer]);
|
|
2818
|
-
const errorResetBoundary = useQueryErrorResetBoundary();
|
|
2819
|
-
defaultedQueries.forEach(query => {
|
|
2820
|
-
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
|
2821
|
-
ensureStaleTime(query);
|
|
2822
|
-
});
|
|
2823
|
-
useClearResetErrorBoundary(errorResetBoundary);
|
|
2832
|
+
}, [defaultedQueries, options, observer]);
|
|
2824
2833
|
const shouldAtLeastOneSuspend = optimisticResult.some((result, index) => shouldSuspend(defaultedQueries[index], result, isRestoring));
|
|
2825
2834
|
const suspensePromises = shouldAtLeastOneSuspend ? optimisticResult.flatMap((result, index) => {
|
|
2826
|
-
const
|
|
2835
|
+
const opts = defaultedQueries[index];
|
|
2827
2836
|
const queryObserver = observer.getObservers()[index];
|
|
2828
|
-
if (
|
|
2829
|
-
if (shouldSuspend(
|
|
2830
|
-
return fetchOptimistic(
|
|
2837
|
+
if (opts && queryObserver) {
|
|
2838
|
+
if (shouldSuspend(opts, result, isRestoring)) {
|
|
2839
|
+
return fetchOptimistic(opts, queryObserver, errorResetBoundary);
|
|
2831
2840
|
} else if (willFetch(result, isRestoring)) {
|
|
2832
|
-
void fetchOptimistic(
|
|
2841
|
+
void fetchOptimistic(opts, queryObserver, errorResetBoundary);
|
|
2833
2842
|
}
|
|
2834
2843
|
}
|
|
2835
2844
|
return [];
|
|
@@ -2837,16 +2846,17 @@
|
|
|
2837
2846
|
if (suspensePromises.length > 0) {
|
|
2838
2847
|
throw Promise.all(suspensePromises);
|
|
2839
2848
|
}
|
|
2849
|
+
const observerQueries = observer.getQueries();
|
|
2840
2850
|
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => getHasError({
|
|
2841
2851
|
result,
|
|
2842
2852
|
errorResetBoundary,
|
|
2843
|
-
|
|
2844
|
-
query:
|
|
2853
|
+
throwOnError: defaultedQueries[index]?.throwOnError ?? false,
|
|
2854
|
+
query: observerQueries[index]
|
|
2845
2855
|
}));
|
|
2846
2856
|
if (firstSingleResultWhichShouldThrow?.error) {
|
|
2847
2857
|
throw firstSingleResultWhichShouldThrow.error;
|
|
2848
2858
|
}
|
|
2849
|
-
return
|
|
2859
|
+
return getCombinedResult(trackResult());
|
|
2850
2860
|
}
|
|
2851
2861
|
|
|
2852
2862
|
function useBaseQuery(options, Observer, queryClient) {
|
|
@@ -2857,17 +2867,6 @@
|
|
|
2857
2867
|
|
|
2858
2868
|
// Make sure results are optimistically set in fetching state before subscribing or updating options
|
|
2859
2869
|
defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
|
|
2860
|
-
|
|
2861
|
-
// Include callbacks in batch renders
|
|
2862
|
-
if (defaultedOptions.onError) {
|
|
2863
|
-
defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
|
|
2864
|
-
}
|
|
2865
|
-
if (defaultedOptions.onSuccess) {
|
|
2866
|
-
defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
|
|
2867
|
-
}
|
|
2868
|
-
if (defaultedOptions.onSettled) {
|
|
2869
|
-
defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
|
|
2870
|
-
}
|
|
2871
2870
|
ensureStaleTime(defaultedOptions);
|
|
2872
2871
|
ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary);
|
|
2873
2872
|
useClearResetErrorBoundary(errorResetBoundary);
|
|
@@ -2891,7 +2890,7 @@
|
|
|
2891
2890
|
if (getHasError({
|
|
2892
2891
|
result,
|
|
2893
2892
|
errorResetBoundary,
|
|
2894
|
-
|
|
2893
|
+
throwOnError: defaultedOptions.throwOnError,
|
|
2895
2894
|
query: observer.getCurrentQuery()
|
|
2896
2895
|
})) {
|
|
2897
2896
|
throw result.error;
|
|
@@ -2901,6 +2900,10 @@
|
|
|
2901
2900
|
return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result;
|
|
2902
2901
|
}
|
|
2903
2902
|
|
|
2903
|
+
function queryOptions(options) {
|
|
2904
|
+
return options;
|
|
2905
|
+
}
|
|
2906
|
+
|
|
2904
2907
|
// HOOK
|
|
2905
2908
|
|
|
2906
2909
|
function useQuery(options, queryClient) {
|
|
@@ -2978,7 +2981,7 @@
|
|
|
2978
2981
|
const mutate = React__namespace.useCallback((variables, mutateOptions) => {
|
|
2979
2982
|
observer.mutate(variables, mutateOptions).catch(noop);
|
|
2980
2983
|
}, [observer]);
|
|
2981
|
-
if (result.error && shouldThrowError(observer.options.
|
|
2984
|
+
if (result.error && shouldThrowError(observer.options.throwOnError, [result.error])) {
|
|
2982
2985
|
throw result.error;
|
|
2983
2986
|
}
|
|
2984
2987
|
return {
|
|
@@ -3023,6 +3026,7 @@
|
|
|
3023
3026
|
exports.matchQuery = matchQuery;
|
|
3024
3027
|
exports.notifyManager = notifyManager;
|
|
3025
3028
|
exports.onlineManager = onlineManager;
|
|
3029
|
+
exports.queryOptions = queryOptions;
|
|
3026
3030
|
exports.replaceEqualDeep = replaceEqualDeep;
|
|
3027
3031
|
exports.useInfiniteQuery = useInfiniteQuery;
|
|
3028
3032
|
exports.useIsFetching = useIsFetching;
|