|
1 | 1 | <script lang="ts">
|
2 | 2 | import { ProviderRpcErrorCode, WalletModule } from '@web3-onboard/common'
|
3 |
| - import { BehaviorSubject, takeUntil } from 'rxjs' |
4 | 3 | import EventEmitter from 'eventemitter3'
|
5 | 4 | import { _ } from 'svelte-i18n'
|
6 | 5 | import en from '../../i18n/en.json'
|
7 |
| - import { selectAccounts } from '../../provider.js' |
| 6 | + import { listenAccountsChanged, selectAccounts } from '../../provider.js' |
8 | 7 | import { state } from '../../store/index.js'
|
9 | 8 | import { connectWallet$, onDestroy$ } from '../../streams.js'
|
10 | 9 | import { addWallet, updateAccount } from '../../store/actions.js'
|
|
20 | 19 | import { configuration } from '../../configuration.js'
|
21 | 20 | import { getBlocknativeSdk } from '../../services.js'
|
22 | 21 | import { BigNumber } from 'ethers'
|
| 22 | +
|
| 23 | + import { |
| 24 | + BehaviorSubject, |
| 25 | + distinctUntilChanged, |
| 26 | + filter, |
| 27 | + firstValueFrom, |
| 28 | + mapTo, |
| 29 | + Subject, |
| 30 | + take, |
| 31 | + takeUntil |
| 32 | + } from 'rxjs' |
| 33 | +
|
23 | 34 | import {
|
24 | 35 | getChainId,
|
25 | 36 | requestAccounts,
|
|
39 | 50 |
|
40 | 51 | const { appMetadata } = configuration
|
41 | 52 | const { walletModules, connect } = state.get()
|
| 53 | + const cancelPreviousConnect$ = new Subject<void>() |
42 | 54 |
|
43 | 55 | let connectionRejected = false
|
| 56 | + let previousConnectionRequest = false |
44 | 57 | let wallets: WalletWithLoadingIcon[] = []
|
45 | 58 | let selectedWallet: WalletState | null
|
46 | 59 | let agreed: boolean
|
|
54 | 67 | 'selectingWallet'
|
55 | 68 | )
|
56 | 69 |
|
| 70 | + // handle the edge case where disableModals was set to true on first call |
| 71 | + // and then set to false on second call and there is still a pending call |
| 72 | + connectWallet$ |
| 73 | + .pipe( |
| 74 | + distinctUntilChanged( |
| 75 | + (prev, curr) => |
| 76 | + prev.autoSelect && |
| 77 | + curr.autoSelect && |
| 78 | + prev.autoSelect.disableModals === curr.autoSelect.disableModals |
| 79 | + ), |
| 80 | + filter( |
| 81 | + ({ autoSelect }) => autoSelect && autoSelect.disableModals === false |
| 82 | + ), |
| 83 | + takeUntil(onDestroy$) |
| 84 | + ) |
| 85 | + .subscribe(() => { |
| 86 | + selectedWallet && connectWallet() |
| 87 | + }) |
| 88 | +
|
57 | 89 | // ==== SELECT WALLET ==== //
|
58 | 90 | async function selectWallet({
|
59 | 91 | label,
|
|
161 | 193 |
|
162 | 194 | const { provider, label } = selectedWallet
|
163 | 195 |
|
| 196 | + cancelPreviousConnect$.next() |
| 197 | +
|
164 | 198 | try {
|
165 |
| - const [address] = await requestAccounts(provider) |
| 199 | + const [address] = await Promise.race([ |
| 200 | + // resolved account |
| 201 | + requestAccounts(provider), |
| 202 | + // or connect wallet is called again whilst waiting for response |
| 203 | + firstValueFrom(cancelPreviousConnect$.pipe(mapTo([]))) |
| 204 | + ]) |
166 | 205 |
|
167 | 206 | // canceled previous request
|
168 | 207 | if (!address) {
|
|
216 | 255 |
|
217 | 256 | // account access has already been requested and is awaiting approval
|
218 | 257 | if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_ALREADY_REQUESTED) {
|
| 258 | + previousConnectionRequest = true |
| 259 | +
|
| 260 | + if (autoSelect.disableModals) { |
| 261 | + connectWallet$.next({ inProgress: false }) |
| 262 | + return |
| 263 | + } |
| 264 | +
|
| 265 | + listenAccountsChanged({ |
| 266 | + provider: selectedWallet.provider, |
| 267 | + disconnected$: connectWallet$.pipe( |
| 268 | + filter(({ inProgress }) => !inProgress), |
| 269 | + mapTo('') |
| 270 | + ) |
| 271 | + }) |
| 272 | + .pipe(take(1)) |
| 273 | + .subscribe(([account]) => { |
| 274 | + account && connectWallet() |
| 275 | + }) |
| 276 | +
|
219 | 277 | return
|
220 | 278 | }
|
221 | 279 | }
|
|
286 | 344 | })
|
287 | 345 |
|
288 | 346 | function setStep(update: keyof i18n['connect']) {
|
| 347 | + cancelPreviousConnect$.next() |
289 | 348 | modalStep$.next(update)
|
290 | 349 | }
|
291 | 350 |
|
|
408 | 467 | <ConnectingWallet
|
409 | 468 | {connectWallet}
|
410 | 469 | {connectionRejected}
|
| 470 | + {previousConnectionRequest} |
411 | 471 | {setStep}
|
412 | 472 | {deselectWallet}
|
413 | 473 | {selectedWallet}
|
|
0 commit comments