Skip to content

fix: useSubscription calls onConnectionStateChange - #6797

Merged
KATT merged 2 commits into
trpc:mainfrom
darkobits:fix/call-on-connection-state-change
May 30, 2025
Merged

KATT merged 2 commits into
trpc:mainfrom
darkobits:fix/call-on-connection-state-change

Conversation

@darkobits

@darkobits darkobits commented May 30, 2025 •

Copy link
Copy Markdown
Contributor

Closes #6693

🎯 Changes

The useSubscription hook registers several event handlers on the subscription it creates. Where relevant, event handlers call the appropriate user-provided callback for a given event. This fix adds that call for the onConnectionStateChange event, ensuring that any user-provided callback is invoked as expected.

✅ Checklist

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

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of connection state change notifications by ensuring user-provided callbacks are triggered appropriately.
  • Tests

    • Added tests to verify that connection state change callbacks are called when expected.

@darkobits
darkobits requested a review from a team as a code owner May 30, 2025 01:53
@vercel

vercel Bot commented May 30, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 30, 2025 9:05pm

@coderabbitai

coderabbitai Bot commented May 30, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce a call to the user-provided onConnectionStateChange callback within the subscription's connection state handler in subscriptionOptions.ts. Additionally, a test is added to verify that this callback is invoked when the connection state changes in the useSubscription hook.

Changes

File(s) Change Summary
packages/tanstack-react-query/src/internals/subscriptionOptions.ts Added invocation of onConnectionStateChange callback in the subscription's connection state handler.
packages/tanstack-react-query/test/useSubscription.test.tsx Added a mock and assertion to verify onConnectionStateChange is called during connection state changes.

Sequence Diagram(s)

sequenceDiagram
    participant UserComponent
    participant useSubscription
    participant Subscription
    participant UserCallback

    UserComponent->>useSubscription: Provide onConnectionStateChange callback
    useSubscription->>Subscription: Setup connection state handler
    Subscription-->>useSubscription: Connection state changes
    useSubscription->>UserCallback: Call onConnectionStateChange(result)
    useSubscription->>useSubscription: Update internal state
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure useSubscription calls the user-provided onConnectionStateChange callback (#6693) ✅
Add test to verify onConnectionStateChange is invoked on connection state change (#6693) ✅

Suggested reviewers

  • KATT

Poem

In the warren where code flows free,
A callback now hops with glee.
Connection state change, once ignored,
Now gets the call it long implored.
With tests to prove the fix is right,
The burrow’s future is ever bright!
🐇✨


📜 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 0f67600 and 1d80192.

📒 Files selected for processing (2)
  • packages/tanstack-react-query/src/internals/subscriptionOptions.ts (1 hunks)
  • packages/tanstack-react-query/test/useSubscription.test.tsx (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: test
  • GitHub Check: build
  • GitHub Check: Lint and auto-fix
  • GitHub Check: Analyze (typescript)
🔇 Additional comments (4)
packages/tanstack-react-query/src/internals/subscriptionOptions.ts (1)

203-203: LGTM! Bug fix correctly implemented.

The addition of the user-provided onConnectionStateChange callback invocation follows the same pattern as other event handlers (onStarted, onData, onError) and fixes the missing callback invocation mentioned in issue #6693. The optional chaining operator ensures safe execution, and the placement before internal state updates is appropriate.

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

286-286: LGTM! Proper test setup for callback verification.

The mock function is correctly created to verify that the onConnectionStateChange callback is invoked.


299-299: LGTM! Callback correctly passed to subscription options.

The mock function is properly passed to the subscription options to enable testing of the callback invocation.


348-348: LGTM! Appropriate assertion for callback verification.

The assertion correctly verifies that the onConnectionStateChange callback is invoked when connection state changes are triggered by ctx.destroyConnections(). This directly tests the bug fix implemented in the subscription options.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

@vercel

vercel Bot commented May 30, 2025

Copy link
Copy Markdown

@darkobits is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

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

@trpc/next

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

@trpc/react-query

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

@trpc/server

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

@trpc/tanstack-react-query

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

@trpc/upgrade

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

commit: ca75082

@darkobits

Copy link
Copy Markdown
Contributor Author

Aside: This made me smile! 😄

In the warren where code flows free,
A callback now hops with glee.
Connection state change, once ignored,
Now gets the call it long implored.
With tests to prove the fix is right,
The burrow’s future is ever bright!
🐇✨

@KATT
KATT enabled auto-merge (squash) May 30, 2025 21:02
@github-actions

github-actions Bot commented Jun 1, 2025

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 Jun 1, 2025

This branch was successfully deployed

2 active deployments
Preview – www — ca75082e Deployed May 30, 2025 by vercel[bot]
Preview – trpc-next-app-dir — ca75082e Deployed May 30, 2025 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: useSubscription never calls onConnectionStateChange

2 participants