Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useSubscription restart was not cached #12044

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
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
Loading