Skip to content

Commit

Permalink
fix: add back refresh logic for mobile only
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Nov 14, 2024
1 parent b6c138b commit 379263d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ export class DAppClient extends Client {
await this.setTransport(this.p2pTransport)
} else if (origin === Origin.WALLETCONNECT) {
await this.setTransport(this.walletConnectTransport)
this.walletConnectTransport?.forceUpdate('INIT')
}
if (this._transport.isResolved()) {
const transport = await this.transport
Expand Down Expand Up @@ -1080,7 +1081,7 @@ export class DAppClient extends Client {
const isWCInstance = isResolved && (await this.transport) instanceof WalletConnectTransport
const isLeader = this.multiTabChannel.isLeader()

return !isResolved || (isResolved && (!isWCInstance || (isWCInstance && isLeader)))
return !isResolved || !isWCInstance || isLeader || isMobileOS(window)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ export class WalletConnectTransport<
: !!this.client.signClient?.session.getAll()?.length
}

/**
* Forcefully updates any DApps running on the same session
* Typical use case: localStorage changes to reflect to indexDB
* @param type the message type
*/
public forceUpdate(type: string) {
this.client.storage.notify(type)
}

public async getPeers(): Promise<T[]> {
const client = WalletConnectCommunicationClient.getInstance(this.wcOptions, this.isLeader)
const session = client.currentSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
private isLeader: Function
) {
super()
this.storage.onMessageHandler = this.onStorageMessageHandler.bind(this)
this.storage.onErrorHandler = this.onStorageErrorHandler.bind(this)
}

static getInstance(
Expand Down Expand Up @@ -163,6 +165,25 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
this.channelOpeningListeners.set('channelOpening', callbackFunction)
}

/**
* WC Sign client doesn't sync between intances, meaning that a dApp signClient instance state may
* differ from a wallet state
*/
private async refreshState() {
await this.closeSignClient()

const client = (await this.getSignClient())!
const lastIndex = client.session.keys.length - 1

if (lastIndex > -1) {
this.session = client.session.get(client.session.keys[lastIndex])
this.updateStorageWallet(this.session)
this.setDefaultAccountAndNetwork()
} else {
this.clearState()
}
}

private clearEvents() {
this.signClient?.removeAllListeners('session_event')
this.signClient?.removeAllListeners('session_update')
Expand All @@ -172,6 +193,44 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
this.signClient?.core.pairing.events.removeAllListeners('pairing_expire')
}

private abortErrorBuilder() {
if (!this.messageIds.length) {
return
}

const errorResponse: any = {
type: BeaconMessageType.Disconnect,
id: this.messageIds.pop(),
errorType: BeaconErrorType.ABORTED_ERROR
}
this.session && this.notifyListeners(this.getTopicFromSession(this.session), errorResponse)
this.messageIds = [] // reset
}

private onStorageMessageHandler(type: string) {
if (!this.isMobileOS()) {
return
}

logger.debug('onStorageMessageHandler', type)

if (type === 'RESET') {
this.abortErrorBuilder()
this.clearEvents()
// no need to invoke `closeSignClinet` as the other tab already closed the connection
this.signClient = undefined
this.clearState()

return
}

this.refreshState()
}

private onStorageErrorHandler(data: any) {
logger.error('onStorageError', data)
}

async unsubscribeFromEncryptedMessages(): Promise<void> {
this.activeListeners.clear()
this.channelOpeningListeners.clear()
Expand Down Expand Up @@ -796,7 +855,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
'session_update'
)
}
} catch {}
} catch { }
}

private async disconnect(
Expand Down Expand Up @@ -836,8 +895,8 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
this.session?.pairingTopic === topic
? this.session
: signClient.session
.getAll()
.find((session: SessionTypes.Struct) => session.pairingTopic === topic)
.getAll()
.find((session: SessionTypes.Struct) => session.pairingTopic === topic)

if (!session) {
return undefined
Expand Down

0 comments on commit 379263d

Please sign in to comment.