From 6073171ff062e1f05165c2059f9b202ad4e92e49 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Thu, 17 Aug 2023 13:04:37 -0500 Subject: [PATCH] refactor onmount --- src/state/megaStore.tsx | 99 ++++++++++++++++++----------------------- vite.config.ts | 4 +- 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/state/megaStore.tsx b/src/state/megaStore.tsx index b76fc2f8..efd92a87 100644 --- a/src/state/megaStore.tsx +++ b/src/state/megaStore.tsx @@ -4,7 +4,6 @@ import { ParentComponent, createContext, - createEffect, onCleanup, onMount, useContext @@ -21,7 +20,6 @@ import { MutinyBalance, MutinyWallet } from "@mutinywallet/mutiny-wasm"; import { MutinyTagItem } from "~/utils/tags"; import { checkBrowserCompatibility } from "~/logic/browserCompatibility"; import eify from "~/utils/eify"; -import { timeout } from "~/utils/timeout"; import { ParsedParams } from "~/logic/waila"; import { subscriptionValid } from "~/utils/subscriptions"; import { useSearchParams } from "solid-start"; @@ -63,7 +61,6 @@ export type MegaStore = [ sync(): Promise; setHasBackedUp(): void; listTags(): Promise; - checkBrowserCompat(): Promise; checkForSubscription(justPaid?: boolean): Promise; } ]; @@ -239,14 +236,6 @@ export const Provider: ParentComponent = (props) => { console.error(e); return []; } - }, - async checkBrowserCompat(): Promise { - try { - return await checkBrowserCompatibility(); - } catch (e) { - setState({ setup_error: eify(e) }); - return false; - } } }; @@ -263,46 +252,8 @@ export const Provider: ParentComponent = (props) => { }); }); - onMount(() => { - if (!state.mutiny_wallet && !state.deleting) { - console.log("checking for browser compatibility..."); - actions.checkBrowserCompat().then(() => { - if (state.existing_tab_detected) { - setState({ - setup_error: new Error( - "Existing tab detected, aborting setup" - ) - }); - } else { - console.log("running setup node manager..."); - - actions - .setup() - .then(() => console.log("node manager setup done")); - - // Setup an event listener to stop the mutiny wallet when the page unloads - window.onunload = async (_e) => { - console.log("stopping mutiny_wallet"); - await state.mutiny_wallet?.stop(); - console.log("mutiny_wallet stopped"); - sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED"); - }; - } - }); - } - }); - - createEffect(() => { - const interval = setInterval(async () => { - await actions.sync(); - }, 3 * 1000); // Poll every 3 seconds - - onCleanup(() => { - clearInterval(interval); - }); - }); - - onMount(() => { + onMount(async () => { + // Set up existing tab detector const channel = new BroadcastChannel("tab-detector"); // First we let everyone know we exist @@ -312,7 +263,13 @@ export const Provider: ParentComponent = (props) => { // If any tabs reply, we know there's an existing tab so abort setup if (e.data.type === "EXISTING_TAB") { console.debug("there's an existing tab"); - setState({ existing_tab_detected: true }); + setState({ + existing_tab_detected: true, + setup_error: new Error( + "Existing tab detected, aborting setup" + ) + }); + return; } // If we get notified of a new tab, we let it know we exist @@ -322,9 +279,41 @@ export const Provider: ParentComponent = (props) => { } }; - onCleanup(() => { - channel.close(); - }); + console.log("checking for browser compatibility"); + try { + await checkBrowserCompatibility(); + } catch (e) { + setState({ setup_error: eify(e) }); + return; + } + + // Setup catches its own errors and sets state itself + console.log("running setup node manager"); + if ( + !state.mutiny_wallet && + !state.deleting && + !state.setup_error && + !state.existing_tab_detected + ) { + await actions.setup(); + } else { + console.warn("setup aborted"); + } + + console.log("node manager setup done"); + + // Setup an event listener to stop the mutiny wallet when the page unloads + window.onunload = async (_e) => { + console.log("stopping mutiny_wallet"); + await state.mutiny_wallet?.stop(); + console.log("mutiny_wallet stopped"); + sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED"); + }; + + // Set up syncing + setInterval(async () => { + await actions.sync(); + }, 3 * 1000); // Poll every 3 seconds }); const store = [state, actions] as MegaStore; diff --git a/vite.config.ts b/vite.config.ts index 439d1fc2..f3a47d1a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -60,7 +60,9 @@ export default defineConfig({ "@kobalte/core", "@solid-primitives/upload", "i18next", - "i18next-browser-languagedetector" + "i18next-browser-languagedetector", + "@capacitor/clipboard", + "@capacitor/core" ], // This is necessary because otherwise `vite dev` can't find the wasm exclude: ["@mutinywallet/mutiny-wasm", "@mutinywallet/waila-wasm"]