Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: listener callback should bind this #37

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/appkit-universal/appKitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class WalletConnectModal extends Web3ModalScaffold {
private universalProvider: Awaited<ReturnType<(typeof UniversalProvider)['init']>>
private requestedScope: string
private requestedNamespaces: Exclude<ConnectParams['optionalNamespaces'], undefined>
private _onSyncAccount: WalletConnectModal['syncAccount'] | undefined

public constructor(options: Web3ModalClientOptions) {
const { universalProvider, namespaces, ...w3mOptions } = options
Expand Down Expand Up @@ -104,8 +105,10 @@ export class WalletConnectModal extends Web3ModalScaffold {
])
this.syncAccount()
this.syncNetwork()
universalProvider.client.on('session_update', this.syncAccount)
universalProvider.client.on('session_delete', this.syncAccount)

this._onSyncAccount = this.syncAccount.bind(this)
universalProvider.client.on('session_update', this._onSyncAccount)
universalProvider.client.on('session_delete', this._onSyncAccount)
}

async disconnect() {
Expand Down
8 changes: 6 additions & 2 deletions packages/walletconnect-solana/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class WalletConnectWalletAdapter extends BaseSignerWalletAdapter {
private _readyState: WalletReadyState =
typeof window === 'undefined' ? WalletReadyState.Unsupported : WalletReadyState.Loadable

private _onDisconnect: WalletConnectWalletAdapter['disconnect'] | undefined

constructor(config: WalletConnectWalletAdapterConfig) {
super()

Expand All @@ -52,6 +54,8 @@ export class WalletConnectWalletAdapter extends BaseSignerWalletAdapter {
: WalletConnectChainID.Devnet,
options: this._config.options,
})

this._onDisconnect = this.disconnect.bind(this)
}

get publicKey() {
Expand All @@ -76,7 +80,7 @@ export class WalletConnectWalletAdapter extends BaseSignerWalletAdapter {
const { publicKey } = await this._wallet.connect()
this._publicKey = publicKey
this.emit('connect', publicKey)
this._wallet.client.on('session_delete', this.disconnect)
this._wallet.client.on('session_delete', this._onDisconnect)
} catch (error: unknown) {
if ((error as Error).constructor.name === 'QRCodeModalError') throw new WalletWindowClosedError()
throw error
Expand All @@ -88,7 +92,7 @@ export class WalletConnectWalletAdapter extends BaseSignerWalletAdapter {
async disconnect(): Promise<void> {
const wallet = this._wallet
if (wallet) {
wallet.client.off('session_delete', this.disconnect)
wallet.client.off('session_delete', this._onDisconnect)
this._publicKey = null

try {
Expand Down