From 2a6a48af69cd412dcfc36a1e19b5b52aad967e90 Mon Sep 17 00:00:00 2001 From: Hunter Miller Date: Fri, 13 Oct 2023 15:24:49 -0500 Subject: [PATCH] reconnect: bootstrap on channel reap and delay aux data when reconnecting --- ui/src/api.ts | 7 ++--- ui/src/state/bootstrap.ts | 60 ++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/ui/src/api.ts b/ui/src/api.ts index b0448e00d7..f7cebc60e3 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -111,9 +111,6 @@ class API { if (onReconnect) { onReconnect(); } - useLocalState.setState((state) => ({ - subscription: 'connected', - })); }; this.client.onRetry = () => { @@ -258,7 +255,9 @@ class API { // should only happen once since we call this each invocation // and onReconnect will set the lastReconnect time const { lastReconnect, onReconnect } = useLocalState.getState(); - const threshold = 12 * 60 * 60 * 1000; // 12 hours + const threshold = import.meta.env.DEV + ? 60 * 1000 + : 12 * 60 * 60 * 1000; // 12 hours if (Date.now() - lastReconnect >= threshold && onReconnect) { onReconnect(); } diff --git a/ui/src/state/bootstrap.ts b/ui/src/state/bootstrap.ts index 1995549cef..e116c85440 100644 --- a/ui/src/state/bootstrap.ts +++ b/ui/src/state/bootstrap.ts @@ -118,20 +118,8 @@ async function startTalk(groupsStarted: boolean) { type Bootstrap = 'initial' | 'reset' | 'full-reset'; -export default async function bootstrap(reset = 'initial' as Bootstrap) { +function auxiliaryData() { const { wait } = useSchedulerStore.getState(); - if (reset === 'full-reset') { - api.reset(); - } - - if (isTalk) { - startTalk(false); - wait(() => startGroups(true), 5); - } else { - startGroups(false); - wait(async () => startTalk(true), 5); - } - wait(() => { useContactState.getState().start(); useStorage.getState().initialize(api as unknown as Urbit); @@ -147,12 +135,43 @@ export default async function bootstrap(reset = 'initial' as Bootstrap) { if (!import.meta.env.DEV) { usePalsState.getState().initializePals(); } - api.poke({ - app: isTalk ? 'talk-ui' : 'groups-ui', - mark: 'ui-vita', - json: null, - }); }, 5); + + api.poke({ + app: isTalk ? 'talk-ui' : 'groups-ui', + mark: 'ui-vita', + json: null, + }); +} + +let auxiliaryTimer = 0; +export default async function bootstrap( + reset = 'initial' as Bootstrap, + sendVita = true +) { + const { wait } = useSchedulerStore.getState(); + + if (reset === 'full-reset') { + api.reset(); + } + + if (isTalk) { + startTalk(false); + wait(() => startGroups(true), 5); + } else { + startGroups(false); + wait(async () => startTalk(true), 5); + } + + if (reset === 'initial') { + auxiliaryData(); + } else { + clearTimeout(auxiliaryTimer); + auxiliaryTimer = setTimeout( + () => auxiliaryData(), + 30 * 1000 + ) as unknown as number; + } } useLocalState.setState({ @@ -161,6 +180,9 @@ useLocalState.setState({ reset(); bootstrap('reset'); - useLocalState.setState({ lastReconnect: Date.now() }); + useLocalState.setState({ + lastReconnect: Date.now(), + subscription: 'connected', + }); }, });