From 67674c0de73e38a79074a70f775c4206a5f89687 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Fri, 12 Jul 2024 16:53:40 -0500 Subject: [PATCH] add a polling interval and a done state --- src/components/Activity.tsx | 2 +- src/routes/settings/ManageFederations.tsx | 71 ++++++++++++++++++++--- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/components/Activity.tsx b/src/components/Activity.tsx index e9781991..588a7d4e 100644 --- a/src/components/Activity.tsx +++ b/src/components/Activity.tsx @@ -1,6 +1,6 @@ import { TagItem } from "@mutinywallet/mutiny-wasm"; import { cache, createAsync, useNavigate } from "@solidjs/router"; -import { Plus, Save, Search, Shuffle, Users } from "lucide-solid"; +import { Plus, Save, Search, Shuffle } from "lucide-solid"; import { createEffect, createMemo, diff --git a/src/routes/settings/ManageFederations.tsx b/src/routes/settings/ManageFederations.tsx index 986fb863..e18a21db 100644 --- a/src/routes/settings/ManageFederations.tsx +++ b/src/routes/settings/ManageFederations.tsx @@ -22,6 +22,7 @@ import { createSignal, For, Match, + onCleanup, onMount, Show, Suspense, @@ -363,6 +364,8 @@ function FederationListItem(props: { } }); + const [shouldPollProgress, setShouldPollProgress] = createSignal(false); + async function resyncFederation() { setResyncLoading(true); try { @@ -370,17 +373,18 @@ function FederationListItem(props: { console.warn("RESYNC STARTED"); + // This loop is so we can try enough times until the resync actually starts for (let i = 0; i < 60; i++) { await timeout(1000); const progress = await sw.get_federation_resync_progress( props.fed.federation_id ); - // FIXME: this only logs once when we start the resync but not after console.log("progress", progress); if (progress?.total !== 0) { setResyncProgress(progress); setResyncOpen(false); + setShouldPollProgress(true); break; } } @@ -390,6 +394,47 @@ function FederationListItem(props: { } } + function refetch() { + sw.get_federation_resync_progress(props.fed.federation_id).then( + (progress) => { + // if the progress is undefined, it also means we're done + if (progress === undefined) { + setResyncProgress({ + total: 100, + complete: 100, + done: true + }); + setShouldPollProgress(false); + setResyncLoading(false); + return; + } else if (progress?.done) { + setShouldPollProgress(false); + setResyncLoading(false); + } + + setResyncProgress(progress); + } + ); + } + + createEffect(() => { + let interval = undefined; + + if (shouldPollProgress()) { + interval = setInterval(() => { + refetch(); + }, 1000); // Poll every second + } + + if (!shouldPollProgress()) { + clearInterval(interval); + } + + onCleanup(() => { + clearInterval(interval); + }); + }); + async function confirmRemove() { setConfirmOpen(true); } @@ -523,14 +568,22 @@ function FederationListItem(props: { title={"Resyncing..."} open={!resyncProgress()!.done} > - This could take a couple of hours. - - DO NOT CLOSE THIS WINDOW UNTIL RESYNC IS DONE. - - + + This could take a couple of hours. + + DO NOT CLOSE THIS WINDOW UNTIL RESYNC IS DONE. + + + + + Resync complete! + +