@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.6
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/lib/__tests__/ssr.test.d.ts +0 -3
- package/build/lib/__tests__/utils.d.ts +2 -3
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -5
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/useQueries.esm.js +10 -7
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +10 -7
- package/build/lib/useQueries.js.map +1 -1
- package/build/umd/index.development.js +52 -51
- 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 +3 -3
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +12 -11
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +14 -13
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +110 -2
- package/src/__tests__/useMutation.test.tsx +21 -20
- package/src/__tests__/useMutationState.test.tsx +1 -58
- package/src/__tests__/useQueries.test.tsx +13 -73
- package/src/__tests__/useQuery.test.tsx +30 -36
- package/src/__tests__/utils.tsx +3 -2
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
1
|
/// <reference types="node" />
|
|
3
2
|
import * as React from 'react';
|
|
4
3
|
import { render } from '@testing-library/react';
|
|
@@ -10,8 +9,8 @@ export declare const Blink: ({ duration, children, }: {
|
|
|
10
9
|
children: React.ReactNode;
|
|
11
10
|
}) => JSX.Element;
|
|
12
11
|
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
13
|
-
export declare function mockVisibilityState(value: DocumentVisibilityState):
|
|
14
|
-
export declare function mockNavigatorOnLine(value: boolean):
|
|
12
|
+
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
13
|
+
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
15
14
|
export declare function queryKey(): Array<string>;
|
|
16
15
|
export declare function sleep(timeout: number): Promise<void>;
|
|
17
16
|
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
@@ -8,16 +8,16 @@ const ensureStaleTime = defaultedOptions => {
|
|
|
8
8
|
}
|
|
9
9
|
};
|
|
10
10
|
const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
|
|
11
|
-
const shouldSuspend = (defaultedOptions, result, isRestoring) => defaultedOptions
|
|
11
|
+
const shouldSuspend = (defaultedOptions, result, isRestoring) => (defaultedOptions == null ? void 0 : defaultedOptions.suspense) && willFetch(result, isRestoring);
|
|
12
12
|
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).then(({
|
|
13
13
|
data
|
|
14
14
|
}) => {
|
|
15
|
-
defaultedOptions.onSuccess
|
|
16
|
-
defaultedOptions.onSettled
|
|
15
|
+
defaultedOptions.onSuccess == null ? void 0 : defaultedOptions.onSuccess(data);
|
|
16
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(data, null);
|
|
17
17
|
}).catch(error => {
|
|
18
18
|
errorResetBoundary.clearReset();
|
|
19
|
-
defaultedOptions.onError
|
|
20
|
-
defaultedOptions.onSettled
|
|
19
|
+
defaultedOptions.onError == null ? void 0 : defaultedOptions.onError(error);
|
|
20
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(undefined, error);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
export { ensureStaleTime, fetchOptimistic, shouldSuspend, willFetch };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense.esm.js","sources":["../../src/suspense.ts"],"sourcesContent":["import type { DefaultedQueryObserverOptions } from '@tanstack/query-core'\nimport type { QueryObserver } from '@tanstack/query-core'\nimport type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'\nimport type { QueryObserverResult } from '@tanstack/query-core'\nimport type { QueryKey } from '@tanstack/query-core'\n\nexport const ensureStaleTime = (\n defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,\n) => {\n if (defaultedOptions.suspense) {\n // Always set stale time when using suspense to prevent\n // fetching again when directly mounting after suspending\n if (typeof defaultedOptions.staleTime !== 'number') {\n defaultedOptions.staleTime = 1000\n }\n }\n}\n\nexport const willFetch = (\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => result.isLoading && result.isFetching && !isRestoring\n\nexport const shouldSuspend = (\n defaultedOptions:\n | DefaultedQueryObserverOptions<any, any, any, any, any>\n | undefined,\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => defaultedOptions?.suspense && willFetch(result, isRestoring)\n\nexport const fetchOptimistic = <\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n defaultedOptions: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n errorResetBoundary: QueryErrorResetBoundaryValue,\n) =>\n observer\n .fetchOptimistic(defaultedOptions)\n .then(({ data }) => {\n defaultedOptions.onSuccess?.(data as TData)\n defaultedOptions.onSettled?.(data, null)\n })\n .catch((error) => {\n errorResetBoundary.clearReset()\n defaultedOptions.onError?.(error)\n defaultedOptions.onSettled?.(undefined, error)\n })\n"],"names":["ensureStaleTime","defaultedOptions","suspense","staleTime","willFetch","result","isRestoring","isLoading","isFetching","shouldSuspend","fetchOptimistic","observer","errorResetBoundary","then","data","onSuccess","onSettled","catch","error","clearReset","onError","undefined"],"mappings":"AAMaA,MAAAA,eAAe,GAC1BC,gBAAwE,IACrE;EACH,IAAIA,gBAAgB,CAACC,QAAQ,EAAE;AAC7B;AACA;AACA,IAAA,IAAI,OAAOD,gBAAgB,CAACE,SAAS,KAAK,QAAQ,EAAE;MAClDF,gBAAgB,CAACE,SAAS,GAAG,IAAI,CAAA;AACnC,KAAA;AACF,GAAA;AACF,EAAC;MAEYC,SAAS,GAAG,CACvBC,MAAqC,EACrCC,WAAoB,KACjBD,MAAM,CAACE,SAAS,IAAIF,MAAM,CAACG,UAAU,IAAI,CAACF,YAAW;
|
|
1
|
+
{"version":3,"file":"suspense.esm.js","sources":["../../src/suspense.ts"],"sourcesContent":["import type { DefaultedQueryObserverOptions } from '@tanstack/query-core'\nimport type { QueryObserver } from '@tanstack/query-core'\nimport type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'\nimport type { QueryObserverResult } from '@tanstack/query-core'\nimport type { QueryKey } from '@tanstack/query-core'\n\nexport const ensureStaleTime = (\n defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,\n) => {\n if (defaultedOptions.suspense) {\n // Always set stale time when using suspense to prevent\n // fetching again when directly mounting after suspending\n if (typeof defaultedOptions.staleTime !== 'number') {\n defaultedOptions.staleTime = 1000\n }\n }\n}\n\nexport const willFetch = (\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => result.isLoading && result.isFetching && !isRestoring\n\nexport const shouldSuspend = (\n defaultedOptions:\n | DefaultedQueryObserverOptions<any, any, any, any, any>\n | undefined,\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => defaultedOptions?.suspense && willFetch(result, isRestoring)\n\nexport const fetchOptimistic = <\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n defaultedOptions: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n errorResetBoundary: QueryErrorResetBoundaryValue,\n) =>\n observer\n .fetchOptimistic(defaultedOptions)\n .then(({ data }) => {\n defaultedOptions.onSuccess?.(data as TData)\n defaultedOptions.onSettled?.(data, null)\n })\n .catch((error) => {\n errorResetBoundary.clearReset()\n defaultedOptions.onError?.(error)\n defaultedOptions.onSettled?.(undefined, error)\n })\n"],"names":["ensureStaleTime","defaultedOptions","suspense","staleTime","willFetch","result","isRestoring","isLoading","isFetching","shouldSuspend","fetchOptimistic","observer","errorResetBoundary","then","data","onSuccess","onSettled","catch","error","clearReset","onError","undefined"],"mappings":"AAMaA,MAAAA,eAAe,GAC1BC,gBAAwE,IACrE;EACH,IAAIA,gBAAgB,CAACC,QAAQ,EAAE;AAC7B;AACA;AACA,IAAA,IAAI,OAAOD,gBAAgB,CAACE,SAAS,KAAK,QAAQ,EAAE;MAClDF,gBAAgB,CAACE,SAAS,GAAG,IAAI,CAAA;AACnC,KAAA;AACF,GAAA;AACF,EAAC;MAEYC,SAAS,GAAG,CACvBC,MAAqC,EACrCC,WAAoB,KACjBD,MAAM,CAACE,SAAS,IAAIF,MAAM,CAACG,UAAU,IAAI,CAACF,YAAW;AAEnD,MAAMG,aAAa,GAAG,CAC3BR,gBAEa,EACbI,MAAqC,EACrCC,WAAoB,KACjB,CAAAL,gBAAgB,IAAhBA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAAEC,QAAQ,KAAIE,SAAS,CAACC,MAAM,EAAEC,WAAW,EAAC;MAEpDI,eAAe,GAAG,CAO7BT,gBAMC,EACDU,QAA2E,EAC3EC,kBAAgD,KAEhDD,QAAQ,CACLD,eAAe,CAACT,gBAAgB,CAAC,CACjCY,IAAI,CAAC,CAAC;AAAEC,EAAAA,IAAAA;AAAK,CAAC,KAAK;EAClBb,gBAAgB,CAACc,SAAS,IAA1Bd,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAACc,SAAS,CAAGD,IAAI,CAAU,CAAA;EAC3Cb,gBAAgB,CAACe,SAAS,IAAA,IAAA,GAAA,KAAA,CAAA,GAA1Bf,gBAAgB,CAACe,SAAS,CAAGF,IAAI,EAAE,IAAI,CAAC,CAAA;AAC1C,CAAC,CAAC,CACDG,KAAK,CAAEC,KAAK,IAAK;EAChBN,kBAAkB,CAACO,UAAU,EAAE,CAAA;EAC/BlB,gBAAgB,CAACmB,OAAO,IAAxBnB,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAACmB,OAAO,CAAGF,KAAK,CAAC,CAAA;EACjCjB,gBAAgB,CAACe,SAAS,IAAA,IAAA,GAAA,KAAA,CAAA,GAA1Bf,gBAAgB,CAACe,SAAS,CAAGK,SAAS,EAAEH,KAAK,CAAC,CAAA;AAChD,CAAC;;;;"}
|
package/build/lib/suspense.js
CHANGED
|
@@ -10,16 +10,16 @@ const ensureStaleTime = defaultedOptions => {
|
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
12
|
const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
|
|
13
|
-
const shouldSuspend = (defaultedOptions, result, isRestoring) => defaultedOptions
|
|
13
|
+
const shouldSuspend = (defaultedOptions, result, isRestoring) => (defaultedOptions == null ? void 0 : defaultedOptions.suspense) && willFetch(result, isRestoring);
|
|
14
14
|
const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).then(({
|
|
15
15
|
data
|
|
16
16
|
}) => {
|
|
17
|
-
defaultedOptions.onSuccess
|
|
18
|
-
defaultedOptions.onSettled
|
|
17
|
+
defaultedOptions.onSuccess == null ? void 0 : defaultedOptions.onSuccess(data);
|
|
18
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(data, null);
|
|
19
19
|
}).catch(error => {
|
|
20
20
|
errorResetBoundary.clearReset();
|
|
21
|
-
defaultedOptions.onError
|
|
22
|
-
defaultedOptions.onSettled
|
|
21
|
+
defaultedOptions.onError == null ? void 0 : defaultedOptions.onError(error);
|
|
22
|
+
defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(undefined, error);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
exports.ensureStaleTime = ensureStaleTime;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense.js","sources":["../../src/suspense.ts"],"sourcesContent":["import type { DefaultedQueryObserverOptions } from '@tanstack/query-core'\nimport type { QueryObserver } from '@tanstack/query-core'\nimport type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'\nimport type { QueryObserverResult } from '@tanstack/query-core'\nimport type { QueryKey } from '@tanstack/query-core'\n\nexport const ensureStaleTime = (\n defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,\n) => {\n if (defaultedOptions.suspense) {\n // Always set stale time when using suspense to prevent\n // fetching again when directly mounting after suspending\n if (typeof defaultedOptions.staleTime !== 'number') {\n defaultedOptions.staleTime = 1000\n }\n }\n}\n\nexport const willFetch = (\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => result.isLoading && result.isFetching && !isRestoring\n\nexport const shouldSuspend = (\n defaultedOptions:\n | DefaultedQueryObserverOptions<any, any, any, any, any>\n | undefined,\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => defaultedOptions?.suspense && willFetch(result, isRestoring)\n\nexport const fetchOptimistic = <\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n defaultedOptions: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n errorResetBoundary: QueryErrorResetBoundaryValue,\n) =>\n observer\n .fetchOptimistic(defaultedOptions)\n .then(({ data }) => {\n defaultedOptions.onSuccess?.(data as TData)\n defaultedOptions.onSettled?.(data, null)\n })\n .catch((error) => {\n errorResetBoundary.clearReset()\n defaultedOptions.onError?.(error)\n defaultedOptions.onSettled?.(undefined, error)\n })\n"],"names":["ensureStaleTime","defaultedOptions","suspense","staleTime","willFetch","result","isRestoring","isLoading","isFetching","shouldSuspend","fetchOptimistic","observer","errorResetBoundary","then","data","onSuccess","onSettled","catch","error","clearReset","onError","undefined"],"mappings":";;AAMaA,MAAAA,eAAe,GAC1BC,gBAAwE,IACrE;EACH,IAAIA,gBAAgB,CAACC,QAAQ,EAAE;AAC7B;AACA;AACA,IAAA,IAAI,OAAOD,gBAAgB,CAACE,SAAS,KAAK,QAAQ,EAAE;MAClDF,gBAAgB,CAACE,SAAS,GAAG,IAAI,CAAA;AACnC,KAAA;AACF,GAAA;AACF,EAAC;MAEYC,SAAS,GAAG,CACvBC,MAAqC,EACrCC,WAAoB,KACjBD,MAAM,CAACE,SAAS,IAAIF,MAAM,CAACG,UAAU,IAAI,CAACF,YAAW;
|
|
1
|
+
{"version":3,"file":"suspense.js","sources":["../../src/suspense.ts"],"sourcesContent":["import type { DefaultedQueryObserverOptions } from '@tanstack/query-core'\nimport type { QueryObserver } from '@tanstack/query-core'\nimport type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'\nimport type { QueryObserverResult } from '@tanstack/query-core'\nimport type { QueryKey } from '@tanstack/query-core'\n\nexport const ensureStaleTime = (\n defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,\n) => {\n if (defaultedOptions.suspense) {\n // Always set stale time when using suspense to prevent\n // fetching again when directly mounting after suspending\n if (typeof defaultedOptions.staleTime !== 'number') {\n defaultedOptions.staleTime = 1000\n }\n }\n}\n\nexport const willFetch = (\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => result.isLoading && result.isFetching && !isRestoring\n\nexport const shouldSuspend = (\n defaultedOptions:\n | DefaultedQueryObserverOptions<any, any, any, any, any>\n | undefined,\n result: QueryObserverResult<any, any>,\n isRestoring: boolean,\n) => defaultedOptions?.suspense && willFetch(result, isRestoring)\n\nexport const fetchOptimistic = <\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n defaultedOptions: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n errorResetBoundary: QueryErrorResetBoundaryValue,\n) =>\n observer\n .fetchOptimistic(defaultedOptions)\n .then(({ data }) => {\n defaultedOptions.onSuccess?.(data as TData)\n defaultedOptions.onSettled?.(data, null)\n })\n .catch((error) => {\n errorResetBoundary.clearReset()\n defaultedOptions.onError?.(error)\n defaultedOptions.onSettled?.(undefined, error)\n })\n"],"names":["ensureStaleTime","defaultedOptions","suspense","staleTime","willFetch","result","isRestoring","isLoading","isFetching","shouldSuspend","fetchOptimistic","observer","errorResetBoundary","then","data","onSuccess","onSettled","catch","error","clearReset","onError","undefined"],"mappings":";;AAMaA,MAAAA,eAAe,GAC1BC,gBAAwE,IACrE;EACH,IAAIA,gBAAgB,CAACC,QAAQ,EAAE;AAC7B;AACA;AACA,IAAA,IAAI,OAAOD,gBAAgB,CAACE,SAAS,KAAK,QAAQ,EAAE;MAClDF,gBAAgB,CAACE,SAAS,GAAG,IAAI,CAAA;AACnC,KAAA;AACF,GAAA;AACF,EAAC;MAEYC,SAAS,GAAG,CACvBC,MAAqC,EACrCC,WAAoB,KACjBD,MAAM,CAACE,SAAS,IAAIF,MAAM,CAACG,UAAU,IAAI,CAACF,YAAW;AAEnD,MAAMG,aAAa,GAAG,CAC3BR,gBAEa,EACbI,MAAqC,EACrCC,WAAoB,KACjB,CAAAL,gBAAgB,IAAhBA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAAEC,QAAQ,KAAIE,SAAS,CAACC,MAAM,EAAEC,WAAW,EAAC;MAEpDI,eAAe,GAAG,CAO7BT,gBAMC,EACDU,QAA2E,EAC3EC,kBAAgD,KAEhDD,QAAQ,CACLD,eAAe,CAACT,gBAAgB,CAAC,CACjCY,IAAI,CAAC,CAAC;AAAEC,EAAAA,IAAAA;AAAK,CAAC,KAAK;EAClBb,gBAAgB,CAACc,SAAS,IAA1Bd,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAACc,SAAS,CAAGD,IAAI,CAAU,CAAA;EAC3Cb,gBAAgB,CAACe,SAAS,IAAA,IAAA,GAAA,KAAA,CAAA,GAA1Bf,gBAAgB,CAACe,SAAS,CAAGF,IAAI,EAAE,IAAI,CAAC,CAAA;AAC1C,CAAC,CAAC,CACDG,KAAK,CAAEC,KAAK,IAAK;EAChBN,kBAAkB,CAACO,UAAU,EAAE,CAAA;EAC/BlB,gBAAgB,CAACmB,OAAO,IAAxBnB,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,gBAAgB,CAACmB,OAAO,CAAGF,KAAK,CAAC,CAAA;EACjCjB,gBAAgB,CAACe,SAAS,IAAA,IAAA,GAAA,KAAA,CAAA,GAA1Bf,gBAAgB,CAACe,SAAS,CAAGK,SAAS,EAAEH,KAAK,CAAC,CAAA;AAChD,CAAC;;;;;;;"}
|
|
@@ -53,13 +53,16 @@ function useQueries({
|
|
|
53
53
|
if (suspensePromises.length > 0) {
|
|
54
54
|
throw Promise.all(suspensePromises);
|
|
55
55
|
}
|
|
56
|
-
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) =>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => {
|
|
57
|
+
var _defaultedQueries$ind, _defaultedQueries$ind2;
|
|
58
|
+
return getHasError({
|
|
59
|
+
result,
|
|
60
|
+
errorResetBoundary,
|
|
61
|
+
throwErrors: (_defaultedQueries$ind = (_defaultedQueries$ind2 = defaultedQueries[index]) == null ? void 0 : _defaultedQueries$ind2.throwErrors) != null ? _defaultedQueries$ind : false,
|
|
62
|
+
query: observer.getQueries()[index]
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
if (firstSingleResultWhichShouldThrow != null && firstSingleResultWhichShouldThrow.error) {
|
|
63
66
|
throw firstSingleResultWhichShouldThrow.error;
|
|
64
67
|
}
|
|
65
68
|
return optimisticResult;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueries.esm.js","sources":["../../src/useQueries.ts"],"sourcesContent":["import * as React from 'react'\n\nimport type {\n QueryKey,\n QueryFunction,\n QueriesPlaceholderDataFunction,\n QueryClient,\n DefaultError,\n} from '@tanstack/query-core'\nimport { notifyManager, QueriesObserver } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type { UseQueryOptions, UseQueryResult } from './types'\nimport { useIsRestoring } from './isRestoring'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureStaleTime,\n shouldSuspend,\n fetchOptimistic,\n willFetch,\n} from './suspense'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = Omit<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\ntype GetOptions<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>\n select: (data: any) => infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TData, TQueryKey>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TQueryFnData, TQueryKey>\n : // Fallback\n UseQueryOptionsForUseQueries\n\ntype GetResults<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? UseQueryResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryResult<TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? UseQueryResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryResult<TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<unknown, any>\n select: (data: any) => infer TData\n }\n ? UseQueryResult<TData>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, any> }\n ? UseQueryResult<TQueryFnData>\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryOptionsForUseQueries[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>\n : unknown[] extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >[]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>[]\n : // Fallback\n UseQueryOptionsForUseQueries[]\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryResult[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>\n : T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >[]\n ? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results\n UseQueryResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >[]\n : // Fallback\n UseQueryResult[]\n\nexport function useQueries<T extends any[]>(\n {\n queries,\n }: {\n queries: readonly [...QueriesOptions<T>]\n },\n queryClient?: QueryClient,\n): QueriesResults<T> {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n\n const defaultedQueries = React.useMemo(\n () =>\n queries.map((options) => {\n const defaultedOptions = client.defaultQueryOptions(options)\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n const [observer] = React.useState(\n () => new QueriesObserver(client, defaultedQueries),\n )\n\n const optimisticResult = observer.getOptimisticResult(defaultedQueries)\n\n React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n isRestoring\n ? () => undefined\n : observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer, isRestoring],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n React.useEffect(() => {\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n observer.setQueries(defaultedQueries, { listeners: false })\n }, [defaultedQueries, observer])\n\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n defaultedQueries.forEach((query) => {\n ensurePreventErrorBoundaryRetry(query, errorResetBoundary)\n ensureStaleTime(query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result, isRestoring),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const options = defaultedQueries[index]\n const queryObserver = observer.getObservers()[index]\n\n if (options && queryObserver) {\n if (shouldSuspend(options, result, isRestoring)) {\n return fetchOptimistic(options, queryObserver, errorResetBoundary)\n } else if (willFetch(result, isRestoring)) {\n void fetchOptimistic(options, queryObserver, errorResetBoundary)\n }\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) =>\n getHasError({\n result,\n errorResetBoundary,\n throwErrors: defaultedQueries[index]?.throwErrors ?? false,\n query: observer.getQueries()[index]!,\n }),\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return optimisticResult as QueriesResults<T>\n}\n"],"names":["useQueries","queries","queryClient","client","useQueryClient","isRestoring","useIsRestoring","defaultedQueries","React","useMemo","map","options","defaultedOptions","defaultQueryOptions","_optimisticResults","observer","useState","QueriesObserver","optimisticResult","getOptimisticResult","useSyncExternalStore","useCallback","onStoreChange","undefined","subscribe","notifyManager","batchCalls","getCurrentResult","useEffect","setQueries","listeners","errorResetBoundary","useQueryErrorResetBoundary","forEach","query","ensurePreventErrorBoundaryRetry","ensureStaleTime","useClearResetErrorBoundary","shouldAtLeastOneSuspend","some","result","index","shouldSuspend","suspensePromises","flatMap","queryObserver","getObservers","fetchOptimistic","willFetch","length","Promise","all","firstSingleResultWhichShouldThrow","find","getHasError","throwErrors","getQueries","error"],"mappings":";;;;;;;;AA0BA;AACA;;AAkIO,SAASA,UAAU,CACxB;AACEC,EAAAA,OAAAA;AAGF,CAAC,EACDC,WAAyB,EACN;AACnB,EAAA,MAAMC,MAAM,GAAGC,cAAc,CAACF,WAAW,CAAC,CAAA;EAC1C,MAAMG,WAAW,GAAGC,cAAc,EAAE,CAAA;AAEpC,EAAA,MAAMC,gBAAgB,GAAGC,KAAK,CAACC,OAAO,CACpC,MACER,OAAO,CAACS,GAAG,CAAEC,OAAO,IAAK;AACvB,IAAA,MAAMC,gBAAgB,GAAGT,MAAM,CAACU,mBAAmB,CAACF,OAAO,CAAC,CAAA;;AAE5D;AACAC,IAAAA,gBAAgB,CAACE,kBAAkB,GAAGT,WAAW,GAC7C,aAAa,GACb,YAAY,CAAA;AAEhB,IAAA,OAAOO,gBAAgB,CAAA;GACxB,CAAC,EACJ,CAACX,OAAO,EAAEE,MAAM,EAAEE,WAAW,CAAC,CAC/B,CAAA;AAED,EAAA,MAAM,CAACU,QAAQ,CAAC,GAAGP,KAAK,CAACQ,QAAQ,CAC/B,MAAM,IAAIC,eAAe,CAACd,MAAM,EAAEI,gBAAgB,CAAC,CACpD,CAAA;AAED,EAAA,MAAMW,gBAAgB,GAAGH,QAAQ,CAACI,mBAAmB,CAACZ,gBAAgB,CAAC,CA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AAY,EAAE,CAACL,KAAK,CAAC,CAAA;IAEpD,IAAI9B,OAAO,IAAIkC,aAAa,EAAE;MAC5B,IAAIH,aAAa,CAAC/B,OAAO,EAAE6B,MAAM,EAAEnC,WAAW,CAAC,EAAE;AAC/C,QAAA,OAAO0C,eAAe,CAACpC,OAAO,EAAEkC,aAAa,EAAEd,kBAAkB,CAAC,CAAA;OACnE,MAAM,IAAIiB,SAAS,CAACR,MAAM,EAAEnC,WAAW,CAAC,EAAE;AACzC,QAAA,KAAK0C,eAAe,CAACpC,OAAO,EAAEkC,aAAa,EAAEd,kBAAkB,CAAC,CAAA;AAClE,OAAA;AACF,KAAA;AACA,IAAA,OAAO,EAAE,CAAA;GACV,CAAC,GACF,EAAE,CAAA;AAEN,EAAA,IAAIY,gBAAgB,CAACM,MAAM,GAAG,CAAC,EAAE;AAC/B,IAAA,MAAMC,OAAO,CAACC,GAAG,CAACR,gBAAgB,CAAC,CAAA;AACrC,GAAA;AAEA,EAAA,MAAMS,iCAAiC,GAAGlC,gBAAgB,CAACmC,IAAI,CAC7D,CAACb,MAAM,EAAEC,KAAK,KACZa,WAAW,CAAC;IACVd,MAAM;IACNT,kBAAkB;IAClBwB,WAAW,EAAEhD,gBAAgB,CAACkC,KAAK,CAAC,EAAEc,WAAW,IAAI,KAAK;AAC1DrB,IAAAA,KAAK,EAAEnB,QAAQ,CAACyC,UAAU,EAAE,CAACf,KAAK,CAAA;AACpC,GAAC,CAAC,CACL,CAAA;EAED,IAAIW,iCAAiC,EAAEK,KAAK,EAAE;IAC5C,MAAML,iCAAiC,CAACK,KAAK,CAAA;AAC/C,GAAA;AAEA,EAAA,OAAOvC,gBAAgB,CAAA;AACzB;;;;"}
|
|
1
|
+
{"version":3,"file":"useQueries.esm.js","sources":["../../src/useQueries.ts"],"sourcesContent":["import * as React from 'react'\n\nimport type {\n QueryKey,\n QueryFunction,\n QueriesPlaceholderDataFunction,\n QueryClient,\n DefaultError,\n} from '@tanstack/query-core'\nimport { notifyManager, QueriesObserver } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type { UseQueryOptions, UseQueryResult } from './types'\nimport { useIsRestoring } from './isRestoring'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureStaleTime,\n shouldSuspend,\n fetchOptimistic,\n willFetch,\n} from './suspense'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = Omit<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\ntype GetOptions<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>\n select: (data: any) => infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TData, TQueryKey>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TQueryFnData, TQueryKey>\n : // Fallback\n UseQueryOptionsForUseQueries\n\ntype GetResults<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? UseQueryResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryResult<TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? UseQueryResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryResult<TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<unknown, any>\n select: (data: any) => infer TData\n }\n ? UseQueryResult<TData>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, any> }\n ? UseQueryResult<TQueryFnData>\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryOptionsForUseQueries[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>\n : unknown[] extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >[]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>[]\n : // Fallback\n UseQueryOptionsForUseQueries[]\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryResult[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>\n : T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >[]\n ? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results\n UseQueryResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >[]\n : // Fallback\n UseQueryResult[]\n\nexport function useQueries<T extends any[]>(\n {\n queries,\n }: {\n queries: readonly [...QueriesOptions<T>]\n },\n queryClient?: QueryClient,\n): QueriesResults<T> {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n\n const defaultedQueries = React.useMemo(\n () =>\n queries.map((options) => {\n const defaultedOptions = client.defaultQueryOptions(options)\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n const [observer] = React.useState(\n () => new QueriesObserver(client, defaultedQueries),\n )\n\n const optimisticResult = observer.getOptimisticResult(defaultedQueries)\n\n React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n isRestoring\n ? () => undefined\n : observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer, isRestoring],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n React.useEffect(() => {\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n observer.setQueries(defaultedQueries, { listeners: false })\n }, [defaultedQueries, observer])\n\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n defaultedQueries.forEach((query) => {\n ensurePreventErrorBoundaryRetry(query, errorResetBoundary)\n ensureStaleTime(query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result, isRestoring),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const options = defaultedQueries[index]\n const queryObserver = observer.getObservers()[index]\n\n if (options && queryObserver) {\n if (shouldSuspend(options, result, isRestoring)) {\n return fetchOptimistic(options, queryObserver, errorResetBoundary)\n } else if (willFetch(result, isRestoring)) {\n void fetchOptimistic(options, queryObserver, errorResetBoundary)\n }\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) =>\n getHasError({\n result,\n errorResetBoundary,\n throwErrors: defaultedQueries[index]?.throwErrors ?? false,\n query: observer.getQueries()[index]!,\n }),\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return optimisticResult as QueriesResults<T>\n}\n"],"names":["useQueries","queries","queryClient","client","useQueryClient","isRestoring","useIsRestoring","defaultedQueries","React","useMemo","map","options","defaultedOptions","defaultQueryOptions","_optimisticResults","observer","useState","QueriesObserver","optimisticResult","getOptimisticResult","useSyncExternalStore","useCallback","onStoreChange","undefined","subscribe","notifyManager","batchCalls","getCurrentResult","useEffect","setQueries","listeners","errorResetBoundary","useQueryErrorResetBoundary","forEach","query","ensurePreventErrorBoundaryRetry","ensureStaleTime","useClearResetErrorBoundary","shouldAtLeastOneSuspend","some","result","index","shouldSuspend","suspensePromises","flatMap","queryObserver","getObservers","fetchOptimistic","willFetch","length","Promise","all","firstSingleResultWhichShouldThrow","find","getHasError","throwErrors","getQueries","error"],"mappings":";;;;;;;;AA0BA;AACA;;AAkIO,SAASA,UAAU,CACxB;AACEC,EAAAA,OAAAA;AAGF,CAAC,EACDC,WAAyB,EACN;AACnB,EAAA,MAAMC,MAAM,GAAGC,cAAc,CAACF,WAAW,CAAC,CAAA;EAC1C,MAAMG,WAAW,GAAGC,cAAc,EAAE,CAAA;AAEpC,EAAA,MAAMC,gBAAgB,GAAGC,KAAK,CAACC,OAAO,CACpC,MACER,OAAO,CAACS,GAAG,CAAEC,OAAO,IAAK;AACvB,IAAA,MAAMC,gBAAgB,GAAGT,MAAM,CAACU,mBAAmB,CAACF,OAAO,CAAC,CAAA;;AAE5D;AACAC,IAAAA,gBAAgB,CAACE,kBAAkB,GAAGT,WAAW,GAC7C,aAAa,GACb,YAAY,CAAA;AAEhB,IAAA,OAAOO,gBAAgB,CAAA;GACxB,CAAC,EACJ,CAACX,OAAO,EAAEE,MAAM,EAAEE,WAAW,CAAC,CAC/B,CAAA;AAED,EAAA,MAAM,CAACU,QAAQ,CAAC,GAAGP,KAAK,CAACQ,QAAQ,CAC/B,MAAM,IAAIC,eAAe,CAACd,MAAM,EAAEI,gBAAgB,CAAC,CACpD,CAAA;AAED,EAAA,MAAMW,gBAAgB,GAAGH,QAAQ,CAACI,mBAAmB,CAACZ,gBAAgB,CAAC,CAAA;EAEvEC,KAAK,CAACY,oBAAoB,CACxBZ,KAAK,CAACa,WAAW,CACdC,aAAa,IACZjB,WAAW,GACP,MAAMkB,SAAS,GACfR,QAAQ,CAACS,SAAS,CAACC,aAAa,CAACC,UAAU,CAACJ,aAAa,CAAC,CAAC,EACjE,CAACP,QAAQ,EAAEV,WAAW,CAAC,CACxB,EACD,MAAMU,QAAQ,CAACY,gBAAgB,EAAE,EACjC,MAAMZ,QAAQ,CAACY,gBAAgB,EAAE,CAClC,CAAA;EAEDnB,KAAK,CAACoB,S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,EAAEkC,aAAa,EAAEd,kBAAkB,CAAC,CAAA;AAClE,OAAA;AACF,KAAA;AACA,IAAA,OAAO,EAAE,CAAA;GACV,CAAC,GACF,EAAE,CAAA;AAEN,EAAA,IAAIY,gBAAgB,CAACM,MAAM,GAAG,CAAC,EAAE;AAC/B,IAAA,MAAMC,OAAO,CAACC,GAAG,CAACR,gBAAgB,CAAC,CAAA;AACrC,GAAA;EAEA,MAAMS,iCAAiC,GAAGlC,gBAAgB,CAACmC,IAAI,CAC7D,CAACb,MAAM,EAAEC,KAAK,KAAA;AAAA,IAAA,IAAA,qBAAA,EAAA,sBAAA,CAAA;AAAA,IAAA,OACZa,WAAW,CAAC;MACVd,MAAM;MACNT,kBAAkB;MAClBwB,WAAW,EAAA,CAAA,qBAAA,GAAA,CAAA,sBAAA,GAAEhD,gBAAgB,CAACkC,KAAK,CAAC,KAAvB,IAAA,GAAA,KAAA,CAAA,GAAA,sBAAA,CAAyBc,WAAW,KAAA,IAAA,GAAA,qBAAA,GAAI,KAAK;AAC1DrB,MAAAA,KAAK,EAAEnB,QAAQ,CAACyC,UAAU,EAAE,CAACf,KAAK,CAAA;AACpC,KAAC,CAAC,CAAA;GACL,CAAA,CAAA;AAED,EAAA,IAAIW,iCAAiC,IAAA,IAAA,IAAjCA,iCAAiC,CAAEK,KAAK,EAAE;IAC5C,MAAML,iCAAiC,CAACK,KAAK,CAAA;AAC/C,GAAA;AAEA,EAAA,OAAOvC,gBAAgB,CAAA;AACzB;;;;"}
|
package/build/lib/useQueries.js
CHANGED
|
@@ -74,13 +74,16 @@ function useQueries({
|
|
|
74
74
|
if (suspensePromises.length > 0) {
|
|
75
75
|
throw Promise.all(suspensePromises);
|
|
76
76
|
}
|
|
77
|
-
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) =>
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => {
|
|
78
|
+
var _defaultedQueries$ind, _defaultedQueries$ind2;
|
|
79
|
+
return errorBoundaryUtils.getHasError({
|
|
80
|
+
result,
|
|
81
|
+
errorResetBoundary,
|
|
82
|
+
throwErrors: (_defaultedQueries$ind = (_defaultedQueries$ind2 = defaultedQueries[index]) == null ? void 0 : _defaultedQueries$ind2.throwErrors) != null ? _defaultedQueries$ind : false,
|
|
83
|
+
query: observer.getQueries()[index]
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
if (firstSingleResultWhichShouldThrow != null && firstSingleResultWhichShouldThrow.error) {
|
|
84
87
|
throw firstSingleResultWhichShouldThrow.error;
|
|
85
88
|
}
|
|
86
89
|
return optimisticResult;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueries.js","sources":["../../src/useQueries.ts"],"sourcesContent":["import * as React from 'react'\n\nimport type {\n QueryKey,\n QueryFunction,\n QueriesPlaceholderDataFunction,\n QueryClient,\n DefaultError,\n} from '@tanstack/query-core'\nimport { notifyManager, QueriesObserver } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type { UseQueryOptions, UseQueryResult } from './types'\nimport { useIsRestoring } from './isRestoring'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureStaleTime,\n shouldSuspend,\n fetchOptimistic,\n willFetch,\n} from './suspense'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = Omit<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\ntype GetOptions<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>\n select: (data: any) => infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TData, TQueryKey>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TQueryFnData, TQueryKey>\n : // Fallback\n UseQueryOptionsForUseQueries\n\ntype GetResults<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? UseQueryResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryResult<TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? UseQueryResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryResult<TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<unknown, any>\n select: (data: any) => infer TData\n }\n ? UseQueryResult<TData>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, any> }\n ? UseQueryResult<TQueryFnData>\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryOptionsForUseQueries[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>\n : unknown[] extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >[]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>[]\n : // Fallback\n UseQueryOptionsForUseQueries[]\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryResult[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>\n : T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >[]\n ? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results\n UseQueryResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >[]\n : // Fallback\n UseQueryResult[]\n\nexport function useQueries<T extends any[]>(\n {\n queries,\n }: {\n queries: readonly [...QueriesOptions<T>]\n },\n queryClient?: QueryClient,\n): QueriesResults<T> {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n\n const defaultedQueries = React.useMemo(\n () =>\n queries.map((options) => {\n const defaultedOptions = client.defaultQueryOptions(options)\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n const [observer] = React.useState(\n () => new QueriesObserver(client, defaultedQueries),\n )\n\n const optimisticResult = observer.getOptimisticResult(defaultedQueries)\n\n React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n isRestoring\n ? () => undefined\n : observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer, isRestoring],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n React.useEffect(() => {\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n observer.setQueries(defaultedQueries, { listeners: false })\n }, [defaultedQueries, observer])\n\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n defaultedQueries.forEach((query) => {\n ensurePreventErrorBoundaryRetry(query, errorResetBoundary)\n ensureStaleTime(query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result, isRestoring),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const options = defaultedQueries[index]\n const queryObserver = observer.getObservers()[index]\n\n if (options && queryObserver) {\n if (shouldSuspend(options, result, isRestoring)) {\n return fetchOptimistic(options, queryObserver, errorResetBoundary)\n } else if (willFetch(result, isRestoring)) {\n void fetchOptimistic(options, queryObserver, errorResetBoundary)\n }\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) =>\n getHasError({\n result,\n errorResetBoundary,\n throwErrors: defaultedQueries[index]?.throwErrors ?? false,\n query: observer.getQueries()[index]!,\n }),\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return optimisticResult as QueriesResults<T>\n}\n"],"names":["useQueries","queries","queryClient","client","useQueryClient","isRestoring","useIsRestoring","defaultedQueries","React","useMemo","map","options","defaultedOptions","defaultQueryOptions","_optimisticResults","observer","useState","QueriesObserver","optimisticResult","getOptimisticResult","useSyncExternalStore","useCallback","onStoreChange","undefined","subscribe","notifyManager","batchCalls","getCurrentResult","useEffect","setQueries","listeners","errorResetBoundary","useQueryErrorResetBoundary","forEach","query","ensurePreventErrorBoundaryRetry","ensureStaleTime","useClearResetErrorBoundary","shouldAtLeastOneSuspend","some","result","index","shouldSuspend","suspensePromises","flatMap","queryObserver","getObservers","fetchOptimistic","willFetch","length","Promise","all","firstSingleResultWhichShouldThrow","find","getHasError","throwErrors","getQueries","error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA;AACA;;AAkIO,SAASA,UAAU,CACxB;AACEC,EAAAA,OAAAA;AAGF,CAAC,EACDC,WAAyB,EACN;AACnB,EAAA,MAAMC,MAAM,GAAGC,kCAAc,CAACF,WAAW,CAAC,CAAA;EAC1C,MAAMG,aAAW,GAAGC,0BAAc,EAAE,CAAA;AAEpC,EAAA,MAAMC,gBAAgB,GAAGC,gBAAK,CAACC,OAAO,CACpC,MACER,OAAO,CAACS,GAAG,CAAEC,OAAO,IAAK;AACvB,IAAA,MAAMC,gBAAgB,GAAGT,MAAM,CAACU,mBAAmB,CAACF,OAAO,CAAC,CAAA;;AAE5D;AACAC,IAAAA,gBAAgB,CAACE,kBAAkB,GAAGT,aAAW,GAC7C,aAAa,GACb,YAAY,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ACpC,GAAC,CAAC,CACL,CAAA;EAED,IAAIW,iCAAiC,EAAEK,KAAK,EAAE;IAC5C,MAAML,iCAAiC,CAACK,KAAK,CAAA;AAC/C,GAAA;AAEA,EAAA,OAAOvC,gBAAgB,CAAA;AACzB;;;;"}
|
|
1
|
+
{"version":3,"file":"useQueries.js","sources":["../../src/useQueries.ts"],"sourcesContent":["import * as React from 'react'\n\nimport type {\n QueryKey,\n QueryFunction,\n QueriesPlaceholderDataFunction,\n QueryClient,\n DefaultError,\n} from '@tanstack/query-core'\nimport { notifyManager, QueriesObserver } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type { UseQueryOptions, UseQueryResult } from './types'\nimport { useIsRestoring } from './isRestoring'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureStaleTime,\n shouldSuspend,\n fetchOptimistic,\n willFetch,\n} from './suspense'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = Omit<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\ntype GetOptions<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>\n select: (data: any) => infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TData, TQueryKey>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }\n ? UseQueryOptionsForUseQueries<TQueryFnData, Error, TQueryFnData, TQueryKey>\n : // Fallback\n UseQueryOptionsForUseQueries\n\ntype GetResults<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? UseQueryResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryResult<TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? UseQueryResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryResult<TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?: QueryFunction<unknown, any>\n select: (data: any) => infer TData\n }\n ? UseQueryResult<TData>\n : T extends { queryFn?: QueryFunction<infer TQueryFnData, any> }\n ? UseQueryResult<TQueryFnData>\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryOptionsForUseQueries[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>\n : unknown[] extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >[]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>[]\n : // Fallback\n UseQueryOptionsForUseQueries[]\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends any[],\n Result extends any[] = [],\n Depth extends ReadonlyArray<number> = [],\n> = Depth['length'] extends MAXIMUM_DEPTH\n ? UseQueryResult[]\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...Result, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>\n : T extends UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >[]\n ? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results\n UseQueryResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >[]\n : // Fallback\n UseQueryResult[]\n\nexport function useQueries<T extends any[]>(\n {\n queries,\n }: {\n queries: readonly [...QueriesOptions<T>]\n },\n queryClient?: QueryClient,\n): QueriesResults<T> {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n\n const defaultedQueries = React.useMemo(\n () =>\n queries.map((options) => {\n const defaultedOptions = client.defaultQueryOptions(options)\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n const [observer] = React.useState(\n () => new QueriesObserver(client, defaultedQueries),\n )\n\n const optimisticResult = observer.getOptimisticResult(defaultedQueries)\n\n React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n isRestoring\n ? () => undefined\n : observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer, isRestoring],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n React.useEffect(() => {\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n observer.setQueries(defaultedQueries, { listeners: false })\n }, [defaultedQueries, observer])\n\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n defaultedQueries.forEach((query) => {\n ensurePreventErrorBoundaryRetry(query, errorResetBoundary)\n ensureStaleTime(query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result, isRestoring),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const options = defaultedQueries[index]\n const queryObserver = observer.getObservers()[index]\n\n if (options && queryObserver) {\n if (shouldSuspend(options, result, isRestoring)) {\n return fetchOptimistic(options, queryObserver, errorResetBoundary)\n } else if (willFetch(result, isRestoring)) {\n void fetchOptimistic(options, queryObserver, errorResetBoundary)\n }\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) =>\n getHasError({\n result,\n errorResetBoundary,\n throwErrors: defaultedQueries[index]?.throwErrors ?? false,\n query: observer.getQueries()[index]!,\n }),\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return optimisticResult as QueriesResults<T>\n}\n"],"names":["useQueries","queries","queryClient","client","useQueryClient","isRestoring","useIsRestoring","defaultedQueries","React","useMemo","map","options","defaultedOptions","defaultQueryOptions","_optimisticResults","observer","useState","QueriesObserver","optimisticResult","getOptimisticResult","useSyncExternalStore","useCallback","onStoreChange","undefined","subscribe","notifyManager","batchCalls","getCurrentResult","useEffect","setQueries","listeners","errorResetBoundary","useQueryErrorResetBoundary","forEach","query","ensurePreventErrorBoundaryRetry","ensureStaleTime","useClearResetErrorBoundary","shouldAtLeastOneSuspend","some","result","index","shouldSuspend","suspensePromises","flatMap","queryObserver","getObservers","fetchOptimistic","willFetch","length","Promise","all","firstSingleResultWhichShouldThrow","find","getHasError","throwErrors","getQueries","error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA;AACA;;AAkIO,SAASA,UAAU,CACxB;AACEC,EAAAA,OAAAA;AAGF,CAAC,EACDC,WAAyB,EACN;AACnB,EAAA,MAAMC,MAAM,GAAGC,kCAAc,CAACF,WAAW,CAAC,CAAA;EAC1C,MAAMG,aAAW,GAAGC,0BAAc,EAAE,CAAA;AAEpC,EAAA,MAAMC,gBAAgB,GAAGC,gBAAK,CAACC,OAAO,CACpC,MACER,OAAO,CAACS,GAAG,CAAEC,OAAO,IAAK;AACvB,IAAA,MAAMC,gBAAgB,GAAGT,MAAM,CAACU,mBAAmB,CAACF,OAAO,CAAC,CAAA;;AAE5D;AACAC,IAAAA,gBAAgB,CAACE,kBAAkB,GAAGT,aAAW,GAC7C,aAAa,GACb,YAAY,CAAA;AAEhB,IAAA,OAAOO,gBAAgB,CAAA;GACxB,CAAC,EACJ,CAACX,OAAO,EAAEE,MAAM,EAAEE,aAAW,CAAC,CAC/B,CAAA;AAED,EAAA,MAAM,CAACU,QAAQ,CAAC,GAAGP,gBAAK,CAACQ,QAAQ,CAC/B,MAAM,IAAIC,yBAAe,CAACd,MAAM,EAAEI,gBAAgB,CAAC,CACpD,CAAA;AAED,EAAA,MAAMW,gBAAgB,GAAGH,QAAQ,CA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iC,CAACK,KAAK,CAAA;AAC/C,GAAA;AAEA,EAAA,OAAOvC,gBAAgB,CAAA;AACzB;;;;"}
|
|
@@ -1473,12 +1473,15 @@
|
|
|
1473
1473
|
function infiniteQueryBehavior() {
|
|
1474
1474
|
return {
|
|
1475
1475
|
onFetch: context => {
|
|
1476
|
-
context.fetchFn = () => {
|
|
1476
|
+
context.fetchFn = async () => {
|
|
1477
1477
|
const options = context.options;
|
|
1478
1478
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction;
|
|
1479
1479
|
const oldPages = context.state.data?.pages || [];
|
|
1480
1480
|
const oldPageParams = context.state.data?.pageParams || [];
|
|
1481
|
-
|
|
1481
|
+
const empty = {
|
|
1482
|
+
pages: [],
|
|
1483
|
+
pageParams: []
|
|
1484
|
+
};
|
|
1482
1485
|
let cancelled = false;
|
|
1483
1486
|
const addSignalProperty = object => {
|
|
1484
1487
|
Object.defineProperty(object, 'signal', {
|
|
@@ -1498,95 +1501,95 @@
|
|
|
1498
1501
|
|
|
1499
1502
|
// Get query function
|
|
1500
1503
|
const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
|
|
1501
|
-
const buildNewPages = (pages, param, page, previous) => {
|
|
1502
|
-
const {
|
|
1503
|
-
maxPages
|
|
1504
|
-
} = context.options;
|
|
1505
|
-
if (previous) {
|
|
1506
|
-
newPageParams = addToStart(newPageParams, param, maxPages);
|
|
1507
|
-
return addToStart(pages, page, maxPages);
|
|
1508
|
-
}
|
|
1509
|
-
newPageParams = addToEnd(newPageParams, param, maxPages);
|
|
1510
|
-
return addToEnd(pages, page, maxPages);
|
|
1511
|
-
};
|
|
1512
1504
|
|
|
1513
1505
|
// Create function to fetch a page
|
|
1514
|
-
const fetchPage = (
|
|
1506
|
+
const fetchPage = async (data, param, previous) => {
|
|
1515
1507
|
if (cancelled) {
|
|
1516
1508
|
return Promise.reject();
|
|
1517
1509
|
}
|
|
1518
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
1519
|
-
return Promise.resolve(
|
|
1510
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
1511
|
+
return Promise.resolve(data);
|
|
1520
1512
|
}
|
|
1521
1513
|
const queryFnContext = {
|
|
1522
1514
|
queryKey: context.queryKey,
|
|
1523
1515
|
pageParam: param,
|
|
1516
|
+
direction: previous ? 'backward' : 'forward',
|
|
1524
1517
|
meta: context.options.meta
|
|
1525
1518
|
};
|
|
1526
1519
|
addSignalProperty(queryFnContext);
|
|
1527
|
-
const
|
|
1528
|
-
const
|
|
1529
|
-
|
|
1520
|
+
const page = await queryFn(queryFnContext);
|
|
1521
|
+
const {
|
|
1522
|
+
maxPages
|
|
1523
|
+
} = context.options;
|
|
1524
|
+
const addTo = previous ? addToStart : addToEnd;
|
|
1525
|
+
return {
|
|
1526
|
+
pages: addTo(data.pages, page, maxPages),
|
|
1527
|
+
pageParams: addTo(data.pageParams, param, maxPages)
|
|
1528
|
+
};
|
|
1530
1529
|
};
|
|
1531
|
-
let
|
|
1530
|
+
let result;
|
|
1532
1531
|
|
|
1533
1532
|
// Fetch first page?
|
|
1534
1533
|
if (!oldPages.length) {
|
|
1535
|
-
|
|
1534
|
+
result = await fetchPage(empty, options.defaultPageParam);
|
|
1536
1535
|
}
|
|
1537
1536
|
|
|
1538
1537
|
// fetch next / previous page?
|
|
1539
1538
|
else if (direction) {
|
|
1540
1539
|
const previous = direction === 'backward';
|
|
1541
|
-
const
|
|
1542
|
-
|
|
1540
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
|
|
1541
|
+
const oldData = {
|
|
1542
|
+
pages: oldPages,
|
|
1543
|
+
pageParams: oldPageParams
|
|
1544
|
+
};
|
|
1545
|
+
const param = pageParamFn(options, oldData);
|
|
1546
|
+
result = await fetchPage(oldData, param, previous);
|
|
1543
1547
|
}
|
|
1544
1548
|
|
|
1545
1549
|
// Refetch pages
|
|
1546
1550
|
else {
|
|
1547
|
-
newPageParams = [];
|
|
1548
|
-
|
|
1549
1551
|
// Fetch first page
|
|
1550
|
-
|
|
1552
|
+
result = await fetchPage(empty, oldPageParams[0]);
|
|
1551
1553
|
|
|
1552
1554
|
// Fetch remaining pages
|
|
1553
1555
|
for (let i = 1; i < oldPages.length; i++) {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
return fetchPage(pages, param);
|
|
1557
|
-
});
|
|
1556
|
+
const param = getNextPageParam(options, result);
|
|
1557
|
+
result = await fetchPage(result, param);
|
|
1558
1558
|
}
|
|
1559
1559
|
}
|
|
1560
|
-
|
|
1561
|
-
pages,
|
|
1562
|
-
pageParams: newPageParams
|
|
1563
|
-
}));
|
|
1564
|
-
return finalPromise;
|
|
1560
|
+
return result;
|
|
1565
1561
|
};
|
|
1566
1562
|
}
|
|
1567
1563
|
};
|
|
1568
1564
|
}
|
|
1569
|
-
function getNextPageParam(options,
|
|
1570
|
-
|
|
1565
|
+
function getNextPageParam(options, {
|
|
1566
|
+
pages,
|
|
1567
|
+
pageParams
|
|
1568
|
+
}) {
|
|
1569
|
+
const lastIndex = pages.length - 1;
|
|
1570
|
+
return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
|
|
1571
1571
|
}
|
|
1572
|
-
function getPreviousPageParam(options,
|
|
1573
|
-
|
|
1572
|
+
function getPreviousPageParam(options, {
|
|
1573
|
+
pages,
|
|
1574
|
+
pageParams
|
|
1575
|
+
}) {
|
|
1576
|
+
return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
|
|
1574
1577
|
}
|
|
1575
1578
|
|
|
1576
1579
|
/**
|
|
1577
1580
|
* Checks if there is a next page.
|
|
1578
1581
|
*/
|
|
1579
|
-
function hasNextPage(options,
|
|
1580
|
-
if (!
|
|
1581
|
-
return typeof getNextPageParam(options,
|
|
1582
|
+
function hasNextPage(options, data) {
|
|
1583
|
+
if (!data) return false;
|
|
1584
|
+
return typeof getNextPageParam(options, data) !== 'undefined';
|
|
1582
1585
|
}
|
|
1583
1586
|
|
|
1584
1587
|
/**
|
|
1585
1588
|
* Checks if there is a previous page.
|
|
1586
1589
|
*/
|
|
1587
|
-
function hasPreviousPage(options,
|
|
1588
|
-
if (!
|
|
1589
|
-
return typeof getPreviousPageParam(options,
|
|
1590
|
+
function hasPreviousPage(options, data) {
|
|
1591
|
+
if (!data || !options.getPreviousPageParam) return false;
|
|
1592
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined';
|
|
1590
1593
|
}
|
|
1591
1594
|
|
|
1592
1595
|
// CLASS
|
|
@@ -2444,7 +2447,7 @@
|
|
|
2444
2447
|
options.behavior = infiniteQueryBehavior();
|
|
2445
2448
|
return super.getOptimisticResult(options);
|
|
2446
2449
|
}
|
|
2447
|
-
fetchNextPage(options
|
|
2450
|
+
fetchNextPage(options) {
|
|
2448
2451
|
return this.fetch({
|
|
2449
2452
|
...options,
|
|
2450
2453
|
meta: {
|
|
@@ -2454,9 +2457,7 @@
|
|
|
2454
2457
|
}
|
|
2455
2458
|
});
|
|
2456
2459
|
}
|
|
2457
|
-
fetchPreviousPage({
|
|
2458
|
-
...options
|
|
2459
|
-
} = {}) {
|
|
2460
|
+
fetchPreviousPage(options) {
|
|
2460
2461
|
return this.fetch({
|
|
2461
2462
|
...options,
|
|
2462
2463
|
meta: {
|
|
@@ -2481,8 +2482,8 @@
|
|
|
2481
2482
|
...result,
|
|
2482
2483
|
fetchNextPage: this.fetchNextPage,
|
|
2483
2484
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
2484
|
-
hasNextPage: hasNextPage(options, state.data
|
|
2485
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
2485
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
2486
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
2486
2487
|
isFetchingNextPage,
|
|
2487
2488
|
isFetchingPreviousPage,
|
|
2488
2489
|
isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
|