@@ -160,7 +160,7 @@ export const Provider: ParentComponent = (props) => {
160
160
console . error ( e ) ;
161
161
}
162
162
} ,
163
- async setup ( password ?: string ) : Promise < void > {
163
+ async preSetup ( ) : Promise < void > {
164
164
try {
165
165
// If we're already in an error state there should be no reason to continue
166
166
if ( state . setup_error ) {
@@ -185,7 +185,13 @@ export const Provider: ParentComponent = (props) => {
185
185
await doubleInitDefense ( ) ;
186
186
setState ( { load_stage : "downloading" } ) ;
187
187
await initializeWasm ( ) ;
188
-
188
+ } catch ( e ) {
189
+ console . error ( e ) ;
190
+ setState ( { setup_error : eify ( e ) } ) ;
191
+ }
192
+ } ,
193
+ async setup ( password ?: string ) : Promise < void > {
194
+ try {
189
195
const settings = await getSettings ( ) ;
190
196
setState ( { load_stage : "setup" } ) ;
191
197
@@ -203,15 +209,11 @@ export const Provider: ParentComponent = (props) => {
203
209
setState ( { needs_password : false } ) ;
204
210
205
211
// Get balance
206
- console . time ( "get_balance" ) ;
207
212
const balance = await mutinyWallet . get_balance ( ) ;
208
- console . timeEnd ( "get_balance" ) ;
209
213
210
214
// Get federations
211
- console . time ( "list_federations" ) ;
212
215
const federations =
213
216
( await mutinyWallet . list_federations ( ) ) as MutinyFederationIdentity [ ] ;
214
- console . timeEnd ( "list_federations" ) ;
215
217
216
218
setState ( {
217
219
mutiny_wallet : mutinyWallet ,
@@ -220,6 +222,8 @@ export const Provider: ParentComponent = (props) => {
220
222
balance,
221
223
federations
222
224
} ) ;
225
+
226
+ await actions . postSetup ( ) ;
223
227
} catch ( e ) {
224
228
console . error ( e ) ;
225
229
if ( eify ( e ) . message === "Incorrect password entered." ) {
@@ -231,7 +235,6 @@ export const Provider: ParentComponent = (props) => {
231
235
}
232
236
} ,
233
237
async postSetup ( ) : Promise < void > {
234
- console . time ( "post_setup" ) ;
235
238
if ( ! state . mutiny_wallet ) {
236
239
console . error (
237
240
"Unable to run post setup, no mutiny_wallet is set"
@@ -257,7 +260,23 @@ export const Provider: ParentComponent = (props) => {
257
260
} catch ( e ) {
258
261
console . error ( "error checking subscription" , e ) ;
259
262
}
260
- console . timeEnd ( "post_setup" ) ;
263
+
264
+ // Set up syncing
265
+ setInterval ( async ( ) => {
266
+ await actions . sync ( ) ;
267
+ } , 3 * 1000 ) ; // Poll every 3 seconds
268
+
269
+ // Run our first price check
270
+ console . log ( "running first price check" ) ;
271
+ await actions . priceCheck ( ) ;
272
+
273
+ // Set up price checking every minute
274
+ setInterval (
275
+ async ( ) => {
276
+ await actions . priceCheck ( ) ;
277
+ } ,
278
+ 60 * 1000 * state . price_sync_backoff_multiple
279
+ ) ; // Poll every minute * backoff multiple
261
280
} ,
262
281
async deleteMutinyWallet ( ) : Promise < void > {
263
282
try {
@@ -474,7 +493,7 @@ export const Provider: ParentComponent = (props) => {
474
493
} ) ;
475
494
} ) ;
476
495
477
- onMount ( async ( ) => {
496
+ async function checkForExistingTab ( ) {
478
497
// Set up existing tab detector
479
498
const channel = new BroadcastChannel ( "tab-detector" ) ;
480
499
@@ -500,6 +519,13 @@ export const Provider: ParentComponent = (props) => {
500
519
channel . postMessage ( { type : "EXISTING_TAB" } ) ;
501
520
}
502
521
} ;
522
+ }
523
+
524
+ onMount ( async ( ) => {
525
+ await checkForExistingTab ( ) ;
526
+ if ( state . existing_tab_detected ) {
527
+ return ;
528
+ }
503
529
504
530
console . log ( "checking for browser compatibility" ) ;
505
531
try {
@@ -509,6 +535,16 @@ export const Provider: ParentComponent = (props) => {
509
535
return ;
510
536
}
511
537
538
+ await actions . preSetup ( ) ;
539
+
540
+ setState ( { load_stage : "checking_for_existing_wallet" } ) ;
541
+ const existing = await MutinyWallet . has_node_manager ( ) ;
542
+
543
+ if ( ! existing ) {
544
+ navigate ( "/setup" ) ;
545
+ return ;
546
+ }
547
+
512
548
// Setup catches its own errors and sets state itself
513
549
console . log ( "running setup node manager" ) ;
514
550
if (
@@ -520,37 +556,13 @@ export const Provider: ParentComponent = (props) => {
520
556
await actions . setup ( ) ;
521
557
} else {
522
558
console . warn ( "setup aborted" ) ;
559
+ return ;
523
560
}
524
561
525
562
// After we have the mutiny wallet we still need to check for subscription and sync nostr
526
- await actions . postSetup ( ) ;
563
+ // await actions.postSetup();
527
564
528
565
console . log ( "node manager setup done" ) ;
529
-
530
- // Setup an event listener to stop the mutiny wallet when the page unloads
531
- window . onunload = async ( _e ) => {
532
- console . log ( "stopping mutiny_wallet" ) ;
533
- await state . mutiny_wallet ?. stop ( ) ;
534
- console . log ( "mutiny_wallet stopped" ) ;
535
- sessionStorage . removeItem ( "MUTINY_WALLET_INITIALIZED" ) ;
536
- } ;
537
-
538
- // Set up syncing
539
- setInterval ( async ( ) => {
540
- await actions . sync ( ) ;
541
- } , 3 * 1000 ) ; // Poll every 3 seconds
542
-
543
- // Run our first price check
544
- console . log ( "running first price check" ) ;
545
- await actions . priceCheck ( ) ;
546
-
547
- // Set up price checking every minute
548
- setInterval (
549
- async ( ) => {
550
- await actions . priceCheck ( ) ;
551
- } ,
552
- 60 * 1000 * state . price_sync_backoff_multiple
553
- ) ; // Poll every minute * backoff multiple
554
566
} ) ;
555
567
556
568
const store = [ state , actions ] as MegaStore ;
0 commit comments