Skip to content

Commit

Permalink
Merge pull request #158 from nightly-labs/solana-detection
Browse files Browse the repository at this point in the history
add detection of delayed wallets
  • Loading branch information
LukassF authored Apr 5, 2024
2 parents 233ca95 + 98545dd commit b54fd53
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sdk/apps/modal-example/src/routes/externalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export default function SolanaExternalModal() {
setAdapter(adapter)
})

createEffect(() => {
if (modal() && adapter()) {
modal()!.walletsList = adapter()!.walletsList
}
})

createEffect(() => {
if (eager()) {
adapter()
Expand Down
30 changes: 30 additions & 0 deletions sdk/packages/selector-solana/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {

private _loading: boolean

// interval used for checking for wallets with delayed detection
private _detectionIntervalId: NodeJS.Timeout | undefined
private _maxNumberOfChecks = 10

private _connectionOptions: ConnectionOptions = defaultConnectionOptions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _eventHandlers: Map<string, (...args: any[]) => void> = new Map()
Expand Down Expand Up @@ -220,6 +224,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined
)

adapter.checkForArrivingWallets(metadataWallets)

// Add event listener for userConnected
app.on('userConnected', async () => {
try {
Expand Down Expand Up @@ -304,6 +310,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined
)

adapter.checkForArrivingWallets(metadataWallets)

// Add event listener for userConnected
app.on('userConnected', async () => {
try {
Expand Down Expand Up @@ -560,10 +568,14 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
.then(([app, metadataWallets]) => {
this._app = app
this._metadataWallets = metadataWallets

this.walletsList = getSolanaWalletsList(
metadataWallets,
getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined
)

this.checkForArrivingWallets(metadataWallets)

// Add event listener for userConnected
app.on('userConnected', async () => {
try {
Expand Down Expand Up @@ -686,6 +698,22 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
return this.walletsList
}

checkForArrivingWallets = (metadataWallets: WalletMetadata[]) => {
clearInterval(this._detectionIntervalId)
let checks = 0

this._detectionIntervalId = setInterval(() => {
if (checks >= this._maxNumberOfChecks || this.connected) {
clearInterval(this._detectionIntervalId)
}
checks++
this.walletsList = getSolanaWalletsList(
metadataWallets,
getRecentWalletForNetwork(SOLANA_NETWORK)?.walletName ?? undefined
)
}, 1000)
}

disconnect = async () => {
if (this.connected) {
if (this._appSessionActive) {
Expand Down Expand Up @@ -734,6 +762,8 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter {
this._connected = false

this.emit('disconnect')

clearInterval(this._detectionIntervalId)
}
}

Expand Down

0 comments on commit b54fd53

Please sign in to comment.