UNPKG

react-query

Version:

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

97 lines (71 loc) 3.88 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; exports.__esModule = true; exports.useBaseQuery = useBaseQuery; var _react = _interopRequireDefault(require("react")); var _index = require("use-sync-external-store/shim/index.js"); var _core = require("../core"); var _QueryErrorResetBoundary = require("./QueryErrorResetBoundary"); var _QueryClientProvider = require("./QueryClientProvider"); var _utils = require("./utils"); var _isRestoring = require("./isRestoring"); function useBaseQuery(options, Observer) { const queryClient = (0, _QueryClientProvider.useQueryClient)({ context: options.context }); const isRestoring = (0, _isRestoring.useIsRestoring)(); const errorResetBoundary = (0, _QueryErrorResetBoundary.useQueryErrorResetBoundary)(); const defaultedOptions = queryClient.defaultQueryOptions(options); // Make sure results are optimistically set in fetching state before subscribing or updating options defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic'; // Include callbacks in batch renders if (defaultedOptions.onError) { defaultedOptions.onError = _core.notifyManager.batchCalls(defaultedOptions.onError); } if (defaultedOptions.onSuccess) { defaultedOptions.onSuccess = _core.notifyManager.batchCalls(defaultedOptions.onSuccess); } if (defaultedOptions.onSettled) { defaultedOptions.onSettled = _core.notifyManager.batchCalls(defaultedOptions.onSettled); } if (defaultedOptions.suspense) { // Always set stale time when using suspense to prevent // fetching again when directly mounting after suspending if (typeof defaultedOptions.staleTime !== 'number') { defaultedOptions.staleTime = 1000; } } if (defaultedOptions.suspense || defaultedOptions.useErrorBoundary) { // Prevent retrying failed query if the error boundary has not been reset yet if (!errorResetBoundary.isReset()) { defaultedOptions.retryOnMount = false; } } const [observer] = _react.default.useState(() => new Observer(queryClient, defaultedOptions)); const result = observer.getOptimisticResult(defaultedOptions); (0, _index.useSyncExternalStore)(_react.default.useCallback(onStoreChange => isRestoring ? () => undefined : observer.subscribe(_core.notifyManager.batchCalls(onStoreChange)), [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult()); _react.default.useEffect(() => { errorResetBoundary.clearReset(); }, [errorResetBoundary]); _react.default.useEffect(() => { // Do not notify on updates because of changes in the options because // these changes should already be reflected in the optimistic result. observer.setOptions(defaultedOptions, { listeners: false }); }, [defaultedOptions, observer]); // Handle suspense if (defaultedOptions.suspense && result.isLoading && result.isFetching && !isRestoring) { throw observer.fetchOptimistic(defaultedOptions).then(({ data }) => { defaultedOptions.onSuccess == null ? void 0 : defaultedOptions.onSuccess(data); defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(data, null); }).catch(error => { errorResetBoundary.clearReset(); defaultedOptions.onError == null ? void 0 : defaultedOptions.onError(error); defaultedOptions.onSettled == null ? void 0 : defaultedOptions.onSettled(undefined, error); }); } // Handle error boundary if (result.isError && !errorResetBoundary.isReset() && !result.isFetching && (0, _utils.shouldThrowError)(defaultedOptions.useErrorBoundary, [result.error, observer.getCurrentQuery()])) { throw result.error; } // Handle result property usage tracking return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result; }