Skip to content

Subscription connection state - #5886

Closed
BeBoRE wants to merge 35 commits into
trpc:nextfrom
BeBoRE:subscription-connection-state
Closed

BeBoRE wants to merge 35 commits into
trpc:nextfrom
BeBoRE:subscription-connection-state

Conversation

@BeBoRE

@BeBoRE BeBoRE commented Jul 9, 2024 •

Copy link
Copy Markdown
Contributor

Closes #5871
Closes #5786

🎯 Changes

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

Lets useSubscription return a result containing the state of the connection, most recently received data (useful for myself), and utilities to better recover from errors.

✅ 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.
  • Verify old state callbacks (onStarted, onError) still function the same
  • Verify there are no unnecessary re-renders
  • Remove debug console logs

📋 Tasks

  • Extend what the SubscriptionObservable can emit (connection state)
  • Implement the specification as described in the UseSubscriptionResult typedef
  • Extend httpSubscriptionLink to conform to new observable events
  • Make state only change when watched properties change
  • Extend wsLInk to conform to new observable events

@vercel

vercel Bot commented Jul 9, 2024 •

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 Jul 21, 2024 10:59am

@vercel

vercel Bot commented Jul 9, 2024

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

},
onData: (data) => {
if (!isStopped) {
setSubscriptionState((prev) => ({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure we wanna update data for every event - it can become incredibly expensive for high throughput events, likely better to force developers to handle data themselves

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we update data of every event, we need to have a smart handler that won't update data unless it's used - not looked at the magic that RQ does for that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I probably agree, I'll reconsider, removing it for now

@BeBoRE BeBoRE Jul 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Re-added this :) now only rerenders if that property is watched

@BeBoRE

BeBoRE commented Jul 10, 2024

Copy link
Copy Markdown
Contributor Author

I have a few points to think on:

  1. What does the error state mean? Does it include that the connection has been interrupted or does it only mean the subscription has completely failed? If connection interruptions don't count as errors how should this be distinguished.
  2. What does stopped mean? Is this when the subscription has completely failed. Or does it just mean that the subscription isn't running due to the subscription having ended succesfully or the subscription ending due to an exception.
  3. What does completed mean? Is this when the subscription has ended succesfully?
  4. You mentioned adding a retry callback, what should the behaviour of the retry be if the subscription is still running or reconnecting?
  5. Should we add an end callback aswell to end the subscription, or should this be handled by the enabled option?
  6. (How) should we distinguish connecting vs reconnecting vs retry?

@BeBoRE

BeBoRE commented Jul 10, 2024

Copy link
Copy Markdown
Contributor Author

I changed the result type of the hook, to reflect how I think the hook should work. It's loosely based on TanStack Query, I added comments explaining what the properties should reflect.

I personally dislike the error state since it's very ambigious in this context, failed is less ambigious since it tells you that the subscription has completely failed, which is what I believe error would indicate.

I have added a restart callback. My idea is that this can be called at any time, even while the subscription is not stopped. A 'retry' callback would imply that it can only be called on failure, or would this be more desired?

@BeBoRE

BeBoRE commented Jul 14, 2024

Copy link
Copy Markdown
Contributor Author

I extended the wsLink with the new connection state events, very crudely needs a lot more testing. It seems that when using WebSockets the browser doesn't recognize connection issues for a long time. The starting connecting state and pending state work quite well however.

@BeBoRE

BeBoRE commented Jul 16, 2024

Copy link
Copy Markdown
Contributor Author

I believe the only thing missing now are tests, unless there needs to be more changes to the specifications.

@github-actions

github-actions Bot commented Aug 3, 2024

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 Aug 3, 2024

This branch was successfully deployed

3 active deployments
Preview – examples-todomvc — db7421a2 Deployed Jul 22, 2024 by vercel[bot]
Preview – www — db7421a2 Deployed Jul 21, 2024 by vercel[bot]
Preview – trpc-next-app-dir — db7421a2 Deployed Jul 21, 2024 by vercel[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.

feat: access subscription connection status from tRPC client feat: Make useSubscription return info about the subscription

2 participants