111 lines
3.3 KiB
JavaScript
111 lines
3.3 KiB
JavaScript
|
"use client";
|
||
|
|
||
|
// src/useQueries.ts
|
||
|
import * as React from "react";
|
||
|
import {
|
||
|
QueriesObserver,
|
||
|
QueryObserver,
|
||
|
notifyManager
|
||
|
} from "@tanstack/query-core";
|
||
|
import { useQueryClient } from "./QueryClientProvider.js";
|
||
|
import { useIsRestoring } from "./isRestoring.js";
|
||
|
import { useQueryErrorResetBoundary } from "./QueryErrorResetBoundary.js";
|
||
|
import {
|
||
|
ensurePreventErrorBoundaryRetry,
|
||
|
getHasError,
|
||
|
useClearResetErrorBoundary
|
||
|
} from "./errorBoundaryUtils.js";
|
||
|
import {
|
||
|
ensureSuspenseTimers,
|
||
|
fetchOptimistic,
|
||
|
shouldSuspend,
|
||
|
willFetch
|
||
|
} from "./suspense.js";
|
||
|
function useQueries({
|
||
|
queries,
|
||
|
...options
|
||
|
}, queryClient) {
|
||
|
const client = useQueryClient(queryClient);
|
||
|
const isRestoring = useIsRestoring();
|
||
|
const errorResetBoundary = useQueryErrorResetBoundary();
|
||
|
const defaultedQueries = React.useMemo(
|
||
|
() => queries.map((opts) => {
|
||
|
const defaultedOptions = client.defaultQueryOptions(
|
||
|
opts
|
||
|
);
|
||
|
defaultedOptions._optimisticResults = isRestoring ? "isRestoring" : "optimistic";
|
||
|
return defaultedOptions;
|
||
|
}),
|
||
|
[queries, client, isRestoring]
|
||
|
);
|
||
|
defaultedQueries.forEach((query) => {
|
||
|
ensureSuspenseTimers(query);
|
||
|
ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
|
||
|
});
|
||
|
useClearResetErrorBoundary(errorResetBoundary);
|
||
|
const [observer] = React.useState(
|
||
|
() => new QueriesObserver(
|
||
|
client,
|
||
|
defaultedQueries,
|
||
|
options
|
||
|
)
|
||
|
);
|
||
|
const [optimisticResult, getCombinedResult, trackResult] = observer.getOptimisticResult(
|
||
|
defaultedQueries,
|
||
|
options.combine
|
||
|
);
|
||
|
React.useSyncExternalStore(
|
||
|
React.useCallback(
|
||
|
(onStoreChange) => isRestoring ? () => void 0 : observer.subscribe(notifyManager.batchCalls(onStoreChange)),
|
||
|
[observer, isRestoring]
|
||
|
),
|
||
|
() => observer.getCurrentResult(),
|
||
|
() => observer.getCurrentResult()
|
||
|
);
|
||
|
React.useEffect(() => {
|
||
|
observer.setQueries(
|
||
|
defaultedQueries,
|
||
|
options,
|
||
|
{
|
||
|
listeners: false
|
||
|
}
|
||
|
);
|
||
|
}, [defaultedQueries, options, observer]);
|
||
|
const shouldAtLeastOneSuspend = optimisticResult.some(
|
||
|
(result, index) => shouldSuspend(defaultedQueries[index], result)
|
||
|
);
|
||
|
const suspensePromises = shouldAtLeastOneSuspend ? optimisticResult.flatMap((result, index) => {
|
||
|
const opts = defaultedQueries[index];
|
||
|
if (opts) {
|
||
|
const queryObserver = new QueryObserver(client, opts);
|
||
|
if (shouldSuspend(opts, result)) {
|
||
|
return fetchOptimistic(opts, queryObserver, errorResetBoundary);
|
||
|
} else if (willFetch(result, isRestoring)) {
|
||
|
void fetchOptimistic(opts, queryObserver, errorResetBoundary);
|
||
|
}
|
||
|
}
|
||
|
return [];
|
||
|
}) : [];
|
||
|
if (suspensePromises.length > 0) {
|
||
|
throw Promise.all(suspensePromises);
|
||
|
}
|
||
|
const firstSingleResultWhichShouldThrow = optimisticResult.find(
|
||
|
(result, index) => {
|
||
|
const query = defaultedQueries[index];
|
||
|
return query && getHasError({
|
||
|
result,
|
||
|
errorResetBoundary,
|
||
|
throwOnError: query.throwOnError,
|
||
|
query: client.getQueryCache().get(query.queryHash)
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
if (firstSingleResultWhichShouldThrow == null ? void 0 : firstSingleResultWhichShouldThrow.error) {
|
||
|
throw firstSingleResultWhichShouldThrow.error;
|
||
|
}
|
||
|
return getCombinedResult(trackResult());
|
||
|
}
|
||
|
export {
|
||
|
useQueries
|
||
|
};
|
||
|
//# sourceMappingURL=useQueries.js.map
|