Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: polkadot selector lazy loading without session id #94

Merged
merged 13 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class NightlyMobileMain extends LitElement {
this._selectorItems = [...value].sort(walletsSort)

this.setItemsCount()
this.requestUpdate()
}

@state()
Expand Down
11 changes: 9 additions & 2 deletions sdk/packages/selector-base/src/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class NightlyConnectSelectorModal {
}
}

set sessionId(id: string) {
if (this._modal && id) this._modal.sessionId = id
}

createSelectorElement = (
variablesOverride?: object,
stylesOverride?: string,
Expand All @@ -67,10 +71,13 @@ export class NightlyConnectSelectorModal {
}
}

public openModal = (sessionId: string, onSelectListWallet: (name: string) => void) => {
public openModal = (
sessionId: string | undefined,
onSelectListWallet: (name: string) => void
) => {
if (this._modal && this._open === false) {
this._modal.onWalletClick = onSelectListWallet
this._modal.sessionId = sessionId
this._modal.sessionId = sessionId ?? ''
this._anchor.appendChild(this._modal)
this._open = true
this.onOpen?.()
Expand Down
191 changes: 100 additions & 91 deletions sdk/packages/selector-polkadot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,106 @@ export class NightlyConnectAdapter implements Injected {
return
}

let intervalId: NodeJS.Timeout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to loadingInterval


// opening modal and waiting for sessionId
if (this._modal) {
this._modal.onClose = () => {
if (intervalId) clearInterval(intervalId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove


if (this._connecting) {
this._connecting = false
const error = new Error('Connection cancelled')
reject(error)
}
}
this._modal.openModal(this._app?.sessionId ?? undefined, (walletName: string) => {
if (
isMobileBrowser() &&
!this.walletsList.find((w) => w.name === walletName)?.injectedWallet
) {
this.connectToMobileWallet(walletName)
if (intervalId) clearInterval(intervalId)
} else {
this.connectToStandardWallet(walletName, resolve)
if (intervalId) clearInterval(intervalId)
}
})

// checking whether sessionId is defined
let checks = 0
intervalId = setInterval(async (): Promise<void> => {
checks++
if (this._app) {
if (this._modal) this._modal.sessionId = this._app.sessionId
if (intervalId) clearInterval(intervalId)

if (this._app.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) {
// Try to eager connect if session is restored
try {
this.eagerConnectDeeplink()
this._connected = true
this._connecting = false
this._appSessionActive = true
resolve()
return
} catch (error) {
// If we fail because of whatever reason
// Reset session since it might be corrupted
const [app] = await NightlyConnectAdapter.initApp(this._appInitData)
this._app = app
}
}

const recentName = getRecentStandardWalletForNetwork(this.network)
if (
this._useEagerConnect &&
recentName !== null &&
isStandardConnectedForNetwork(this.network)
) {
await this.connectToStandardWallet(recentName, resolve)

if (this._connected) {
return
}
}

this._app.on('userConnected', () => {
try {
if (this._chosenMobileWalletName) {
persistRecentStandardWalletForNetwork(
this._chosenMobileWalletName,
this.network
)
} else {
clearRecentStandardWalletForNetwork(this.network)
}
if (!this._app || this._app.accounts.activeAccounts.length <= 0) {
this._connecting = false
// If user does not pass any accounts, we should disconnect
this.disconnect()
}
this._connected = true
this._connecting = false
this._appSessionActive = true
this._modal?.closeModal()
resolve()
} catch {
this.disconnect()
}
})

return
}

// fallback when connecting takes too long
if (checks > 500) {
if (intervalId) clearInterval(intervalId)
reject(new Error('Connecting takes too long'))
}
}, 10)
}

if (this._initOnConnect) {
this._connecting = true

Expand All @@ -473,99 +573,8 @@ export class NightlyConnectAdapter implements Injected {
throw e
}
}
} else {
if (this._loading) {
// we do it to ensure proper connect flow in case if adapter is lazily built, but e. g. polkadot wallets selector uses its own eager connect
for (let i = 0; i < 200; i++) {
await sleep(10)

if (!this._loading) {
break
}
}

if (this._loading) {
throw new Error('Wallet not ready')
}
}

if (!this._app) {
throw new Error('Wallet not ready')
}

this._connecting = true
}

if (this._app.hasBeenRestored() && this._app.accounts.activeAccounts.length > 0) {
// Try to eager connect if session is restored
try {
this.eagerConnectDeeplink()
this._connected = true
this._connecting = false
this._appSessionActive = true
resolve()
return
} catch (error) {
// If we fail because of whatever reason
// Reset session since it might be corrupted
const [app] = await NightlyConnectAdapter.initApp(this._appInitData)
this._app = app
}
}

const recentName = getRecentStandardWalletForNetwork(this.network)
if (
this._useEagerConnect &&
recentName !== null &&
isStandardConnectedForNetwork(this.network)
) {
await this.connectToStandardWallet(recentName, resolve)

if (this._connected) {
return
}
}
this._app.on('userConnected', () => {
try {
if (this._chosenMobileWalletName) {
persistRecentStandardWalletForNetwork(this._chosenMobileWalletName, this.network)
} else {
clearRecentStandardWalletForNetwork(this.network)
}
if (!this._app || this._app.accounts.activeAccounts.length <= 0) {
this._connecting = false
// If user does not pass any accounts, we should disconnect
this.disconnect()
}
this._connected = true
this._connecting = false
this._appSessionActive = true
this._modal?.closeModal()
resolve()
} catch {
this.disconnect()
}
})

if (this._modal) {
this._modal.onClose = () => {
if (this._connecting) {
this._connecting = false
const error = new Error('Connection cancelled')
reject(error)
}
}
this._modal.openModal(this._app.sessionId, (walletName: string) => {
if (
isMobileBrowser() &&
!this.walletsList.find((w) => w.name === walletName)?.injectedWallet
) {
this.connectToMobileWallet(walletName)
} else {
this.connectToStandardWallet(walletName, resolve)
}
})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
this._connecting = false
Expand Down
Loading