Skip to content

Commit

Permalink
Fix useSubscription restart was not cached (#12044)
Browse files Browse the repository at this point in the history
Co-authored-by: Lenz Weber-Tronic <[email protected]>
  • Loading branch information
DoctorJohn and phryneas authored Sep 3, 2024
1 parent a67727e commit 04462a2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .api-reports/api-report-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ export interface UseReadQueryResult<TData = unknown> {

// @public
export function useSubscription<TData = any, TVariables extends OperationVariables = OperationVariables>(subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SubscriptionHookOptions<NoInfer_2<TData>, NoInfer_2<TVariables>>): {
restart(): void;
restart: () => void;
loading: boolean;
data?: TData | undefined;
error?: ApolloError;
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_hooks.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,7 @@ export interface UseReadQueryResult<TData = unknown> {
//
// @public
export function useSubscription<TData = any, TVariables extends OperationVariables = OperationVariables>(subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SubscriptionHookOptions<NoInfer_2<TData>, NoInfer_2<TVariables>>): {
restart(): void;
restart: () => void;
loading: boolean;
data?: TData | undefined;
error?: ApolloError;
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ export interface UseReadQueryResult<TData = unknown> {

// @public
export function useSubscription<TData = any, TVariables extends OperationVariables = OperationVariables>(subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SubscriptionHookOptions<NoInfer_2<TData>, NoInfer_2<TVariables>>): {
restart(): void;
restart: () => void;
loading: boolean;
data?: TData | undefined;
error?: ApolloError;
Expand Down
5 changes: 5 additions & 0 deletions .changeset/old-ghosts-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Cache the `useSubscription` hook's `restart` function definition between re-renders.
2 changes: 1 addition & 1 deletion .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 40242,
"dist/apollo-client.min.cjs": 40249,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 33058
}
23 changes: 10 additions & 13 deletions src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,16 @@ export function useSubscription<
: fallbackResult,
() => fallbackResult
);
return React.useMemo(
() => ({
...ret,
restart() {
invariant(
!optionsRef.current.skip,
"A subscription that is skipped cannot be restarted."
);
setObservable(recreateRef.current());
},
}),
[ret]
);

const restart = React.useCallback(() => {
invariant(
!optionsRef.current.skip,
"A subscription that is skipped cannot be restarted."
);
setObservable(recreateRef.current());
}, [optionsRef, recreateRef]);

return React.useMemo(() => ({ ...ret, restart }), [ret, restart]);
}

function createSubscription<
Expand Down

0 comments on commit 04462a2

Please sign in to comment.