1 | import "../../utilities/globals/index.js";
|
2 | import { useState, useRef, useEffect } from 'react';
|
3 | import { equal } from '@wry/equality';
|
4 | import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
5 | import { useApolloClient } from "./useApolloClient.js";
|
6 | export function useSubscription(subscription, options) {
|
7 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
8 | verifyDocumentType(subscription, DocumentType.Subscription);
|
9 | var _a = useState({
|
10 | loading: !(options === null || options === void 0 ? void 0 : options.skip),
|
11 | error: void 0,
|
12 | data: void 0,
|
13 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
14 | }), result = _a[0], setResult = _a[1];
|
15 | var _b = useState(function () {
|
16 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
17 | return null;
|
18 | }
|
19 | return client.subscribe({
|
20 | query: subscription,
|
21 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
22 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
23 | context: options === null || options === void 0 ? void 0 : options.context,
|
24 | });
|
25 | }), observable = _b[0], setObservable = _b[1];
|
26 | var canResetObservableRef = useRef(false);
|
27 | useEffect(function () {
|
28 | return function () {
|
29 | canResetObservableRef.current = true;
|
30 | };
|
31 | }, []);
|
32 | var ref = useRef({ client: client, subscription: subscription, options: options });
|
33 | useEffect(function () {
|
34 | var _a, _b, _c, _d;
|
35 | var shouldResubscribe = options === null || options === void 0 ? void 0 : options.shouldResubscribe;
|
36 | if (typeof shouldResubscribe === 'function') {
|
37 | shouldResubscribe = !!shouldResubscribe(options);
|
38 | }
|
39 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
40 | if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
|
41 | setResult({
|
42 | loading: false,
|
43 | data: void 0,
|
44 | error: void 0,
|
45 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
46 | });
|
47 | setObservable(null);
|
48 | canResetObservableRef.current = false;
|
49 | }
|
50 | }
|
51 | else if ((shouldResubscribe !== false &&
|
52 | (client !== ref.current.client ||
|
53 | subscription !== ref.current.subscription ||
|
54 | (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
55 | !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
|
56 | !equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
|
57 | canResetObservableRef.current) {
|
58 | setResult({
|
59 | loading: true,
|
60 | data: void 0,
|
61 | error: void 0,
|
62 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
63 | });
|
64 | setObservable(client.subscribe({
|
65 | query: subscription,
|
66 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
67 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
68 | context: options === null || options === void 0 ? void 0 : options.context,
|
69 | }));
|
70 | canResetObservableRef.current = false;
|
71 | }
|
72 | Object.assign(ref.current, { client: client, subscription: subscription, options: options });
|
73 | }, [client, subscription, options, canResetObservableRef.current]);
|
74 | useEffect(function () {
|
75 | if (!observable) {
|
76 | return;
|
77 | }
|
78 | var subscription = observable.subscribe({
|
79 | next: function (fetchResult) {
|
80 | var _a, _b;
|
81 | var result = {
|
82 | loading: false,
|
83 | data: fetchResult.data,
|
84 | error: void 0,
|
85 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
86 | };
|
87 | setResult(result);
|
88 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionData) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
89 | client: client,
|
90 | subscriptionData: result
|
91 | });
|
92 | },
|
93 | error: function (error) {
|
94 | setResult({
|
95 | loading: false,
|
96 | data: void 0,
|
97 | error: error,
|
98 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
99 | });
|
100 | },
|
101 | complete: function () {
|
102 | var _a, _b;
|
103 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionComplete) === null || _b === void 0 ? void 0 : _b.call(_a);
|
104 | },
|
105 | });
|
106 | return function () {
|
107 | subscription.unsubscribe();
|
108 | };
|
109 | }, [observable]);
|
110 | return result;
|
111 | }
|
112 |
|
\ | No newline at end of file |