Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Guarantee `Concast` cleanup without `Observable cancelled prematurely` rejection, potentially solving long-standing issues involving that error. <br/>
[@benjamn](https://github.com/benjamn) in [#9701](https://github.com/apollographql/apollo-client/pull/9701)

- Ensure `useSubscription` subscriptions are properly restarted after unmounting/remounting by React 18 in `<StrictMode>`. <br/>
[@benjamn](https://github.com/benjamn) in [#9707](https://github.com/apollographql/apollo-client/pull/9707)

### Improvements

- Internalize `useSyncExternalStore` shim, for more control than `use-sync-external-store` provides, fixing some React Native issues. <br/>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.min.cjs",
"maxSize": "29.45kB"
"maxSize": "29.5kB"
}
],
"engines": {
Expand Down
27 changes: 18 additions & 9 deletions src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
});
});

const canResetObservableRef = useRef(false);
useEffect(() => {
return () => {
canResetObservableRef.current = true;
};
}, []);

const ref = useRef({ client, subscription, options });
useEffect(() => {
let shouldResubscribe = options?.shouldResubscribe;
Expand All @@ -46,23 +53,24 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
}

if (options?.skip) {
if (!options?.skip !== !ref.current.options?.skip) {
if (!options?.skip !== !ref.current.options?.skip || canResetObservableRef.current) {
setResult({
loading: false,
data: void 0,
error: void 0,
variables: options?.variables,
});
setObservable(null);
canResetObservableRef.current = false;
}
} else if (
shouldResubscribe !== false && (
client !== ref.current.client ||
subscription !== ref.current.subscription ||
options?.fetchPolicy !== ref.current.options?.fetchPolicy ||
!options?.skip !== !ref.current.options?.skip ||
!equal(options?.variables, ref.current.options?.variables)
)
(shouldResubscribe !== false &&
(client !== ref.current.client ||
subscription !== ref.current.subscription ||
options?.fetchPolicy !== ref.current.options?.fetchPolicy ||
!options?.skip !== !ref.current.options?.skip ||
!equal(options?.variables, ref.current.options?.variables))) ||
canResetObservableRef.current
) {
setResult({
loading: true,
Expand All @@ -76,10 +84,11 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
fetchPolicy: options?.fetchPolicy,
context: options?.context,
}));
canResetObservableRef.current = false;
}

Object.assign(ref.current, { client, subscription, options });
}, [client, subscription, options]);
}, [client, subscription, options, canResetObservableRef.current]);

useEffect(() => {
if (!observable) {
Expand Down