Skip to content

useSubscription should abort when input changes - #6770

Merged
Nick-Lucas merged 4 commits into
mainfrom
use-subscription-abort
May 21, 2025
Merged

Nick-Lucas merged 4 commits into
mainfrom
use-subscription-abort

Conversation

@Nick-Lucas

@Nick-Lucas Nick-Lucas commented May 16, 2025 •

Copy link
Copy Markdown
Contributor

Closes #

🎯 Changes

What changes are made in this PR? Is it a feature or a bug fix?

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Tests
    • Added a new test to verify that subscriptions are properly aborted when restarted or unmounted.
    • Introduced a helper to facilitate rerendering components with consistent context during testing.
    • Improved test utilities to track and assert subscription abort states.

@coderabbitai

coderabbitai Bot commented May 16, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

A new helper function, rerenderApp, is introduced to the test helpers for React Query, enabling rerendering of test components with consistent context. In the useSubscription test suite, a new test verifies that subscriptions are properly aborted when inputs change or components unmount, with supporting changes to test utilities and context tracking.

Changes

File(s) Change Summary
packages/tanstack-react-query/test/__helpers.tsx Added rerenderApp helper to rerender test components with correct providers; updated exports and imports accordingly.
packages/tanstack-react-query/test/useSubscription.test.tsx Added abortState tracking to test context, updated subscription logic to detect aborts, and introduced a new test for abort behavior on subscription restarts and unmounts.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Case
    participant Component as SubscriptionComponent
    participant Helpers as Test Helpers
    participant Providers as Query/TRPC Providers
    participant AppRouter as Subscription Logic

    Test->>Helpers: renderApp(ui)
    Helpers->>Providers: Wrap ui with Providers
    Providers->>Component: Render with input 1
    Component->>AppRouter: Start subscription (input 1)
    AppRouter-->>Component: Send data
    Test->>Helpers: rerenderApp(renderResult, ui with input 2)
    Helpers->>Providers: Rerender ui with Providers
    Providers->>Component: Render with input 2
    Component->>AppRouter: Abort subscription (input 1)
    AppRouter->>Helpers: Mark abortState[input 1] = 'aborted'
    Component->>AppRouter: Start subscription (input 2)
    Test->>Helpers: Unmount component
    Component->>AppRouter: Abort subscription (input 2)
    AppRouter->>Helpers: Mark abortState[input 2] = 'aborted'
Loading

Poem

In the warren of tests, a helper appears,
Rerendering bunnies now hop without fears.
Subscriptions are watched, their aborts tracked with care,
When inputs are swapped, old tunnels beware!
With each rerender, the garden stays brightβ€”
Hopping through contexts, everything’s right.
πŸ‡βœ¨

Note

⚑️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚑️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boostβ€”your workflow just got faster.


πŸ“œ Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between a6f2917 and 8aa8a53.

