1 | import { __spreadArray } from "tslib";
|
2 | import * as React from "rehackt";
|
3 | import { useApolloClient } from "./useApolloClient.js";
|
4 | import { getSuspenseCache, unwrapQueryRef, updateWrappedQueryRef, wrapQueryRef, } from "../internal/index.js";
|
5 | import { wrapHook } from "./internal/index.js";
|
6 | import { useWatchQueryOptions } from "./useSuspenseQuery.js";
|
7 | import { canonicalStringify } from "../../cache/index.js";
|
8 | export function useBackgroundQuery(query, options) {
|
9 | if (options === void 0) { options = Object.create(null); }
|
10 | return wrapHook("useBackgroundQuery", _useBackgroundQuery, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
|
11 | }
|
12 | function _useBackgroundQuery(query, options) {
|
13 | var client = useApolloClient(options.client);
|
14 | var suspenseCache = getSuspenseCache(client);
|
15 | var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
16 | var fetchPolicy = watchQueryOptions.fetchPolicy, variables = watchQueryOptions.variables;
|
17 | var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var didFetchResult = React.useRef(fetchPolicy !== "standby");
|
25 | didFetchResult.current || (didFetchResult.current = fetchPolicy !== "standby");
|
26 | var cacheKey = __spreadArray([
|
27 | query,
|
28 | canonicalStringify(variables)
|
29 | ], [].concat(queryKey), true);
|
30 | var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
|
31 | return client.watchQuery(watchQueryOptions);
|
32 | });
|
33 | var _b = React.useState(wrapQueryRef(queryRef)), wrappedQueryRef = _b[0], setWrappedQueryRef = _b[1];
|
34 | if (unwrapQueryRef(wrappedQueryRef) !== queryRef) {
|
35 | setWrappedQueryRef(wrapQueryRef(queryRef));
|
36 | }
|
37 | if (queryRef.didChangeOptions(watchQueryOptions)) {
|
38 | var promise = queryRef.applyOptions(watchQueryOptions);
|
39 | updateWrappedQueryRef(wrappedQueryRef, promise);
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | React.useEffect(function () {
|
47 |
|
48 |
|
49 | var id = setTimeout(function () {
|
50 | if (queryRef.disposed) {
|
51 | suspenseCache.add(cacheKey, queryRef);
|
52 | }
|
53 | });
|
54 | return function () { return clearTimeout(id); };
|
55 |
|
56 |
|
57 | });
|
58 | var fetchMore = React.useCallback(function (options) {
|
59 | var promise = queryRef.fetchMore(options);
|
60 | setWrappedQueryRef(wrapQueryRef(queryRef));
|
61 | return promise;
|
62 | }, [queryRef]);
|
63 | var refetch = React.useCallback(function (variables) {
|
64 | var promise = queryRef.refetch(variables);
|
65 | setWrappedQueryRef(wrapQueryRef(queryRef));
|
66 | return promise;
|
67 | }, [queryRef]);
|
68 | React.useEffect(function () { return queryRef.softRetain(); }, [queryRef]);
|
69 | return [
|
70 | didFetchResult.current ? wrappedQueryRef : void 0,
|
71 | {
|
72 | fetchMore: fetchMore,
|
73 | refetch: refetch,
|
74 | subscribeToMore: queryRef.observable.subscribeToMore,
|
75 | },
|
76 | ];
|
77 | }
|
78 |
|
\ | No newline at end of file |