UNPKG

2.28 kBJavaScriptView Raw
1import * as React from "rehackt";
2import { assertWrappedQueryRef, getWrappedPromise, unwrapQueryRef, updateWrappedQueryRef, } from "../internal/index.js";
3import { __use, wrapHook } from "./internal/index.js";
4import { toApolloError } from "./useSuspenseQuery.js";
5import { useSyncExternalStore } from "./useSyncExternalStore.js";
6import { useApolloClient } from "./useApolloClient.js";
7export function useReadQuery(queryRef) {
8 var unwrapped = unwrapQueryRef(queryRef);
9 return wrapHook("useReadQuery", _useReadQuery, unwrapped ?
10 unwrapped["observable"]
11 // in the case of a "transported" queryRef object, we need to use the
12 // client that's available to us at the current position in the React tree
13 // that ApolloClient will then have the job to recreate a real queryRef from
14 // the transported object
15 // This is just a context read - it's fine to do this conditionally.
16 // This hook wrapper also shouldn't be optimized by React Compiler.
17 // eslint-disable-next-line react-compiler/react-compiler
18 // eslint-disable-next-line react-hooks/rules-of-hooks
19 : useApolloClient())(queryRef);
20}
21function _useReadQuery(queryRef) {
22 assertWrappedQueryRef(queryRef);
23 var internalQueryRef = React.useMemo(function () { return unwrapQueryRef(queryRef); }, [queryRef]);
24 var getPromise = React.useCallback(function () { return getWrappedPromise(queryRef); }, [queryRef]);
25 if (internalQueryRef.disposed) {
26 internalQueryRef.reinitialize();
27 updateWrappedQueryRef(queryRef, internalQueryRef.promise);
28 }
29 React.useEffect(function () { return internalQueryRef.retain(); }, [internalQueryRef]);
30 var promise = useSyncExternalStore(React.useCallback(function (forceUpdate) {
31 return internalQueryRef.listen(function (promise) {
32 updateWrappedQueryRef(queryRef, promise);
33 forceUpdate();
34 });
35 }, [internalQueryRef, queryRef]), getPromise, getPromise);
36 var result = __use(promise);
37 return React.useMemo(function () {
38 return {
39 data: result.data,
40 networkStatus: result.networkStatus,
41 error: toApolloError(result),
42 };
43 }, [result]);
44}
45//# sourceMappingURL=useReadQuery.js.map
\No newline at end of file