Skip to content

Commit

Permalink
reconnect: bootstrap on channel reap and delay aux data when reconnec…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
arthyn committed Oct 13, 2023
1 parent 7d8befb commit 2a6a48a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
7 changes: 3 additions & 4 deletions ui/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ class API {
if (onReconnect) {
onReconnect();
}
useLocalState.setState((state) => ({
subscription: 'connected',
}));
};

this.client.onRetry = () => {
Expand Down Expand Up @@ -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();
}
Expand Down
60 changes: 41 additions & 19 deletions ui/src/state/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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({
Expand All @@ -161,6 +180,9 @@ useLocalState.setState({
reset();
bootstrap('reset');

useLocalState.setState({ lastReconnect: Date.now() });
useLocalState.setState({
lastReconnect: Date.now(),
subscription: 'connected',
});
},
});

0 comments on commit 2a6a48a

Please sign in to comment.