Skip to content

Commit

Permalink
Merge pull request #816 from airgap-it/fix/race_condition_transport
Browse files Browse the repository at this point in the history
fix: race condition
  • Loading branch information
IsaccoSordo authored Sep 16, 2024
2 parents 8674705 + 4420a7a commit 63505be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 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 @@ -515,7 +515,8 @@ export class DAppClient extends Client {
}

// block until the transport is ready
await this._transport.promise
const transport = (await this._transport.promise) as DappWalletConnectTransport
await transport.waitForResolution()

this.openRequestsOtherTabs.add(message.id)
isV3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@airgap/beacon-types'
import { Transport, PeerManager } from '@airgap/beacon-core'
import { SignClientTypes } from '@walletconnect/types'
import { ExposedPromise } from '@airgap/beacon-utils'

/**
* @internalapi
Expand All @@ -26,6 +27,8 @@ export class WalletConnectTransport<
> extends Transport<T, K, WalletConnectCommunicationClient> {
public readonly type: TransportType = TransportType.WALLETCONNECT

private isReady = new ExposedPromise<boolean>()

constructor(
name: string,
_keyPair: KeyPair,
Expand All @@ -45,6 +48,13 @@ export class WalletConnectTransport<
return Promise.resolve(true)
}

/**
* Returns a promise that blocks the execution flow when awaited if the transport hasn't resolved yet; otherwise, it returns true.
*/
waitForResolution(): Promise<boolean> {
return this.isReady.promise
}

public async connect(): Promise<void> {
if ([TransportStatus.CONNECTED, TransportStatus.CONNECTING].includes(this._isConnected)) {
return
Expand All @@ -70,6 +80,8 @@ export class WalletConnectTransport<
if (!isLeader) {
this._isConnected = TransportStatus.SECONDARY_TAB_CONNECTED
}

this.isReady.resolve(true)
}

wasDisconnectedByWallet() {
Expand Down Expand Up @@ -114,7 +126,9 @@ export class WalletConnectTransport<
public async disconnect(): Promise<void> {
await this.client.close()

return super.disconnect()
await super.disconnect()

this.isReady = new ExposedPromise()
}

public async startOpenChannelListener(): Promise<void> {
Expand Down

0 comments on commit 63505be

Please sign in to comment.