diff --git a/package.json b/package.json index 3b43eeb..15bb9f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xdefi/wallets-connector", - "version": "2.0.3", + "version": "2.1.0", "description": "Cross chain wallets connector with react hooks", "author": "garageinc", "license": "MIT", diff --git a/src/controllers/providers.ts b/src/controllers/providers.ts index af752cd..3b685a4 100644 --- a/src/controllers/providers.ts +++ b/src/controllers/providers.ts @@ -97,35 +97,38 @@ export class ProviderController { if (providerHasChains) { const cachedChains = this.injectedChains[providerId] const connectList = cachedChains?.length ? cachedChains : chains + const providerChains = connectList.filter( + (chain) => providerHasChains[chain] + ) - for (const chain of connectList) { + const promises = [] + for (const chain of providerChains) { const providerChain = providerHasChains[chain] - if (providerChain) { - try { - const accounts = await providerChain.methods.getAccounts() - if (!accounts.length) { - throw new Error( - `Provider ${providerId} returned empty accounts for chain: ${chain}` - ) - } - - results.push({ - chain: chain as IChainType, - accounts: accounts - }) - } catch (e) { - console.error(e) - - if ( - e.message === - `Provider ${providerId} returned empty accounts for chain: ${chain}` - ) { - throw new Error(e.message) - } - if (e?.code === 4001) { - throw new Error(e.code) - } + promises.push(providerChain.methods.getAccounts()) + } + + try { + const accounts = await Promise.all(promises) + + accounts.forEach((accounts, index) => { + const chain = providerChains[index] as IChainType + if (!accounts.length) { + throw new Error(`Provider is not connected`) } + + results.push({ + chain, + accounts + }) + }) + } catch (e) { + console.error(e) + + if (e.message === `Provider is not connected`) { + throw new Error(e.message) + } + if (e?.code === 4001) { + throw new Error(e.code) } } } else { diff --git a/src/wallets/index.ts b/src/wallets/index.ts index 87b7506..6607a01 100644 --- a/src/wallets/index.ts +++ b/src/wallets/index.ts @@ -80,8 +80,18 @@ export class WalletsConnector { } }) - // GarageInc | 3.02.2022, handle for all subscription hooks in react app - setTimeout(() => this.init()) + if (document?.readyState === 'complete') { + this.init() + } + + const documentStateChange = async (event: Event) => { + if ((event?.target as Document)?.readyState === 'complete') { + this.init() + document?.removeEventListener('readystatechange', documentStateChange) + } + } + + document?.addEventListener('readystatechange', documentStateChange) } private init() {