Skip to content

Commit 1f563bb

Browse files
authored
[core: 2.5.0-alpha.1] - [enhancement] - Delayed Wallet Init (#1145)
* Init wallets upon connect call * Remove log * Remove log * Increase wait to be safe * Fix autoselect wait check * Add gamestop and autoselect for testing * Revert "Add gamestop and autoselect for testing" This reverts commit 0526972. * Increment version * Remove unused import
1 parent 1f8646d commit 1f563bb

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.4.1-alpha.1",
3+
"version": "2.5.0-alpha.1",
44
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",

packages/core/src/configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export let configuration: Configuration = {
55
svelteInstance: null,
66
appMetadata: null,
77
apiKey: null,
8-
device: getDevice()
8+
device: getDevice(),
9+
initialWalletInit: []
910
}
1011

1112
export function updateConfiguration(update: Partial<Configuration>): void {

packages/core/src/connect.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { firstValueFrom } from 'rxjs'
22
import { filter, withLatestFrom, pluck } from 'rxjs/operators'
3+
import { configuration } from './configuration'
34
import { state } from './store'
5+
import { setWalletModules } from './store/actions'
46
import { connectWallet$, wallets$ } from './streams'
57
import type { ConnectOptions, ConnectOptionsString, WalletState } from './types'
8+
import { wait } from './utils'
69
import { validateConnectOptions } from './validation'
710

811
async function connect(
@@ -28,6 +31,16 @@ async function connect(
2831
autoSelect: { label: '', disableModals: false }
2932
}
3033

34+
// if auto selecting, wait until next event loop
35+
if (autoSelect && (typeof autoSelect === 'string' || autoSelect.label)) {
36+
await wait(50)
37+
}
38+
39+
// first time calling connect, so initialize and set wallet modules
40+
if (!state.get().walletModules.length) {
41+
setWalletModules(configuration.initialWalletInit)
42+
}
43+
3144
connectWallet$.next({
3245
autoSelect:
3346
typeof autoSelect === 'string'

packages/core/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import { configuration, updateConfiguration } from './configuration'
1717

1818
import {
1919
addChains,
20-
setWalletModules,
2120
updateAccountCenter,
2221
updateNotify,
2322
customNotification,
2423
setLocale,
25-
setPrimaryWallet
24+
setPrimaryWallet,
25+
setWalletModules
2626
} from './store/actions'
2727

2828
import updateBalances from './update-balances'
@@ -168,7 +168,6 @@ function init(options: InitOptions): OnboardAPI {
168168
if (!apiKey || !notifyUpdate.enabled) {
169169
notifyUpdate.enabled = false
170170
}
171-
console.log(notifyUpdate)
172171
updateNotify(notifyUpdate)
173172
}
174173
} else {
@@ -191,11 +190,10 @@ function init(options: InitOptions): OnboardAPI {
191190
updateConfiguration({
192191
appMetadata,
193192
svelteInstance: app,
194-
apiKey
193+
apiKey,
194+
initialWalletInit: wallets
195195
})
196196

197-
setWalletModules(wallets)
198-
199197
return API
200198
}
201199

@@ -317,10 +315,13 @@ function mountApp() {
317315
</style>
318316
`
319317

320-
const containerElementQuery = state.get().accountCenter.containerElement || 'body'
318+
const containerElementQuery =
319+
state.get().accountCenter.containerElement || 'body'
321320
const containerElement = document.querySelector(containerElementQuery)
322321
if (!containerElement) {
323-
throw new Error(`Element with query ${state.get().accountCenter} does not exist.`)
322+
throw new Error(
323+
`Element with query ${state.get().accountCenter} does not exist.`
324+
)
324325
}
325326

326327
containerElement.appendChild(onboard)

packages/core/src/store/actions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ export function removeWallet(id: string): void {
123123
}
124124

125125
export function setPrimaryWallet(wallet: WalletState, address?: string): void {
126-
console.log({ address })
127126
const error =
128127
validateWallet(wallet) || (address && validateString(address, 'address'))
129128

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export type Configuration = {
119119
appMetadata: AppMetadata | null
120120
device: Device | DeviceNotBrowser
121121
apiKey: string
122+
initialWalletInit: WalletInit[]
122123
}
123124

124125
export type Locale = string

packages/core/src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export function shortenEns(ens: string): string {
8585

8686
export async function copyWalletAddress(text: string): Promise<void> {
8787
try {
88-
const copy = await navigator.clipboard.writeText(text);
89-
return copy
88+
const copy = await navigator.clipboard.writeText(text)
89+
return copy
9090
} catch (err) {
9191
console.error('Failed to copy: ', err)
9292
}
@@ -242,3 +242,6 @@ export const defaultNotifyEventStyles: Record<string, NotifyEventStyles> = {
242242
eventIcon: info
243243
}
244244
}
245+
246+
export const wait = (time: number) =>
247+
new Promise(resolve => setTimeout(resolve, time))

0 commit comments

Comments
 (0)