electron-trpc icon indicating copy to clipboard operation
electron-trpc copied to clipboard

Reasoning behind `Clean up subscriptions on navigation`

Open dastein1 opened this issue 1 year ago • 3 comments

I'm using mat-sz:trpc-11's fork in conjunction with trpc@11 in a demo project. PR #165 closes all subscriptions on navigation. For me that closes too many subscriptions on navigation and has the problem that the useSubscription() mechanism doesn't work anymore on those closed subscriptions. What's the reasoning behind that?

dastein1 avatar Jun 24 '24 13:06 dastein1

Subscriptions should be closed when the client closes the subscription, or when the client disconnects.

The problem is that the connection mechanism for electron isn't like browser connections, where when the window hard-navigates a websocket connection is closed, allowing the server to run cleanup.

We attempt to detect when a window hard-navigates so that the server can run subscription cleanup when hard navigation happens.

jsonnull avatar Jun 25 '24 16:06 jsonnull

Thanks. I do understand that, but I think that the assumption that a component has to unmount on route change is not true. For those components that do not remount it may completely break the subscriptions as the server then runs some cleanup and useSubscription() is not going to re-establish that connection. Could we make that configurable?

dastein1 avatar Jun 26 '24 06:06 dastein1

I think that the assumption that a component has to unmount on route change is not true

We don't enforce component mounting/unmounting on route change, that's all tRPC.

What we do is listen for window navigation events and manually close subscriptions when we know the page will be dropped. Note that components don't unmount on navigation, as the in-page JavaScript context is no longer executing.

I'm looking at the docs, though, and we're using the wrong event-listener for our navigation cleanup, causing this to fire on in-page navigations when it should only be firing for hard navigation. So that's a bug we'll have to address.

jsonnull avatar Jun 26 '24 12:06 jsonnull

When will it gonna be fixed?

reitowo avatar Dec 01 '24 11:12 reitowo

Fix for this is out in [email protected]

jsonnull avatar Dec 07 '24 22:12 jsonnull