1 | import * as React from "rehackt";
|
2 | import { assertWrappedQueryRef, getWrappedPromise, unwrapQueryRef, updateWrappedQueryRef, } from "../internal/index.js";
|
3 | import { __use, wrapHook } from "./internal/index.js";
|
4 | import { toApolloError } from "./useSuspenseQuery.js";
|
5 | import { useSyncExternalStore } from "./useSyncExternalStore.js";
|
6 | import { useApolloClient } from "./useApolloClient.js";
|
7 | export function useReadQuery(queryRef) {
|
8 | var unwrapped = unwrapQueryRef(queryRef);
|
9 | return wrapHook("useReadQuery", _useReadQuery, unwrapped ?
|
10 | unwrapped["observable"]
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | : useApolloClient())(queryRef);
|
20 | }
|
21 | function _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 |
|
\ | No newline at end of file |