Skip to content

Commit

Permalink
add a polling interval and a done state
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Jul 12, 2024
1 parent 86e0dd0 commit 122b666
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/Activity.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
61 changes: 52 additions & 9 deletions src/routes/settings/ManageFederations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
createSignal,
For,
Match,
onCleanup,
onMount,
Show,
Suspense,
Expand Down Expand Up @@ -363,24 +364,27 @@ function FederationListItem(props: {
}
});

const [shouldPollProgress, setShouldPollProgress] = createSignal(false);

async function resyncFederation() {
setResyncLoading(true);
try {
await sw.resync_federation(props.fed.federation_id);

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;
}
}
Expand All @@ -390,6 +394,37 @@ function FederationListItem(props: {
}
}

function refetch() {
sw.get_federation_resync_progress(props.fed.federation_id).then(
(progress) => {
setResyncProgress(progress);

if (progress?.done) {
setShouldPollProgress(false);
setResyncLoading(false);
}
}
);
}

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);
}
Expand Down Expand Up @@ -523,14 +558,22 @@ function FederationListItem(props: {
title={"Resyncing..."}
open={!resyncProgress()!.done}
>
<NiceP>This could take a couple of hours.</NiceP>
<NiceP>
DO NOT CLOSE THIS WINDOW UNTIL RESYNC IS DONE.
</NiceP>
<ResyncLoadingBar
value={resyncProgress()!.complete}
max={resyncProgress()!.total}
/>
<Show when={!resyncProgress()!.done}>
<NiceP>This could take a couple of hours.</NiceP>
<NiceP>
DO NOT CLOSE THIS WINDOW UNTIL RESYNC IS DONE.
</NiceP>
<ResyncLoadingBar
value={resyncProgress()!.complete}
max={resyncProgress()!.total}
/>
</Show>
<Show when={resyncProgress()!.done}>
<NiceP>Resync complete!</NiceP>
<Button onClick={() => (window.location.href = "/")}>
Nice
</Button>
</Show>
</SimpleDialog>
</Show>
</>
Expand Down

0 comments on commit 122b666

Please sign in to comment.