@@ -160,7 +160,7 @@ export const Provider: ParentComponent = (props) => {
160160 console . error ( e ) ;
161161 }
162162 } ,
163- async setup ( password ?: string ) : Promise < void > {
163+ async preSetup ( ) : Promise < void > {
164164 try {
165165 // If we're already in an error state there should be no reason to continue
166166 if ( state . setup_error ) {
@@ -185,7 +185,13 @@ export const Provider: ParentComponent = (props) => {
185185 await doubleInitDefense ( ) ;
186186 setState ( { load_stage : "downloading" } ) ;
187187 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 {
189195 const settings = await getSettings ( ) ;
190196 setState ( { load_stage : "setup" } ) ;
191197
@@ -203,15 +209,11 @@ export const Provider: ParentComponent = (props) => {
203209 setState ( { needs_password : false } ) ;
204210
205211 // Get balance
206- console . time ( "get_balance" ) ;
207212 const balance = await mutinyWallet . get_balance ( ) ;
208- console . timeEnd ( "get_balance" ) ;
209213
210214 // Get federations
211- console . time ( "list_federations" ) ;
212215 const federations =
213216 ( await mutinyWallet . list_federations ( ) ) as MutinyFederationIdentity [ ] ;
214- console . timeEnd ( "list_federations" ) ;
215217
216218 setState ( {
217219 mutiny_wallet : mutinyWallet ,
@@ -220,6 +222,8 @@ export const Provider: ParentComponent = (props) => {
220222 balance,
221223 federations
222224 } ) ;
225+
226+ await actions . postSetup ( ) ;
223227 } catch ( e ) {
224228 console . error ( e ) ;
225229 if ( eify ( e ) . message === "Incorrect password entered." ) {
@@ -231,7 +235,6 @@ export const Provider: ParentComponent = (props) => {
231235 }
232236 } ,
233237 async postSetup ( ) : Promise < void > {
234- console . time ( "post_setup" ) ;
235238 if ( ! state . mutiny_wallet ) {
236239 console . error (
237240 "Unable to run post setup, no mutiny_wallet is set"
@@ -257,7 +260,23 @@ export const Provider: ParentComponent = (props) => {
257260 } catch ( e ) {
258261 console . error ( "error checking subscription" , e ) ;
259262 }
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
261280 } ,
262281 async deleteMutinyWallet ( ) : Promise < void > {
263282 try {
@@ -474,7 +493,7 @@ export const Provider: ParentComponent = (props) => {
474493 } ) ;
475494 } ) ;
476495
477- onMount ( async ( ) => {
496+ async function checkForExistingTab ( ) {
478497 // Set up existing tab detector
479498 const channel = new BroadcastChannel ( "tab-detector" ) ;
480499
@@ -500,6 +519,13 @@ export const Provider: ParentComponent = (props) => {
500519 channel . postMessage ( { type : "EXISTING_TAB" } ) ;
501520 }
502521 } ;
522+ }
523+
524+ onMount ( async ( ) => {
525+ await checkForExistingTab ( ) ;
526+ if ( state . existing_tab_detected ) {
527+ return ;
528+ }
503529
504530 console . log ( "checking for browser compatibility" ) ;
505531 try {
@@ -509,6 +535,16 @@ export const Provider: ParentComponent = (props) => {
509535 return ;
510536 }
511537
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+
512548 // Setup catches its own errors and sets state itself
513549 console . log ( "running setup node manager" ) ;
514550 if (
@@ -520,37 +556,13 @@ export const Provider: ParentComponent = (props) => {
520556 await actions . setup ( ) ;
521557 } else {
522558 console . warn ( "setup aborted" ) ;
559+ return ;
523560 }
524561
525562 // After we have the mutiny wallet we still need to check for subscription and sync nostr
526- await actions . postSetup ( ) ;
563+ // await actions.postSetup();
527564
528565 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
554566 } ) ;
555567
556568 const store = [ state , actions ] as MegaStore ;
0 commit comments