Skip to content

Commit

Permalink
move nostr sync and sub check to post-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Feb 6, 2024
1 parent 0051b1c commit 38856f7
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions src/state/megaStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,22 @@ export const Provider: ParentComponent = (props) => {
state.should_zap_hodl
);

// Sync our nostr profile info
try {
await mutinyWallet.sync_nostr();
} catch (e) {
console.error("error syncing nostr profile", e);
}

// Give other components access to settings via the store
setState({ settings: settings });

// If we get this far then we don't need the password anymore
setState({ needs_password: false });

// Check if we're subscribed and update the timestamp
try {
const timestamp = await mutinyWallet?.check_subscribed();

// Check that timestamp is a number
if (timestamp && !isNaN(Number(timestamp))) {
setState({ subscription_timestamp: Number(timestamp) });
}
} catch (e) {
console.error("error checking subscription", e);
}

// Get balance
console.time("get_balance");
const balance = await mutinyWallet.get_balance();
console.timeEnd("get_balance");

// Get federations
console.time("list_federations");
const federations =
(await mutinyWallet.list_federations()) as MutinyFederationIdentity[];
console.timeEnd("list_federations");

setState({
mutiny_wallet: mutinyWallet,
Expand All @@ -255,6 +240,40 @@ export const Provider: ParentComponent = (props) => {
}
}
},
async postSetup(): Promise<void> {
console.time("post_setup");
if (!state.mutiny_wallet) {
console.error(
"Unable to run post setup, no mutiny_wallet is set"
);
setState({
setup_error: new Error(
"Attempted post-setup without a Mutiny Wallet initialized."
)
});
return;
}

// Sync our nostr profile info
try {
await state.mutiny_wallet.sync_nostr();
} catch (e) {
console.error("error syncing nostr profile", e);
}

// Check if we're subscribed and update the timestamp
try {
const timestamp = await state.mutiny_wallet.check_subscribed();

// Check that timestamp is a number
if (timestamp && !isNaN(Number(timestamp))) {
setState({ subscription_timestamp: Number(timestamp) });
}
} catch (e) {
console.error("error checking subscription", e);
}
console.timeEnd("post_setup");
},
setShouldCreateNewWallet(shouldCreateNewWallet: boolean) {
setState({ shouldCreateNewWallet });
},
Expand Down Expand Up @@ -509,6 +528,9 @@ export const Provider: ParentComponent = (props) => {
console.warn("setup aborted");
}

// After we have the mutiny wallet we still need to check for subscription and sync nostr
await actions.postSetup();

console.log("node manager setup done");

// Setup an event listener to stop the mutiny wallet when the page unloads
Expand Down

0 comments on commit 38856f7

Please sign in to comment.