diff --git a/src/logic/mutinyWalletSetup.ts b/src/logic/mutinyWalletSetup.ts index d92319c..6e9cde7 100644 --- a/src/logic/mutinyWalletSetup.ts +++ b/src/logic/mutinyWalletSetup.ts @@ -1,10 +1,8 @@ +/* @refresh reload */ + import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm"; import initWaila from "@mutinywallet/waila-wasm"; -// export type MutinyWalletSettingStrings = { -// network?: string, proxy?: string, esplora?: string, rgs?: string, lsp?: string, -// } - export type Network = "bitcoin" | "testnet" | "regtest" | "signet"; export type MutinyWalletSettingStrings = { network?: Network; @@ -92,11 +90,23 @@ export async function checkForWasm() { export async function setupMutinyWallet( settings?: MutinyWalletSettingStrings ): Promise { + // Ultimate defense against getting multiple instances of the wallet running. + // If we detect that the wallet has already been initialized in this session, we'll reload the page. + // A successful stop of the wallet in onCleanup will clear this flag + if (sessionStorage.getItem("MUTINY_WALLET_INITIALIZED")) { + console.error( + `Mutiny Wallet already initialized at ${sessionStorage.getItem( + "MUTINY_WALLET_INITIALIZED" + )}. Reloading page.` + ); + sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED"); + window.location.reload(); + } + await initMutinyWallet(); // Might as well init waila while we're at it await initWaila(); - console.time("Setup"); console.log("Starting setup..."); const { network, proxy, esplora, rgs, lsp } = await setAndGetMutinySettings( settings @@ -125,5 +135,7 @@ export async function setupMutinyWallet( await mutinyWallet?.new_node(); } + sessionStorage.setItem("MUTINY_WALLET_INITIALIZED", Date.now().toString()); + return mutinyWallet; } diff --git a/src/state/megaStore.tsx b/src/state/megaStore.tsx index 44c47d9..5a3723a 100644 --- a/src/state/megaStore.tsx +++ b/src/state/megaStore.tsx @@ -225,7 +225,16 @@ export const Provider: ParentComponent = (props) => { }; onCleanup(() => { - console.warn('Parent Component is being unmounted!!!'); + console.warn("Parent Component is being unmounted!!!"); + state.mutiny_wallet + ?.stop() + .then(() => { + console.warn("Successfully stopped mutiny wallet"); + sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED"); + }) + .catch((e) => { + console.error("Error stopping mutiny wallet", e); + }); }); // Fetch status from remote on load