Skip to content

Commit

Permalink
feat: add connect listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 17, 2024
1 parent 802ba6a commit 618be64
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
type: metaMask.type,
async setup() {
const provider = await this.getProvider()
if (provider && !connect) {
connect = this.onConnect.bind(this)
provider.on('connect', connect as Listener)
if (provider?.on) {
if (!connect) {
connect = this.onConnect.bind(this)
provider.on('connect', connect as Listener)
}

// We shouldn't need to listen for `'accountsChanged'` here since the `'connect'` event should suffice (and wallet shouldn't be connected yet).
// Some wallets, like MetaMask, do not implement the `'connect'` event and overload `'accountsChanged'` instead.
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
provider.on('accountsChanged', accountsChanged as Listener)
}
}
},
async connect({ chainId, isReconnecting } = {}) {
Expand Down Expand Up @@ -194,10 +203,6 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
const provider = await this.getProvider()

// Manage EIP-1193 event listeners
if (accountsChanged) {
provider.removeListener('accountsChanged', accountsChanged)
accountsChanged = undefined
}
if (chainChanged) {
provider.removeListener('chainChanged', chainChanged)
chainChanged = undefined
Expand Down Expand Up @@ -441,10 +446,6 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
config.emitter.emit('disconnect')

// Manage EIP-1193 event listeners
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
provider.on('accountsChanged', accountsChanged as Listener)
}
if (chainChanged) {
provider.removeListener('chainChanged', chainChanged)
chainChanged = undefined
Expand Down

0 comments on commit 618be64

Please sign in to comment.