πŸ“’ Files selected for processing (1)
  • packages/tanstack-react-query/test/useSubscription.test.tsx (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/tanstack-react-query/test/useSubscription.test.tsx
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: E2E-tests (nuxt)
  • GitHub Check: E2E-tests (minimal-react)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (cloudflare-workers)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: E2E-tests (.test/ssg)
  • GitHub Check: E2E-tests (express-minimal)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: Release using pkg.pr.new
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: test
  • GitHub Check: Analyze (typescript)
✨ Finishing Touches
  • πŸ“ Generate Docstrings

πŸͺ§ Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@railway-app
railway-app Bot temporarily deployed to trpc-pr-6770 May 16, 2025 09:54 Destroyed
@railway-app
railway-app Bot temporarily deployed to trpc-pr-6770 May 16, 2025 09:54 Destroyed
@railway-app

railway-app Bot commented May 16, 2025 •

Copy link
Copy Markdown

πŸš… Deployed to the trpc-pr-6770 environment in trpc-sse-and-websockets

Service Status Web Updated (UTC)
next-sse-chat ◻️ Removed (View Logs) Web May 21, 2025 at 7:33 pm
next-prisma-websockets-starter ◻️ Removed (View Logs) May 21, 2025 at 7:33 pm

@pkg-pr-new

pkg-pr-new Bot commented May 16, 2025 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6770

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6770

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6770

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6770

@trpc/tanstack-react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/tanstack-react-query@6770

@trpc/upgrade

npm i https://pkg.pr.new/trpc/trpc/@trpc/upgrade@6770

commit: c729308

@railway-app
railway-app Bot temporarily deployed to trpc-pr-6770 May 16, 2025 09:57 Destroyed
@railway-app
railway-app Bot temporarily deployed to trpc-pr-6770 May 16, 2025 09:57 Destroyed
@vercel

vercel Bot commented May 16, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
next-prisma-starter βœ… Ready (Inspect) Visit Preview May 18, 2025 8:29am
og-image βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 18, 2025 8:29am
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 18, 2025 8:29am

@vercel

vercel Bot commented May 16, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
og-image πŸ”„ Building (Inspect) Visit Preview πŸ’¬ Add feedback May 16, 2025 9:57am

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/tanstack-react-query/test/__helpers.tsx (1)

6-6: Update the import to use type-only imports.

The RenderResult is only used as a type, so it should use TypeScript's type-only import syntax.

-import { render, RenderResult } from '@testing-library/react';
+import { render } from '@testing-library/react';
+import type { RenderResult } from '@testing-library/react';
🧰 Tools
πŸͺ› ESLint

[error] 6-6: Imports "RenderResult" are only used as type.

(@typescript-eslint/consistent-type-imports)

packages/tanstack-react-query/test/useSubscription.test.tsx (1)

4-4: Remove unused import.

The input import from @testing-library/user-event/dist/types/event is not used anywhere in the code.

-import { input } from '@testing-library/user-event/dist/types/event';
πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 3775275 and a6f2917.

πŸ“’ Files selected for processing (2)
  • packages/tanstack-react-query/test/__helpers.tsx (2 hunks)
  • packages/tanstack-react-query/test/useSubscription.test.tsx (5 hunks)
🧰 Additional context used
πŸͺ› ESLint
packages/tanstack-react-query/test/__helpers.tsx

[error] 6-6: Imports "RenderResult" are only used as type.

(@typescript-eslint/consistent-type-imports)

⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: E2E-tests (nuxt)
  • GitHub Check: E2E-tests (express-server)
  • GitHub Check: E2E-tests (express-minimal)
  • GitHub Check: E2E-tests (.test/diagnostics-big-router)
  • GitHub Check: E2E-tests (.test/ssg)
  • GitHub Check: E2E-tests (fastify-server)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: Release using pkg.pr.new
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
  • GitHub Check: test
  • GitHub Check: Analyze (typescript)
πŸ”‡ Additional comments (7)
packages/tanstack-react-query/test/__helpers.tsx (2)

43-51: LGTM! The new rerenderApp function is a useful addition.

The implementation correctly maintains the same context providers when rerendering components, which ensures consistent client and context usage during tests.


58-58: LGTM! Correct addition to the return object.

The rerenderApp function is properly exported in the return object, making it available for tests.

packages/tanstack-react-query/test/useSubscription.test.tsx (5)

9-9: LGTM! Necessary import for abort handling.

This import is needed for the new abort error detection in the subscription handler.


62-63: LGTM! Good approach to track aborted subscriptions.

Using an object to track the abort state by subscription input is a clean approach and allows for precise verification in tests.


80-92: LGTM! Proper handling of abort errors.

The try-catch block correctly identifies abort errors and updates the abort state, without swallowing other errors. This allows the test to verify that subscriptions are properly aborted when inputs change.


131-131: LGTM! Exposing abort state for test verification.

Returning the abort state makes it accessible for assertions in the tests.


396-468: LGTM! Comprehensive test for subscription abort behavior.

This test thoroughly verifies two important behaviors:

  1. When a subscription's input changes, the previous subscription is properly aborted
  2. When a component unmounts, all active subscriptions are aborted

The test uses the new rerenderApp helper appropriately to trigger the input change scenario.

KATT
KATT previously approved these changes May 16, 2025
@railway-app
railway-app Bot temporarily deployed to trpc-pr-6770 May 17, 2025 23:47 Destroyed
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 23, 2025

This branch was successfully deployed

5 active deployments
Preview – www β€” c729308e Deployed May 18, 2025 by vercel[bot]
Preview – examples-todomvc β€” c729308e Deployed May 18, 2025 by vercel[bot]
Preview – og-image β€” c729308e Deployed May 18, 2025 by vercel[bot]
Preview – next-prisma-starter β€” c729308e Deployed May 18, 2025 by vercel[bot]
Preview – trpc-next-app-dir β€” c729308e Deployed May 18, 2025 by vercel[bot]
trpc-pr-6770 β€” c729308e Deployed May 18, 2025 by railway-app[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants