1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var globals = require('../../utilities/globals');
|
6 | var React = require('react');
|
7 | var context = require('../context');
|
8 | var tslib = require('tslib');
|
9 | var utilities = require('../../utilities');
|
10 | var equality = require('@wry/equality');
|
11 | var core = require('../../core');
|
12 | var errors = require('../../errors');
|
13 | var parser = require('../parser');
|
14 |
|
15 | function _interopNamespace(e) {
|
16 | if (e && e.__esModule) return e;
|
17 | var n = Object.create(null);
|
18 | if (e) {
|
19 | for (var k in e) {
|
20 | n[k] = e[k];
|
21 | }
|
22 | }
|
23 | n["default"] = e;
|
24 | return Object.freeze(n);
|
25 | }
|
26 |
|
27 | var React__namespace = _interopNamespace(React);
|
28 |
|
29 | function useApolloClient(override) {
|
30 | var context$1 = React.useContext(context.getApolloContext());
|
31 | var client = override || context$1.client;
|
32 | __DEV__ ? globals.invariant(!!client, 'Could not find "client" in the context or passed in as an option. ' +
|
33 | 'Wrap the root component in an <ApolloProvider>, or pass an ApolloClient ' +
|
34 | 'instance in via options.') : globals.invariant(!!client, 29);
|
35 | return client;
|
36 | }
|
37 |
|
38 | var didWarnUncachedGetSnapshot = false;
|
39 | var uSESKey = "useSyncExternalStore";
|
40 | var realHook = React__namespace[uSESKey];
|
41 | var useSyncExternalStore = realHook || (function (subscribe, getSnapshot, getServerSnapshot) {
|
42 | var value = getSnapshot();
|
43 | if (__DEV__ &&
|
44 | !didWarnUncachedGetSnapshot &&
|
45 | value !== getSnapshot()) {
|
46 | didWarnUncachedGetSnapshot = true;
|
47 | __DEV__ && globals.invariant.error('The result of getSnapshot should be cached to avoid an infinite loop');
|
48 | }
|
49 | var _a = React__namespace.useState({ inst: { value: value, getSnapshot: getSnapshot } }), inst = _a[0].inst, forceUpdate = _a[1];
|
50 | if (utilities.canUseLayoutEffect) {
|
51 | React__namespace.useLayoutEffect(function () {
|
52 | Object.assign(inst, { value: value, getSnapshot: getSnapshot });
|
53 | if (checkIfSnapshotChanged(inst)) {
|
54 | forceUpdate({ inst: inst });
|
55 | }
|
56 | }, [subscribe, value, getSnapshot]);
|
57 | }
|
58 | else {
|
59 | Object.assign(inst, { value: value, getSnapshot: getSnapshot });
|
60 | }
|
61 | React__namespace.useEffect(function () {
|
62 | if (checkIfSnapshotChanged(inst)) {
|
63 | forceUpdate({ inst: inst });
|
64 | }
|
65 | return subscribe(function handleStoreChange() {
|
66 | if (checkIfSnapshotChanged(inst)) {
|
67 | forceUpdate({ inst: inst });
|
68 | }
|
69 | });
|
70 | }, [subscribe]);
|
71 | return value;
|
72 | });
|
73 | function checkIfSnapshotChanged(_a) {
|
74 | var value = _a.value, getSnapshot = _a.getSnapshot;
|
75 | try {
|
76 | return value !== getSnapshot();
|
77 | }
|
78 | catch (_b) {
|
79 | return true;
|
80 | }
|
81 | }
|
82 |
|
83 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
84 | function useQuery(query, options) {
|
85 | if (options === void 0) { options = Object.create(null); }
|
86 | return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
87 | }
|
88 | function useInternalState(client, query) {
|
89 | var stateRef = React.useRef();
|
90 | if (!stateRef.current ||
|
91 | client !== stateRef.current.client ||
|
92 | query !== stateRef.current.query) {
|
93 | stateRef.current = new InternalState(client, query, stateRef.current);
|
94 | }
|
95 | var state = stateRef.current;
|
96 | var _a = React.useState(0); _a[0]; var setTick = _a[1];
|
97 | state.forceUpdate = function () {
|
98 | setTick(function (tick) { return tick + 1; });
|
99 | };
|
100 | return state;
|
101 | }
|
102 | var InternalState = (function () {
|
103 | function InternalState(client, query, previous) {
|
104 | this.client = client;
|
105 | this.query = query;
|
106 | this.asyncResolveFns = new Set();
|
107 | this.optionsToIgnoreOnce = new (utilities.canUseWeakSet ? WeakSet : Set)();
|
108 | this.ssrDisabledResult = utilities.maybeDeepFreeze({
|
109 | loading: true,
|
110 | data: void 0,
|
111 | error: void 0,
|
112 | networkStatus: core.NetworkStatus.loading,
|
113 | });
|
114 | this.skipStandbyResult = utilities.maybeDeepFreeze({
|
115 | loading: false,
|
116 | data: void 0,
|
117 | error: void 0,
|
118 | networkStatus: core.NetworkStatus.ready,
|
119 | });
|
120 | this.toQueryResultCache = new (utilities.canUseWeakMap ? WeakMap : Map)();
|
121 | parser.verifyDocumentType(query, parser.DocumentType.Query);
|
122 | var previousResult = previous && previous.result;
|
123 | var previousData = previousResult && previousResult.data;
|
124 | if (previousData) {
|
125 | this.previousData = previousData;
|
126 | }
|
127 | }
|
128 | InternalState.prototype.forceUpdate = function () {
|
129 | __DEV__ && globals.invariant.warn("Calling default no-op implementation of InternalState#forceUpdate");
|
130 | };
|
131 | InternalState.prototype.asyncUpdate = function () {
|
132 | var _this = this;
|
133 | return new Promise(function (resolve) {
|
134 | _this.asyncResolveFns.add(resolve);
|
135 | _this.optionsToIgnoreOnce.add(_this.watchQueryOptions);
|
136 | _this.forceUpdate();
|
137 | });
|
138 | };
|
139 | InternalState.prototype.useQuery = function (options) {
|
140 | var _this = this;
|
141 | this.renderPromises = React.useContext(context.getApolloContext()).renderPromises;
|
142 | this.useOptions(options);
|
143 | var obsQuery = this.useObservableQuery();
|
144 | var result = useSyncExternalStore(React.useCallback(function () {
|
145 | if (_this.renderPromises) {
|
146 | return function () { };
|
147 | }
|
148 | var onNext = function () {
|
149 | var previousResult = _this.result;
|
150 | var result = obsQuery.getCurrentResult();
|
151 | if (previousResult &&
|
152 | previousResult.loading === result.loading &&
|
153 | previousResult.networkStatus === result.networkStatus &&
|
154 | equality.equal(previousResult.data, result.data)) {
|
155 | return;
|
156 | }
|
157 | _this.setResult(result);
|
158 | };
|
159 | var onError = function (error) {
|
160 | var last = obsQuery["last"];
|
161 | subscription.unsubscribe();
|
162 | try {
|
163 | obsQuery.resetLastResults();
|
164 | subscription = obsQuery.subscribe(onNext, onError);
|
165 | }
|
166 | finally {
|
167 | obsQuery["last"] = last;
|
168 | }
|
169 | if (!hasOwnProperty.call(error, 'graphQLErrors')) {
|
170 | throw error;
|
171 | }
|
172 | var previousResult = _this.result;
|
173 | if (!previousResult ||
|
174 | (previousResult && previousResult.loading) ||
|
175 | !equality.equal(error, previousResult.error)) {
|
176 | _this.setResult({
|
177 | data: (previousResult && previousResult.data),
|
178 | error: error,
|
179 | loading: false,
|
180 | networkStatus: core.NetworkStatus.error,
|
181 | });
|
182 | }
|
183 | };
|
184 | var subscription = obsQuery.subscribe(onNext, onError);
|
185 | return function () { return subscription.unsubscribe(); };
|
186 | }, [
|
187 | obsQuery,
|
188 | this.renderPromises,
|
189 | this.client.disableNetworkFetches,
|
190 | ]), function () { return _this.getCurrentResult(); }, function () { return _this.getCurrentResult(); });
|
191 | this.unsafeHandlePartialRefetch(result);
|
192 | var queryResult = this.toQueryResult(result);
|
193 | if (!queryResult.loading && this.asyncResolveFns.size) {
|
194 | this.asyncResolveFns.forEach(function (resolve) { return resolve(queryResult); });
|
195 | this.asyncResolveFns.clear();
|
196 | }
|
197 | return queryResult;
|
198 | };
|
199 | InternalState.prototype.useOptions = function (options) {
|
200 | var _a;
|
201 | var watchQueryOptions = this.createWatchQueryOptions(this.queryHookOptions = options);
|
202 | var currentWatchQueryOptions = this.watchQueryOptions;
|
203 | if (this.optionsToIgnoreOnce.has(currentWatchQueryOptions) ||
|
204 | !equality.equal(watchQueryOptions, currentWatchQueryOptions)) {
|
205 | this.watchQueryOptions = watchQueryOptions;
|
206 | if (currentWatchQueryOptions && this.observable) {
|
207 | this.optionsToIgnoreOnce.delete(currentWatchQueryOptions);
|
208 | this.observable.reobserve(this.getObsQueryOptions());
|
209 | this.previousData = ((_a = this.result) === null || _a === void 0 ? void 0 : _a.data) || this.previousData;
|
210 | this.result = void 0;
|
211 | }
|
212 | }
|
213 | this.onCompleted = options.onCompleted || InternalState.prototype.onCompleted;
|
214 | this.onError = options.onError || InternalState.prototype.onError;
|
215 | if ((this.renderPromises || this.client.disableNetworkFetches) &&
|
216 | this.queryHookOptions.ssr === false &&
|
217 | !this.queryHookOptions.skip) {
|
218 | this.result = this.ssrDisabledResult;
|
219 | }
|
220 | else if (this.queryHookOptions.skip ||
|
221 | this.watchQueryOptions.fetchPolicy === 'standby') {
|
222 | this.result = this.skipStandbyResult;
|
223 | }
|
224 | else if (this.result === this.ssrDisabledResult ||
|
225 | this.result === this.skipStandbyResult) {
|
226 | this.result = void 0;
|
227 | }
|
228 | };
|
229 | InternalState.prototype.getObsQueryOptions = function () {
|
230 | var toMerge = [];
|
231 | var globalDefaults = this.client.defaultOptions.watchQuery;
|
232 | if (globalDefaults)
|
233 | toMerge.push(globalDefaults);
|
234 | if (this.queryHookOptions.defaultOptions) {
|
235 | toMerge.push(this.queryHookOptions.defaultOptions);
|
236 | }
|
237 | toMerge.push(utilities.compact(this.observable && this.observable.options, this.watchQueryOptions));
|
238 | return toMerge.reduce(core.mergeOptions);
|
239 | };
|
240 | InternalState.prototype.createWatchQueryOptions = function (_a) {
|
241 | var _b;
|
242 | if (_a === void 0) { _a = {}; }
|
243 | var skip = _a.skip; _a.ssr; _a.onCompleted; _a.onError; _a.displayName; _a.defaultOptions; var otherOptions = tslib.__rest(_a, ["skip", "ssr", "onCompleted", "onError", "displayName", "defaultOptions"]);
|
244 | var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
|
245 | if (this.renderPromises &&
|
246 | (watchQueryOptions.fetchPolicy === 'network-only' ||
|
247 | watchQueryOptions.fetchPolicy === 'cache-and-network')) {
|
248 | watchQueryOptions.fetchPolicy = 'cache-first';
|
249 | }
|
250 | if (!watchQueryOptions.variables) {
|
251 | watchQueryOptions.variables = {};
|
252 | }
|
253 | if (skip) {
|
254 | var _c = watchQueryOptions.fetchPolicy, fetchPolicy = _c === void 0 ? this.getDefaultFetchPolicy() : _c, _d = watchQueryOptions.initialFetchPolicy, initialFetchPolicy = _d === void 0 ? fetchPolicy : _d;
|
255 | Object.assign(watchQueryOptions, {
|
256 | initialFetchPolicy: initialFetchPolicy,
|
257 | fetchPolicy: 'standby',
|
258 | });
|
259 | }
|
260 | else if (!watchQueryOptions.fetchPolicy) {
|
261 | watchQueryOptions.fetchPolicy =
|
262 | ((_b = this.observable) === null || _b === void 0 ? void 0 : _b.options.initialFetchPolicy) ||
|
263 | this.getDefaultFetchPolicy();
|
264 | }
|
265 | return watchQueryOptions;
|
266 | };
|
267 | InternalState.prototype.getDefaultFetchPolicy = function () {
|
268 | var _a, _b;
|
269 | return (((_a = this.queryHookOptions.defaultOptions) === null || _a === void 0 ? void 0 : _a.fetchPolicy) ||
|
270 | ((_b = this.client.defaultOptions.watchQuery) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
271 | "cache-first");
|
272 | };
|
273 | InternalState.prototype.onCompleted = function (data) { };
|
274 | InternalState.prototype.onError = function (error) { };
|
275 | InternalState.prototype.useObservableQuery = function () {
|
276 | var obsQuery = this.observable =
|
277 | this.renderPromises
|
278 | && this.renderPromises.getSSRObservable(this.watchQueryOptions)
|
279 | || this.observable
|
280 | || this.client.watchQuery(this.getObsQueryOptions());
|
281 | this.obsQueryFields = React.useMemo(function () { return ({
|
282 | refetch: obsQuery.refetch.bind(obsQuery),
|
283 | reobserve: obsQuery.reobserve.bind(obsQuery),
|
284 | fetchMore: obsQuery.fetchMore.bind(obsQuery),
|
285 | updateQuery: obsQuery.updateQuery.bind(obsQuery),
|
286 | startPolling: obsQuery.startPolling.bind(obsQuery),
|
287 | stopPolling: obsQuery.stopPolling.bind(obsQuery),
|
288 | subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
|
289 | }); }, [obsQuery]);
|
290 | var ssrAllowed = !(this.queryHookOptions.ssr === false ||
|
291 | this.queryHookOptions.skip);
|
292 | if (this.renderPromises && ssrAllowed) {
|
293 | this.renderPromises.registerSSRObservable(obsQuery);
|
294 | if (obsQuery.getCurrentResult().loading) {
|
295 | this.renderPromises.addObservableQueryPromise(obsQuery);
|
296 | }
|
297 | }
|
298 | return obsQuery;
|
299 | };
|
300 | InternalState.prototype.setResult = function (nextResult) {
|
301 | var previousResult = this.result;
|
302 | if (previousResult && previousResult.data) {
|
303 | this.previousData = previousResult.data;
|
304 | }
|
305 | this.result = nextResult;
|
306 | this.forceUpdate();
|
307 | this.handleErrorOrCompleted(nextResult);
|
308 | };
|
309 | InternalState.prototype.handleErrorOrCompleted = function (result) {
|
310 | if (!result.loading) {
|
311 | if (result.error) {
|
312 | this.onError(result.error);
|
313 | }
|
314 | else if (result.data) {
|
315 | this.onCompleted(result.data);
|
316 | }
|
317 | }
|
318 | };
|
319 | InternalState.prototype.getCurrentResult = function () {
|
320 | if (!this.result) {
|
321 | this.handleErrorOrCompleted(this.result = this.observable.getCurrentResult());
|
322 | }
|
323 | return this.result;
|
324 | };
|
325 | InternalState.prototype.toQueryResult = function (result) {
|
326 | var queryResult = this.toQueryResultCache.get(result);
|
327 | if (queryResult)
|
328 | return queryResult;
|
329 | var data = result.data; result.partial; var resultWithoutPartial = tslib.__rest(result, ["data", "partial"]);
|
330 | this.toQueryResultCache.set(result, queryResult = tslib.__assign(tslib.__assign(tslib.__assign({ data: data }, resultWithoutPartial), this.obsQueryFields), { client: this.client, observable: this.observable, variables: this.observable.variables, called: !this.queryHookOptions.skip, previousData: this.previousData }));
|
331 | if (!queryResult.error && utilities.isNonEmptyArray(result.errors)) {
|
332 | queryResult.error = new errors.ApolloError({ graphQLErrors: result.errors });
|
333 | }
|
334 | return queryResult;
|
335 | };
|
336 | InternalState.prototype.unsafeHandlePartialRefetch = function (result) {
|
337 | if (result.partial &&
|
338 | this.queryHookOptions.partialRefetch &&
|
339 | !result.loading &&
|
340 | (!result.data || Object.keys(result.data).length === 0) &&
|
341 | this.observable.options.fetchPolicy !== 'cache-only') {
|
342 | Object.assign(result, {
|
343 | loading: true,
|
344 | networkStatus: core.NetworkStatus.refetch,
|
345 | });
|
346 | this.observable.refetch();
|
347 | }
|
348 | };
|
349 | return InternalState;
|
350 | }());
|
351 |
|
352 | var EAGER_METHODS = [
|
353 | 'refetch',
|
354 | 'reobserve',
|
355 | 'fetchMore',
|
356 | 'updateQuery',
|
357 | 'startPolling',
|
358 | 'subscribeToMore',
|
359 | ];
|
360 | function useLazyQuery(query, options) {
|
361 | var internalState = useInternalState(useApolloClient(options && options.client), query);
|
362 | var execOptionsRef = React.useRef();
|
363 | var merged = execOptionsRef.current
|
364 | ? utilities.mergeOptions(options, execOptionsRef.current)
|
365 | : options;
|
366 | var useQueryResult = internalState.useQuery(tslib.__assign(tslib.__assign({}, merged), { skip: !execOptionsRef.current }));
|
367 | var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
368 | internalState.getDefaultFetchPolicy();
|
369 | var result = Object.assign(useQueryResult, {
|
370 | called: !!execOptionsRef.current,
|
371 | });
|
372 | var eagerMethods = React.useMemo(function () {
|
373 | var eagerMethods = {};
|
374 | var _loop_1 = function (key) {
|
375 | var method = result[key];
|
376 | eagerMethods[key] = function () {
|
377 | if (!execOptionsRef.current) {
|
378 | execOptionsRef.current = Object.create(null);
|
379 | internalState.forceUpdate();
|
380 | }
|
381 | return method.apply(this, arguments);
|
382 | };
|
383 | };
|
384 | for (var _i = 0, EAGER_METHODS_1 = EAGER_METHODS; _i < EAGER_METHODS_1.length; _i++) {
|
385 | var key = EAGER_METHODS_1[_i];
|
386 | _loop_1(key);
|
387 | }
|
388 | return eagerMethods;
|
389 | }, []);
|
390 | Object.assign(result, eagerMethods);
|
391 | var execute = React.useCallback(function (executeOptions) {
|
392 | execOptionsRef.current = executeOptions ? tslib.__assign(tslib.__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
393 | fetchPolicy: initialFetchPolicy,
|
394 | };
|
395 | var promise = internalState
|
396 | .asyncUpdate()
|
397 | .then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
398 | promise.catch(function () { });
|
399 | return promise;
|
400 | }, []);
|
401 | return [execute, result];
|
402 | }
|
403 |
|
404 | function useMutation(mutation, options) {
|
405 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
406 | parser.verifyDocumentType(mutation, parser.DocumentType.Mutation);
|
407 | var _a = React.useState({
|
408 | called: false,
|
409 | loading: false,
|
410 | client: client,
|
411 | }), result = _a[0], setResult = _a[1];
|
412 | var ref = React.useRef({
|
413 | result: result,
|
414 | mutationId: 0,
|
415 | isMounted: true,
|
416 | client: client,
|
417 | mutation: mutation,
|
418 | options: options,
|
419 | });
|
420 | {
|
421 | Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
422 | }
|
423 | var execute = React.useCallback(function (executeOptions) {
|
424 | if (executeOptions === void 0) { executeOptions = {}; }
|
425 | var _a = ref.current, client = _a.client, options = _a.options, mutation = _a.mutation;
|
426 | var baseOptions = tslib.__assign(tslib.__assign({}, options), { mutation: mutation });
|
427 | if (!ref.current.result.loading && !baseOptions.ignoreResults) {
|
428 | setResult(ref.current.result = {
|
429 | loading: true,
|
430 | error: void 0,
|
431 | data: void 0,
|
432 | called: true,
|
433 | client: client,
|
434 | });
|
435 | }
|
436 | var mutationId = ++ref.current.mutationId;
|
437 | var clientOptions = core.mergeOptions(baseOptions, executeOptions);
|
438 | return client.mutate(clientOptions).then(function (response) {
|
439 | var _a, _b, _c;
|
440 | var data = response.data, errors$1 = response.errors;
|
441 | var error = errors$1 && errors$1.length > 0
|
442 | ? new errors.ApolloError({ graphQLErrors: errors$1 })
|
443 | : void 0;
|
444 | if (mutationId === ref.current.mutationId &&
|
445 | !clientOptions.ignoreResults) {
|
446 | var result_1 = {
|
447 | called: true,
|
448 | loading: false,
|
449 | data: data,
|
450 | error: error,
|
451 | client: client,
|
452 | };
|
453 | if (ref.current.isMounted && !equality.equal(ref.current.result, result_1)) {
|
454 | setResult(ref.current.result = result_1);
|
455 | }
|
456 | }
|
457 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, response.data);
|
458 | (_c = executeOptions.onCompleted) === null || _c === void 0 ? void 0 : _c.call(executeOptions, response.data);
|
459 | return response;
|
460 | }).catch(function (error) {
|
461 | var _a, _b, _c, _d;
|
462 | if (mutationId === ref.current.mutationId &&
|
463 | ref.current.isMounted) {
|
464 | var result_2 = {
|
465 | loading: false,
|
466 | error: error,
|
467 | data: void 0,
|
468 | called: true,
|
469 | client: client,
|
470 | };
|
471 | if (!equality.equal(ref.current.result, result_2)) {
|
472 | setResult(ref.current.result = result_2);
|
473 | }
|
474 | }
|
475 | if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) || clientOptions.onError) {
|
476 | (_c = (_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onError) === null || _c === void 0 ? void 0 : _c.call(_b, error);
|
477 | (_d = executeOptions.onError) === null || _d === void 0 ? void 0 : _d.call(executeOptions, error);
|
478 | return { data: void 0, errors: error };
|
479 | }
|
480 | throw error;
|
481 | });
|
482 | }, []);
|
483 | var reset = React.useCallback(function () {
|
484 | setResult({ called: false, loading: false, client: client });
|
485 | }, []);
|
486 | React.useEffect(function () {
|
487 | ref.current.isMounted = true;
|
488 | return function () {
|
489 | ref.current.isMounted = false;
|
490 | };
|
491 | }, []);
|
492 | return [execute, tslib.__assign({ reset: reset }, result)];
|
493 | }
|
494 |
|
495 | function useSubscription(subscription, options) {
|
496 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
497 | parser.verifyDocumentType(subscription, parser.DocumentType.Subscription);
|
498 | var _a = React.useState({
|
499 | loading: !(options === null || options === void 0 ? void 0 : options.skip),
|
500 | error: void 0,
|
501 | data: void 0,
|
502 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
503 | }), result = _a[0], setResult = _a[1];
|
504 | var _b = React.useState(function () {
|
505 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
506 | return null;
|
507 | }
|
508 | return client.subscribe({
|
509 | query: subscription,
|
510 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
511 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
512 | context: options === null || options === void 0 ? void 0 : options.context,
|
513 | });
|
514 | }), observable = _b[0], setObservable = _b[1];
|
515 | var canResetObservableRef = React.useRef(false);
|
516 | React.useEffect(function () {
|
517 | return function () {
|
518 | canResetObservableRef.current = true;
|
519 | };
|
520 | }, []);
|
521 | var ref = React.useRef({ client: client, subscription: subscription, options: options });
|
522 | React.useEffect(function () {
|
523 | var _a, _b, _c, _d;
|
524 | var shouldResubscribe = options === null || options === void 0 ? void 0 : options.shouldResubscribe;
|
525 | if (typeof shouldResubscribe === 'function') {
|
526 | shouldResubscribe = !!shouldResubscribe(options);
|
527 | }
|
528 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
529 | if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
|
530 | setResult({
|
531 | loading: false,
|
532 | data: void 0,
|
533 | error: void 0,
|
534 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
535 | });
|
536 | setObservable(null);
|
537 | canResetObservableRef.current = false;
|
538 | }
|
539 | }
|
540 | else if ((shouldResubscribe !== false &&
|
541 | (client !== ref.current.client ||
|
542 | subscription !== ref.current.subscription ||
|
543 | (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
544 | !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
|
545 | !equality.equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
|
546 | canResetObservableRef.current) {
|
547 | setResult({
|
548 | loading: true,
|
549 | data: void 0,
|
550 | error: void 0,
|
551 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
552 | });
|
553 | setObservable(client.subscribe({
|
554 | query: subscription,
|
555 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
556 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
557 | context: options === null || options === void 0 ? void 0 : options.context,
|
558 | }));
|
559 | canResetObservableRef.current = false;
|
560 | }
|
561 | Object.assign(ref.current, { client: client, subscription: subscription, options: options });
|
562 | }, [client, subscription, options, canResetObservableRef.current]);
|
563 | React.useEffect(function () {
|
564 | if (!observable) {
|
565 | return;
|
566 | }
|
567 | var subscription = observable.subscribe({
|
568 | next: function (fetchResult) {
|
569 | var _a, _b;
|
570 | var result = {
|
571 | loading: false,
|
572 | data: fetchResult.data,
|
573 | error: void 0,
|
574 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
575 | };
|
576 | setResult(result);
|
577 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionData) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
578 | client: client,
|
579 | subscriptionData: result
|
580 | });
|
581 | },
|
582 | error: function (error) {
|
583 | setResult({
|
584 | loading: false,
|
585 | data: void 0,
|
586 | error: error,
|
587 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
588 | });
|
589 | },
|
590 | complete: function () {
|
591 | var _a, _b;
|
592 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionComplete) === null || _b === void 0 ? void 0 : _b.call(_a);
|
593 | },
|
594 | });
|
595 | return function () {
|
596 | subscription.unsubscribe();
|
597 | };
|
598 | }, [observable]);
|
599 | return result;
|
600 | }
|
601 |
|
602 | function useReactiveVar(rv) {
|
603 | var value = rv();
|
604 | var setValue = React.useState(value)[1];
|
605 | React.useEffect(function () {
|
606 | var probablySameValue = rv();
|
607 | if (value !== probablySameValue) {
|
608 | setValue(probablySameValue);
|
609 | }
|
610 | else {
|
611 | return rv.onNextChange(setValue);
|
612 | }
|
613 | }, [value]);
|
614 | return value;
|
615 | }
|
616 |
|
617 | exports.useApolloClient = useApolloClient;
|
618 | exports.useLazyQuery = useLazyQuery;
|
619 | exports.useMutation = useMutation;
|
620 | exports.useQuery = useQuery;
|
621 | exports.useReactiveVar = useReactiveVar;
|
622 | exports.useSubscription = useSubscription;
|
623 |
|