1 | import { __assign } from "tslib";
|
2 | import { invariant } from "../../utilities/globals/index.js";
|
3 | import * as React from "rehackt";
|
4 | import { equal } from "@wry/equality";
|
5 | import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
6 | import { ApolloError, Observable } from "../../core/index.js";
|
7 | import { useApolloClient } from "./useApolloClient.js";
|
8 | import { useDeepMemo } from "./internal/useDeepMemo.js";
|
9 | import { useSyncExternalStore } from "./useSyncExternalStore.js";
|
10 | import { toApolloError } from "./useQuery.js";
|
11 | import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
|
12 |
|
13 |
|
14 |
|
15 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export function useSubscription(subscription, options) {
|
94 | if (options === void 0) { options = Object.create(null); }
|
95 | var hasIssuedDeprecationWarningRef = React.useRef(false);
|
96 | var client = useApolloClient(options.client);
|
97 | verifyDocumentType(subscription, DocumentType.Subscription);
|
98 | if (!hasIssuedDeprecationWarningRef.current) {
|
99 | hasIssuedDeprecationWarningRef.current = true;
|
100 | if (options.onSubscriptionData) {
|
101 | globalThis.__DEV__ !== false && invariant.warn(options.onData ? 53 : 54);
|
102 | }
|
103 | if (options.onSubscriptionComplete) {
|
104 | globalThis.__DEV__ !== false && invariant.warn(options.onComplete ? 55 : 56);
|
105 | }
|
106 | }
|
107 | var skip = options.skip, fetchPolicy = options.fetchPolicy, errorPolicy = options.errorPolicy, shouldResubscribe = options.shouldResubscribe, context = options.context, extensions = options.extensions, ignoreResults = options.ignoreResults;
|
108 | var variables = useDeepMemo(function () { return options.variables; }, [options.variables]);
|
109 | var recreate = function () {
|
110 | return createSubscription(client, subscription, variables, fetchPolicy, errorPolicy, context, extensions);
|
111 | };
|
112 | var _a = React.useState(options.skip ? null : recreate), observable = _a[0], setObservable = _a[1];
|
113 | var recreateRef = React.useRef(recreate);
|
114 | useIsomorphicLayoutEffect(function () {
|
115 | recreateRef.current = recreate;
|
116 | });
|
117 | if (skip) {
|
118 | if (observable) {
|
119 | setObservable((observable = null));
|
120 | }
|
121 | }
|
122 | else if (!observable ||
|
123 | ((client !== observable.__.client ||
|
124 | subscription !== observable.__.query ||
|
125 | fetchPolicy !== observable.__.fetchPolicy ||
|
126 | errorPolicy !== observable.__.errorPolicy ||
|
127 | !equal(variables, observable.__.variables)) &&
|
128 | (typeof shouldResubscribe === "function" ?
|
129 | !!shouldResubscribe(options)
|
130 | : shouldResubscribe) !== false)) {
|
131 | setObservable((observable = recreate()));
|
132 | }
|
133 | var optionsRef = React.useRef(options);
|
134 | React.useEffect(function () {
|
135 | optionsRef.current = options;
|
136 | });
|
137 | var fallbackLoading = !skip && !ignoreResults;
|
138 | var fallbackResult = React.useMemo(function () { return ({
|
139 | loading: fallbackLoading,
|
140 | error: void 0,
|
141 | data: void 0,
|
142 | variables: variables,
|
143 | }); }, [fallbackLoading, variables]);
|
144 | var ignoreResultsRef = React.useRef(ignoreResults);
|
145 | useIsomorphicLayoutEffect(function () {
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | ignoreResultsRef.current = ignoreResults;
|
155 | });
|
156 | var ret = useSyncExternalStore(React.useCallback(function (update) {
|
157 | if (!observable) {
|
158 | return function () { };
|
159 | }
|
160 | var subscriptionStopped = false;
|
161 | var variables = observable.__.variables;
|
162 | var client = observable.__.client;
|
163 | var subscription = observable.subscribe({
|
164 | next: function (fetchResult) {
|
165 | var _a, _b;
|
166 | if (subscriptionStopped) {
|
167 | return;
|
168 | }
|
169 | var result = {
|
170 | loading: false,
|
171 |
|
172 |
|
173 | data: fetchResult.data,
|
174 | error: toApolloError(fetchResult),
|
175 | variables: variables,
|
176 | };
|
177 | observable.__.setResult(result);
|
178 | if (!ignoreResultsRef.current)
|
179 | update();
|
180 | if (result.error) {
|
181 | (_b = (_a = optionsRef.current).onError) === null || _b === void 0 ? void 0 : _b.call(_a, result.error);
|
182 | }
|
183 | else if (optionsRef.current.onData) {
|
184 | optionsRef.current.onData({
|
185 | client: client,
|
186 | data: result,
|
187 | });
|
188 | }
|
189 | else if (optionsRef.current.onSubscriptionData) {
|
190 | optionsRef.current.onSubscriptionData({
|
191 | client: client,
|
192 | subscriptionData: result,
|
193 | });
|
194 | }
|
195 | },
|
196 | error: function (error) {
|
197 | var _a, _b;
|
198 | error =
|
199 | error instanceof ApolloError ? error : (new ApolloError({ protocolErrors: [error] }));
|
200 | if (!subscriptionStopped) {
|
201 | observable.__.setResult({
|
202 | loading: false,
|
203 | data: void 0,
|
204 | error: error,
|
205 | variables: variables,
|
206 | });
|
207 | if (!ignoreResultsRef.current)
|
208 | update();
|
209 | (_b = (_a = optionsRef.current).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
|
210 | }
|
211 | },
|
212 | complete: function () {
|
213 | if (!subscriptionStopped) {
|
214 | if (optionsRef.current.onComplete) {
|
215 | optionsRef.current.onComplete();
|
216 | }
|
217 | else if (optionsRef.current.onSubscriptionComplete) {
|
218 | optionsRef.current.onSubscriptionComplete();
|
219 | }
|
220 | }
|
221 | },
|
222 | });
|
223 | return function () {
|
224 |
|
225 |
|
226 |
|
227 | subscriptionStopped = true;
|
228 | setTimeout(function () {
|
229 | subscription.unsubscribe();
|
230 | });
|
231 | };
|
232 | }, [observable]), function () {
|
233 | return observable && !skip && !ignoreResults ?
|
234 | observable.__.result
|
235 | : fallbackResult;
|
236 | }, function () { return fallbackResult; });
|
237 | return React.useMemo(function () { return (__assign(__assign({}, ret), { restart: function () {
|
238 | invariant(!optionsRef.current.skip, 57);
|
239 | setObservable(recreateRef.current());
|
240 | } })); }, [ret]);
|
241 | }
|
242 | function createSubscription(client, query, variables, fetchPolicy, errorPolicy, context, extensions) {
|
243 | var options = {
|
244 | query: query,
|
245 | variables: variables,
|
246 | fetchPolicy: fetchPolicy,
|
247 | errorPolicy: errorPolicy,
|
248 | context: context,
|
249 | extensions: extensions,
|
250 | };
|
251 | var __ = __assign(__assign({}, options), { client: client, result: {
|
252 | loading: true,
|
253 | data: void 0,
|
254 | error: void 0,
|
255 | variables: variables,
|
256 | }, setResult: function (result) {
|
257 | __.result = result;
|
258 | } });
|
259 | var observable = null;
|
260 | return Object.assign(new Observable(function (observer) {
|
261 |
|
262 |
|
263 | if (!observable) {
|
264 | observable = client.subscribe(options);
|
265 | }
|
266 | var sub = observable.subscribe(observer);
|
267 | return function () { return sub.unsubscribe(); };
|
268 | }), {
|
269 | |
270 |
|
271 |
|
272 | __: __,
|
273 | });
|
274 | }
|
275 |
|
\ | No newline at end of file |