Skip to content

Commit

Permalink
Merge pull request #59 from nightly-labs/selector/polkadot-eager-opti…
Browse files Browse the repository at this point in the history
…onal

Polkadot selector: make eager connect optional for both extensions and mobile
  • Loading branch information
awojciak authored Aug 14, 2023
2 parents d0b1862 + a64c651 commit 85ec937
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion sdk/apps/modal-example/src/routes/aleph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function Polkadot() {
},
network: 'AlephZero'
},
true,
true, // change this to false to test disabling eager connect
document.getElementById('modalAnchor')
)

Expand Down
29 changes: 20 additions & 9 deletions sdk/packages/selector-polkadot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class NightlyConnectAdapter implements Injected {
private _modal: NightlyConnectSelectorModal | undefined

private _appInitData: AppSelectorInitialize
private _eagerConnectForStandardWallets: boolean
private _useEagerConnect: boolean

private _metadataWallets: MetadataWallet[] = []
private _walletsList: IPolkadotWalletListItem[] = []
Expand All @@ -46,11 +46,11 @@ export class NightlyConnectAdapter implements Injected {

private _loading: boolean

constructor(appInitData: AppSelectorInitialize, eagerConnectForStandardWallets?: boolean) {
constructor(appInitData: AppSelectorInitialize, useEagerConnect?: boolean) {
this._connecting = false
this._connected = false
this._appInitData = appInitData
this._eagerConnectForStandardWallets = !!eagerConnectForStandardWallets
this._useEagerConnect = !!useEagerConnect
this._appSessionActive = false
this._loading = false
}
Expand Down Expand Up @@ -108,10 +108,14 @@ export class NightlyConnectAdapter implements Injected {
}
public static build = async (
appInitData: AppSelectorInitialize,
eagerConnectForStandardWallets?: boolean,
useEagerConnect?: boolean,
anchorRef?: HTMLElement | null
) => {
const adapter = new NightlyConnectAdapter(appInitData, eagerConnectForStandardWallets)
if (!useEagerConnect) {
clearSessionIdForNetwork(appInitData.network)
}

const adapter = new NightlyConnectAdapter(appInitData, useEagerConnect)

adapter.walletsList = getPolkadotWalletsList(
[],
Expand Down Expand Up @@ -151,10 +155,14 @@ export class NightlyConnectAdapter implements Injected {

public static buildLazy = (
appInitData: AppSelectorInitialize,
eagerConnectForStandardWallets?: boolean,
useEagerConnect?: boolean,
anchorRef?: HTMLElement | null
) => {
const adapter = new NightlyConnectAdapter(appInitData, eagerConnectForStandardWallets)
if (!useEagerConnect) {
clearSessionIdForNetwork(appInitData.network)
}

const adapter = new NightlyConnectAdapter(appInitData, useEagerConnect)

adapter.walletsList = getPolkadotWalletsList(
[],
Expand Down Expand Up @@ -196,6 +204,10 @@ export class NightlyConnectAdapter implements Injected {
}
// ensureLoaded = async () => {}
canEagerConnect = async () => {
if (!this._useEagerConnect) {
return false
}

// utility for case if somebody wants to fire connect asap, but doesn't want to show modal if e. g. there was no user connected on the device yet
if (this._loading) {
for (let i = 0; i < 200; i++) {
Expand All @@ -216,7 +228,6 @@ export class NightlyConnectAdapter implements Injected {
}

if (
this._eagerConnectForStandardWallets &&
getRecentStandardWalletForNetwork(this.network) !== null &&
isStandardConnectedForNetwork(this.network)
) {
Expand Down Expand Up @@ -388,7 +399,7 @@ export class NightlyConnectAdapter implements Injected {

const recentName = getRecentStandardWalletForNetwork(this.network)
if (
this._eagerConnectForStandardWallets &&
this._useEagerConnect &&
recentName !== null &&
isStandardConnectedForNetwork(this.network)
) {
Expand Down

0 comments on commit 85ec937

Please sign in to comment.