Skip to content

fix(SorokitProvider): handle disconnect errors, re-init client on network switch - #417

Merged
k-deejah merged 1 commit into
Sorokit:mainfrom
KayProject:fix/provider-disconnect-network-reinit
Jul 29, 2026
Merged

fix(SorokitProvider): handle disconnect errors, re-init client on network switch#417
k-deejah merged 1 commit into
Sorokit:mainfrom
KayProject:fix/provider-disconnect-network-reinit

Conversation

@KayProject

Copy link
Copy Markdown
Contributor

Resolves #405

1. disconnectWallet no longer leaks an unhandled rejection

await client.wallet.disconnect() had no catch, so an adapter that throws (extension disconnected mid-session) escaped through the useCallback as an unhandled promise rejection.

It's now caught and reported via the existing reportError path, and the session is still torn down — the user asked to disconnect, so address, account, and balances are 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 network

After a successful switchNetwork, setNetwork(data) updated React state but the getClient() singleton still handed back a client pointed at the previous network — so getClient().transaction.submit() after a switch went to the old endpoint.

Two parts:

  • A successful switch re-initialises the singleton. The new optional createClientForNetwork?: (network: NetworkInfo) => SorokitClient prop builds the replacement client; without it, the current client is simply re-registered.
  • The provider now calls initClient(client) when its client prop settles or changes.

That second point is worth calling out: initClient was not called anywhere in src/ before this change, so getClient() 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 onNetworkChange prop

Fires after every successful switch with the new NetworkInfo, so consumers can clear caches or re-subscribe to feeds without watching network in 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 onError pattern from #353, so a parent passing fresh inline functions each render doesn't destabilise switchNetwork's identity.

Verification

npx vitest run src/context/SorokitProvider.test.tsx26 passed, including 7 new cases: disconnect failure surfaced + state still cleared, non-Error rejection fallback message, singleton wired on mount, singleton swapped via the factory on switch, singleton preserved without a factory, onNetworkChange fires on success, and does not fire on failure.

Test files Tests
main (5bc3678) 24 failed / 51 passed 61 failed / 945 passed
this branch 24 failed / 51 passed 61 failed / 952 passed

No regressions.

Heads-up on the build AC

npm run build / tsc -b do not pass on main independently of this change — FeeEstimator.tsx and SorobanPanel.tsx each 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.

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
@drips-wave

drips-wave Bot commented Jul 29, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@k-deejah
k-deejah merged commit 1a5a62f into Sorokit:main Jul 29, 2026
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(SorokitProvider): disconnect() throws uncaught, switchNetwork does not reinit getClient() singleton, no onNetworkChange callback

2 participants