anza-xyz / wallet-adapter

Modular TypeScript wallet adapters and components for Solana applications.
https://anza-xyz.github.io/wallet-adapter/
Apache License 2.0
1.55k stars 944 forks source link

Prevents multiple bindings of the disconnect event handler #866

Closed lucia-w closed 9 months ago

lucia-w commented 9 months ago

Issue: if the wallet emits the disconnect event more than once, the _disconnected method will be added multiple times as an event listener. This might lead to unexpected issues.

Fix: Added _onDisconnectBound to store the bound _disconnected method. In the connect method, the _disconnected method is bound and set to _onDisconnectBound only if it hasn't been set already. This prevents multiple bindings of the same event handler.

jordaaash commented 9 months ago

Thanks for the PR. I'm not sure I understand the sequence of events here though.

Generally, we add one listener on connect, and when the wallet emits the disconnect event, or we disconnect it ourselves, the first thing we do is remove the event listener for that event, and then proceed to tear down the adapter's internal state. Since this occurs synchronously, it shouldn't be possible for this event to be handled multiple times.

Can you explain in what conditions you're seeing this issue?