Skip to content

Commit

Permalink
fix: add locking mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Sep 24, 2024
1 parent c97d833 commit 9fdffaf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
15 changes: 10 additions & 5 deletions packages/beacon-core/src/utils/multi-tab-channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger } from '@airgap/beacon-core'
import { BeaconMessageType } from '@airgap/beacon-types'
import { ExposedPromise } from '@airgap/beacon-utils'
import { createLeaderElection, BroadcastChannel, LeaderElector } from 'broadcast-channel'

type BCMessageType =
Expand Down Expand Up @@ -35,6 +36,7 @@ export class MultiTabChannel {
]
private onBCMessageHandler: Function
private onElectedLeaderHandler: Function
private _isLeader: ExposedPromise<boolean> = new ExposedPromise()
// Auxiliary variable needed for handling beforeUnload.
// Closing a tab causes the elector to be killed immediately
private wasLeader: boolean = false
Expand All @@ -58,8 +60,9 @@ export class MultiTabChannel {

if (!hasLeader) {
await this.elector.awaitLeadership()
this.wasLeader = this.isLeader()
this.wasLeader = this.elector.isLeader
this.wasLeader && logger.log('The current tab is the leader.')
this._isLeader.resolve(this.wasLeader)
}

this.channel.onmessage = this.eventListeners[1]
Expand All @@ -84,9 +87,11 @@ export class MultiTabChannel {
if (message.type === 'LEADER_DEAD') {
await this.elector.awaitLeadership()

this.wasLeader = this.isLeader()
this.wasLeader = this.elector.isLeader
this._isLeader = new ExposedPromise()
this._isLeader.resolve(this.wasLeader)

if (this.isLeader()) {
if (this.wasLeader) {
this.onElectedLeaderHandler()
logger.log('The current tab is the leader.')
}
Expand All @@ -96,8 +101,8 @@ export class MultiTabChannel {
this.onBCMessageHandler(message)
}

isLeader(): boolean {
return this.elector.isLeader
isLeader(): Promise<boolean> {
return this._isLeader.promise
}

async getLeadership() {
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ export class DAppClient extends Client {
}

private async getPairingRequestInfo(transport: DappWalletConnectTransport) {
if (this.multiTabChannel.isLeader()) {
if (await this.multiTabChannel.isLeader()) {
return transport.getPairingRequestInfo()
}

Expand Down Expand Up @@ -960,7 +960,7 @@ export class DAppClient extends Client {
this.debounceSetActiveAccount = true
this._initPromise = undefined
this.postMessageTransport = this.p2pTransport = this.walletConnectTransport = undefined
if (this.multiTabChannel.isLeader()) {
if (await this.multiTabChannel.isLeader()) {
await transport.disconnect()
this.openRequestsOtherTabs.clear()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class DappWalletConnectTransport extends WalletConnectTransport<
keyPair: KeyPair,
storage: Storage,
wcOptions: { network: NetworkType; opts: SignClientTypes.Options },
isLeader: () => boolean
isLeader: () => Promise<boolean>
) {
super(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class WalletConnectTransport<
storage: Storage,
storageKey: K,
private wcOptions: { network: NetworkType; opts: SignClientTypes.Options },
private isLeader: () => boolean
private isLeader: () => Promise<boolean>
) {
super(
name,
Expand All @@ -62,7 +62,7 @@ export class WalletConnectTransport<

this._isConnected = TransportStatus.CONNECTING

const isLeader = this.isLeader()
const isLeader = await this.isLeader()

if (isLeader) {
await this.client.init()
Expand Down

0 comments on commit 9fdffaf

Please sign in to comment.