UNPKG

@tanstack/react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

51 lines (50 loc) 3.06 kB
"use client"; import { useQueryClient } from "./QueryClientProvider.js"; import { useIsRestoring } from "./IsRestoringProvider.js"; import { useQueryErrorResetBoundary } from "./QueryErrorResetBoundary.js"; import { ensurePreventErrorBoundaryRetry, getHasError, useClearResetErrorBoundary } from "./errorBoundaryUtils.js"; import { ensureSuspenseTimers, fetchOptimistic, shouldSuspend } from "./suspense.js"; import * as React from "react"; import { noop, notifyManager } from "@tanstack/query-core"; //#region src/useBaseQuery.ts function useBaseQuery(options, Observer, queryClient) { if (process.env.NODE_ENV !== "production") { if (typeof options !== "object" || Array.isArray(options)) throw new Error("Bad argument type. Starting with v5, only the \"Object\" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object"); } const isRestoring = useIsRestoring(); const errorResetBoundary = useQueryErrorResetBoundary(); const client = useQueryClient(queryClient); const defaultedOptions = client.defaultQueryOptions(options); const query = client.getQueryCache().get(defaultedOptions.queryHash); if (process.env.NODE_ENV !== "production") { if (!defaultedOptions.queryFn) console.error(`[${defaultedOptions.queryHash}]: No queryFn was passed as an option, and no default queryFn was found. The queryFn parameter is only optional when using a default queryFn. More info here: https://tanstack.com/query/latest/docs/framework/react/guides/default-query-function`); } const subscribed = options.subscribed !== false; defaultedOptions._optimisticResults = isRestoring ? "isRestoring" : subscribed ? "optimistic" : void 0; ensureSuspenseTimers(defaultedOptions); ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary, query); useClearResetErrorBoundary(errorResetBoundary); const [observer] = React.useState(() => new Observer(client, defaultedOptions)); const result = observer.getOptimisticResult(defaultedOptions); const shouldSubscribe = !isRestoring && subscribed; React.useSyncExternalStore(React.useCallback((onStoreChange) => { const unsubscribe = shouldSubscribe ? observer.subscribe(notifyManager.batchCalls(onStoreChange)) : noop; observer.updateResult(); return unsubscribe; }, [observer, shouldSubscribe]), () => observer.getCurrentResult(), () => observer.getCurrentResult()); React.useEffect(() => { observer.setOptions(defaultedOptions); }, [defaultedOptions, observer]); if (shouldSuspend(defaultedOptions, result)) throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary); if (getHasError({ result, errorResetBoundary, throwOnError: defaultedOptions.throwOnError, query, suspense: defaultedOptions.suspense })) throw result.error; return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result; } //#endregion export { useBaseQuery }; //# sourceMappingURL=useBaseQuery.js.map