feat(ts-sdk): add reconnect() method to DbConnectionImpl#4631
Closed
xkelxmc wants to merge 1 commit intoclockworklabs:masterfrom
Closed
feat(ts-sdk): add reconnect() method to DbConnectionImpl#4631xkelxmc wants to merge 1 commit intoclockworklabs:masterfrom
xkelxmc wants to merge 1 commit intoclockworklabs:masterfrom
Conversation
Add a reconnect() method that reopens the WebSocket on the same connection object, preserving the client cache. This allows applications to handle token refresh and network recovery without losing table data or causing UI flashes. Use case: long-lived browser tabs (e.g. games) where the WS may drop due to network changes, sleep, or token expiration.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
Adds a
reconnect(newToken?)method toDbConnectionImplthat reopens the WebSocket on the same connection object, preserving the client cache.Problem: Long-lived browser tabs (games, dashboards) lose their SpacetimeDB connection due to network changes, token expiration, or browser throttling. Currently the only recovery path is creating a new
DbConnection, which destroys the client cache — alluseTablehooks return[], causing UI flashes and loading screens.Solution:
reconnect()stores WS creation config in a privatewsConfigfield, silently closes the old WS (nulling handlers to prevent spurious events), then creates a new one.useTablehooks keep returning cached data while subscriptions re-establish.API and ABI breaking changes
None. This is a purely additive change — one new public method on
DbConnectionImpl.Expected complexity level and risk
1 — The change is small (74 lines) and self-contained. It reuses the existing WS creation pattern from the constructor. No existing behavior is modified.
Testing
connection.disconnect()thenconnection.reconnect()— WS reconnects andonConnectfiresuseTablehooks retain cached data during reconnectreconnect(newToken)uses the new token for authentication