fix(SorokitProvider): handle disconnect errors, re-init client on network switch - #417
Merged
k-deejah merged 1 commit intoJul 29, 2026
Conversation
Three related gaps around teardown and network switching. - `disconnectWallet` no longer lets a throwing wallet adapter escape as an unhandled rejection. The failure is reported through `onError`/`error` after the session state is cleared, so the report survives the reset. - A successful `switchNetwork` now re-initialises the `getClient()` singleton, so callers reaching for it after a switch no longer hit the previous network's endpoints. The optional `createClientForNetwork` prop builds the replacement; without it the current client is re-registered. The provider also registers its client on mount, which nothing did before. - New `onNetworkChange` prop fires after every successful switch, so consumers can clear caches or re-subscribe without watching internal state. Resolves Sorokit#405
|
@KayProject Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Resolves #405
1.
disconnectWalletno longer leaks an unhandled rejectionawait client.wallet.disconnect()had nocatch, so an adapter that throws (extension disconnected mid-session) escaped through theuseCallbackas an unhandled promise rejection.It's now caught and reported via the existing
reportErrorpath, and the session is still torn down — the user asked to disconnect, soaddress,account, andbalancesare cleared regardless.One ordering detail worth flagging: the report is deliberately emitted after
setErrorHistory([]). Reporting first meant the fresh-session history reset immediately wiped the very error we'd just recorded.2.
getClient()now reflects the current networkAfter a successful
switchNetwork,setNetwork(data)updated React state but thegetClient()singleton still handed back a client pointed at the previous network — sogetClient().transaction.submit()after a switch went to the old endpoint.Two parts:
createClientForNetwork?: (network: NetworkInfo) => SorokitClientprop builds the replacement client; without it, the current client is simply re-registered.initClient(client)when itsclientprop settles or changes.That second point is worth calling out:
initClientwas not called anywhere insrc/before this change, sogetClient()would throw"Client not initialized"for any component that reached for it outside of tests. The provider registering its own client closes that gap.3. New
onNetworkChangepropFires after every successful switch with the new
NetworkInfo, so consumers can clear caches or re-subscribe to feeds without watchingnetworkin an effect and depending on internal state shape. It does not fire when the switch fails.Both new callbacks are held in refs, matching the existing
onErrorpattern from #353, so a parent passing fresh inline functions each render doesn't destabiliseswitchNetwork's identity.Verification
npx vitest run src/context/SorokitProvider.test.tsx— 26 passed, including 7 new cases: disconnect failure surfaced + state still cleared, non-Errorrejection fallback message, singleton wired on mount, singleton swapped via the factory on switch, singleton preserved without a factory,onNetworkChangefires on success, and does not fire on failure.main(5bc3678)No regressions.
Heads-up on the build AC
npm run build/tsc -bdo not pass onmainindependently of this change —FeeEstimator.tsxandSorobanPanel.tsxeach have an unclosed block from a merge, which is also what the 61 pre-existing test failures are. Glad to send a separate PR repairing those.ESLint clean on all changed files.