1 | import { __assign } from "tslib";
|
2 | import * as React from "rehackt";
|
3 | import { mergeOptions } from "../../utilities/index.js";
|
4 | import { createMakeWatchQueryOptions, getDefaultFetchPolicy, getObsQueryOptions, toQueryResult, useQueryInternals, } from "./useQuery.js";
|
5 | import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
|
6 |
|
7 |
|
8 | var EAGER_METHODS = [
|
9 | "refetch",
|
10 | "reobserve",
|
11 | "fetchMore",
|
12 | "updateQuery",
|
13 | "startPolling",
|
14 | "stopPolling",
|
15 | "subscribeToMore",
|
16 | ];
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | export function useLazyQuery(query, options) {
|
53 | var _a;
|
54 | var execOptionsRef = React.useRef();
|
55 | var optionsRef = React.useRef();
|
56 | var queryRef = React.useRef();
|
57 | var merged = mergeOptions(options, execOptionsRef.current || {});
|
58 | var document = (_a = merged === null || merged === void 0 ? void 0 : merged.query) !== null && _a !== void 0 ? _a : query;
|
59 |
|
60 |
|
61 | optionsRef.current = options;
|
62 | queryRef.current = document;
|
63 | var queryHookOptions = __assign(__assign({}, merged), { skip: !execOptionsRef.current });
|
64 | var _b = useQueryInternals(document, queryHookOptions), obsQueryFields = _b.obsQueryFields, useQueryResult = _b.result, client = _b.client, resultData = _b.resultData, observable = _b.observable, onQueryExecuted = _b.onQueryExecuted;
|
65 | var initialFetchPolicy = observable.options.initialFetchPolicy ||
|
66 | getDefaultFetchPolicy(queryHookOptions.defaultOptions, client.defaultOptions);
|
67 | var forceUpdateState = React.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
68 |
|
69 | var eagerMethods = React.useMemo(function () {
|
70 | var eagerMethods = {};
|
71 | var _loop_1 = function (key) {
|
72 | var method = obsQueryFields[key];
|
73 | eagerMethods[key] = function () {
|
74 | if (!execOptionsRef.current) {
|
75 | execOptionsRef.current = Object.create(null);
|
76 |
|
77 | forceUpdateState();
|
78 | }
|
79 |
|
80 | return method.apply(this, arguments);
|
81 | };
|
82 | };
|
83 | for (var _i = 0, EAGER_METHODS_1 = EAGER_METHODS; _i < EAGER_METHODS_1.length; _i++) {
|
84 | var key = EAGER_METHODS_1[_i];
|
85 | _loop_1(key);
|
86 | }
|
87 | return eagerMethods;
|
88 | }, [forceUpdateState, obsQueryFields]);
|
89 | var called = !!execOptionsRef.current;
|
90 | var result = React.useMemo(function () { return (__assign(__assign(__assign({}, useQueryResult), eagerMethods), { called: called })); }, [useQueryResult, eagerMethods, called]);
|
91 | var execute = React.useCallback(function (executeOptions) {
|
92 | execOptionsRef.current =
|
93 | executeOptions ? __assign(__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
94 | fetchPolicy: initialFetchPolicy,
|
95 | };
|
96 | var options = mergeOptions(optionsRef.current, __assign({ query: queryRef.current }, execOptionsRef.current));
|
97 | var promise = executeQuery(resultData, observable, client, document, __assign(__assign({}, options), { skip: false }), onQueryExecuted).then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
98 |
|
99 |
|
100 | promise.catch(function () { });
|
101 | return promise;
|
102 | }, [
|
103 | client,
|
104 | document,
|
105 | eagerMethods,
|
106 | initialFetchPolicy,
|
107 | observable,
|
108 | resultData,
|
109 | onQueryExecuted,
|
110 | ]);
|
111 | var executeRef = React.useRef(execute);
|
112 | useIsomorphicLayoutEffect(function () {
|
113 | executeRef.current = execute;
|
114 | });
|
115 | var stableExecute = React.useCallback(function () {
|
116 | var args = [];
|
117 | for (var _i = 0; _i < arguments.length; _i++) {
|
118 | args[_i] = arguments[_i];
|
119 | }
|
120 | return executeRef.current.apply(executeRef, args);
|
121 | }, []);
|
122 | return [stableExecute, result];
|
123 | }
|
124 | function executeQuery(resultData, observable, client, currentQuery, options, onQueryExecuted) {
|
125 | var query = options.query || currentQuery;
|
126 | var watchQueryOptions = createMakeWatchQueryOptions(client, query, options, false)(observable);
|
127 | var concast = observable.reobserveAsConcast(getObsQueryOptions(observable, client, options, watchQueryOptions));
|
128 | onQueryExecuted(watchQueryOptions);
|
129 | return new Promise(function (resolve) {
|
130 | var result;
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | concast.subscribe({
|
136 | next: function (value) {
|
137 | result = value;
|
138 | },
|
139 | error: function () {
|
140 | resolve(toQueryResult(observable.getCurrentResult(), resultData.previousData, observable, client));
|
141 | },
|
142 | complete: function () {
|
143 | resolve(toQueryResult(result, resultData.previousData, observable, client));
|
144 | },
|
145 | });
|
146 | });
|
147 | }
|
148 |
|
\ | No newline at end of file |