1 | import "../../utilities/globals/index.js";
|
2 | import { useState, useRef, useEffect } from 'react';
|
3 | import { invariant } from "../../utilities/globals/index.js";
|
4 | import { equal } from '@wry/equality';
|
5 | import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
6 | import { useApolloClient } from "./useApolloClient.js";
|
7 | export function useSubscription(subscription, options) {
|
8 | var hasIssuedDeprecationWarningRef = useRef(false);
|
9 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
10 | verifyDocumentType(subscription, DocumentType.Subscription);
|
11 | var _a = useState({
|
12 | loading: !(options === null || options === void 0 ? void 0 : options.skip),
|
13 | error: void 0,
|
14 | data: void 0,
|
15 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
16 | }), result = _a[0], setResult = _a[1];
|
17 | if (!hasIssuedDeprecationWarningRef.current) {
|
18 | hasIssuedDeprecationWarningRef.current = true;
|
19 | if (options === null || options === void 0 ? void 0 : options.onSubscriptionData) {
|
20 | __DEV__ && invariant.warn(options.onData
|
21 | ? "'useSubscription' supports only the 'onSubscriptionData' or 'onData' option, but not both. Only the 'onData' option will be used."
|
22 | : "'onSubscriptionData' is deprecated and will be removed in a future major version. Please use the 'onData' option instead.");
|
23 | }
|
24 | if (options === null || options === void 0 ? void 0 : options.onSubscriptionComplete) {
|
25 | __DEV__ && invariant.warn(options.onComplete
|
26 | ? "'useSubscription' supports only the 'onSubscriptionComplete' or 'onComplete' option, but not both. Only the 'onComplete' option will be used."
|
27 | : "'onSubscriptionComplete' is deprecated and will be removed in a future major version. Please use the 'onComplete' option instead.");
|
28 | }
|
29 | }
|
30 | var _b = useState(function () {
|
31 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
32 | return null;
|
33 | }
|
34 | return client.subscribe({
|
35 | query: subscription,
|
36 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
37 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
38 | context: options === null || options === void 0 ? void 0 : options.context,
|
39 | });
|
40 | }), observable = _b[0], setObservable = _b[1];
|
41 | var canResetObservableRef = useRef(false);
|
42 | useEffect(function () {
|
43 | return function () {
|
44 | canResetObservableRef.current = true;
|
45 | };
|
46 | }, []);
|
47 | var ref = useRef({ client: client, subscription: subscription, options: options });
|
48 | useEffect(function () {
|
49 | var _a, _b, _c, _d;
|
50 | var shouldResubscribe = options === null || options === void 0 ? void 0 : options.shouldResubscribe;
|
51 | if (typeof shouldResubscribe === 'function') {
|
52 | shouldResubscribe = !!shouldResubscribe(options);
|
53 | }
|
54 | if (options === null || options === void 0 ? void 0 : options.skip) {
|
55 | if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
|
56 | setResult({
|
57 | loading: false,
|
58 | data: void 0,
|
59 | error: void 0,
|
60 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
61 | });
|
62 | setObservable(null);
|
63 | canResetObservableRef.current = false;
|
64 | }
|
65 | }
|
66 | else if ((shouldResubscribe !== false &&
|
67 | (client !== ref.current.client ||
|
68 | subscription !== ref.current.subscription ||
|
69 | (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
70 | !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
|
71 | !equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
|
72 | canResetObservableRef.current) {
|
73 | setResult({
|
74 | loading: true,
|
75 | data: void 0,
|
76 | error: void 0,
|
77 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
78 | });
|
79 | setObservable(client.subscribe({
|
80 | query: subscription,
|
81 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
82 | fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
83 | context: options === null || options === void 0 ? void 0 : options.context,
|
84 | }));
|
85 | canResetObservableRef.current = false;
|
86 | }
|
87 | Object.assign(ref.current, { client: client, subscription: subscription, options: options });
|
88 | }, [client, subscription, options, canResetObservableRef.current]);
|
89 | useEffect(function () {
|
90 | if (!observable) {
|
91 | return;
|
92 | }
|
93 | var subscriptionStopped = false;
|
94 | var subscription = observable.subscribe({
|
95 | next: function (fetchResult) {
|
96 | var _a, _b;
|
97 | if (subscriptionStopped) {
|
98 | return;
|
99 | }
|
100 | var result = {
|
101 | loading: false,
|
102 | data: fetchResult.data,
|
103 | error: void 0,
|
104 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
105 | };
|
106 | setResult(result);
|
107 | if ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onData) {
|
108 | ref.current.options.onData({
|
109 | client: client,
|
110 | data: result
|
111 | });
|
112 | }
|
113 | else if ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onSubscriptionData) {
|
114 | ref.current.options.onSubscriptionData({
|
115 | client: client,
|
116 | subscriptionData: result
|
117 | });
|
118 | }
|
119 | },
|
120 | error: function (error) {
|
121 | var _a, _b;
|
122 | if (!subscriptionStopped) {
|
123 | setResult({
|
124 | loading: false,
|
125 | data: void 0,
|
126 | error: error,
|
127 | variables: options === null || options === void 0 ? void 0 : options.variables,
|
128 | });
|
129 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
|
130 | }
|
131 | ;
|
132 | },
|
133 | complete: function () {
|
134 | var _a, _b;
|
135 | if (!subscriptionStopped) {
|
136 | if ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onComplete) {
|
137 | ref.current.options.onComplete();
|
138 | }
|
139 | else if ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onSubscriptionComplete) {
|
140 | ref.current.options.onSubscriptionComplete();
|
141 | }
|
142 | }
|
143 | },
|
144 | });
|
145 | return function () {
|
146 | subscriptionStopped = true;
|
147 | setTimeout(function () {
|
148 | subscription.unsubscribe();
|
149 | });
|
150 | };
|
151 | }, [observable]);
|
152 | return result;
|
153 | }
|
154 |
|
\ | No newline at end of file |