@@ -11,13 +11,14 @@ import { close, update } from 'yaob'
1111import {
1212 asMaybeOtpError ,
1313 EdgeAccount ,
14+ EdgeCurrencyInfo ,
1415 EdgeCurrencyWallet ,
1516 EdgePluginMap ,
1617 EdgeTokenMap
1718} from '../../types/types'
1819import { makePeriodicTask } from '../../util/periodic-task'
1920import { snooze } from '../../util/snooze'
20- import { loadWalletCache } from '../cache/cache-wallet-loader'
21+ import { loadWalletCache , WalletCacheSetup } from '../cache/cache-wallet-loader'
2122import {
2223 makeWalletCacheSaver ,
2324 WalletCacheSaver
@@ -41,6 +42,11 @@ import {
4142
4243export const EXPEDITED_SYNC_INTERVAL = 5000
4344
45+ /** Returns the disklet path for the account's wallet cache file. */
46+ function getWalletCachePath ( storageWalletId : string ) : string {
47+ return `accountCache/${ storageWalletId } /walletCache.json`
48+ }
49+
4450export interface AccountOutput {
4551 readonly accountApi : EdgeAccount
4652 readonly currencyWallets : { [ walletId : string ] : EdgeCurrencyWallet }
@@ -95,27 +101,20 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
95101
96102 // Try to load wallet cache for instant UI.
97103 // Returns the cache setup if successful, undefined otherwise.
98- async function tryLoadCache ( ) : Promise <
99- import ( '../cache/cache-wallet-loader' ) . WalletCacheSetup | undefined
100- > {
104+ // Assumes storage wallets are already initialized.
105+ async function tryLoadCache ( ) : Promise < WalletCacheSetup | undefined > {
101106 try {
102107 const storageWalletId = accountWalletInfos [ 0 ] ?. id
103108 if ( storageWalletId == null ) {
104109 return undefined
105110 }
106111
107- // Initialize ALL account storage wallets first.
108- // This is cheap (just file reads) and avoids race conditions.
109- await Promise . all (
110- accountWalletInfos . map ( info => addStorageWallet ( ai , info ) )
111- )
112-
113- const cachePath = `accountCache/${ storageWalletId } /walletCache.json`
112+ const cachePath = getWalletCachePath ( storageWalletId )
114113 const cacheJson = await ai . props . io . disklet . getText ( cachePath )
115114
116115 // Build currency info map from loaded plugins:
117116 const currencyInfos : {
118- [ pluginId : string ] : import ( '../../types/types' ) . EdgeCurrencyInfo
117+ [ pluginId : string ] : EdgeCurrencyInfo
119118 } = { }
120119 for ( const pluginId of Object . keys ( state . plugins . currency ) ) {
121120 currencyInfos [ pluginId ] =
@@ -148,8 +147,8 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
148147 // Check for "file not found" errors which are expected on first login:
149148 let isExpectedError = false
150149 if ( error instanceof Error ) {
151- // Disklet throws Error with 'Cannot read file' message
152- if ( error . message . includes ( 'Cannot read' ) ) {
150+ // Disklet throws Error with 'Cannot read file ... ' message
151+ if ( error . message . startsWith ( 'Cannot read file ' ) ) {
153152 isExpectedError = true
154153 }
155154 // Node.js-style errors have a 'code' property
@@ -171,6 +170,12 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
171170 try {
172171 await waitForPlugins ( ai )
173172
173+ // Initialize storage wallets (cheap file reads, needed for both paths):
174+ await Promise . all (
175+ accountWalletInfos . map ( info => addStorageWallet ( ai , info ) )
176+ )
177+ log . warn ( 'Login: synced account repos' )
178+
174179 // Try cache-first login for instant UI:
175180 const cacheSetup = await tryLoadCache ( )
176181 if ( cacheSetup != null ) {
@@ -191,6 +196,10 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
191196 } )
192197 . catch ( ( error : unknown ) => {
193198 log . error ( 'Login: background loading failed:' , error )
199+ input . props . dispatch ( {
200+ type : 'ACCOUNT_LOAD_FAILED' ,
201+ payload : { accountId, error }
202+ } )
194203 } )
195204
196205 return await stopUpdates
@@ -199,12 +208,6 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
199208 // Normal login flow (no cache available):
200209 await loadBuiltinTokens ( ai , accountId )
201210
202- // Start the repo:
203- await Promise . all (
204- accountWalletInfos . map ( info => addStorageWallet ( ai , info ) )
205- )
206- log . warn ( 'Login: synced account repos' )
207-
208211 await loadAllFiles ( )
209212 log . warn ( 'Login: loaded files' )
210213
@@ -342,7 +345,7 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
342345 if ( accountApi != null && cacheSaver == null ) {
343346 const storageWalletId = accountState . accountWalletInfos [ 0 ] ?. id
344347 if ( storageWalletId != null ) {
345- const cachePath = `accountCache/ ${ storageWalletId } /walletCache.json`
348+ const cachePath = getWalletCachePath ( storageWalletId )
346349
347350 cacheSaver = makeWalletCacheSaver (
348351 accountApi ,
0 commit comments