interface: fix reconnect backoff

This commit is contained in:
Liam Fitzgerald 2021-06-30 14:09:58 +10:00
parent 0a29d95cb8
commit daea523a93
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
4 changed files with 10 additions and 26 deletions

View File

@ -14,10 +14,10 @@ export async function bootstrapApi() {
(async () => {
const { reconnect } = useLocalState.getState();
try {
useLocalState.setState({ subscription: 'reconnecting' });
await reconnect();
} catch (e) {
useLocalState.setState({ subscription: 'disconnected' });
console.log(e);
console.log('onError');
}
})();
};

View File

@ -97,24 +97,23 @@ const useLocalState = create<LocalStateZus>(persist((set, get) => ({
// resume doesn't work properly
reconnect: async () => {
const { errorCount } = get();
set(s => ({ errorCount: s.errorCount+1, subscription: 'reconnecting' }));
if(errorCount > 5) {
set({ subscription: 'disconnected' });
return;
}
await wait(Math.pow(2, errorCount) * 750);
try {
airlock.reset();
await bootstrapApi();
} catch (e) {
await wait(Math.pow(2, errorCount) * 750);
set({ errorCount: errorCount + 1 });
const { reconnect } = get();
await reconnect();
console.error(`Retrying connection, attempt #${errorCount}`);
}
},
bootstrap: async () => {
set({ subscription: 'reconnecting' });
set({ subscription: 'reconnecting', errorCount: 0 });
airlock.reset();
await bootstrapApi();
set({ subscription: 'connected' });
@ -124,7 +123,8 @@ const useLocalState = create<LocalStateZus>(persist((set, get) => ({
}), {
blacklist: [
'suspendedFocus', 'toggleOmnibox', 'omniboxShown', 'tutorialProgress',
'prevTutStep', 'nextTutStep', 'tutorialRef', 'setTutorialRef', 'subscription'
'prevTutStep', 'nextTutStep', 'tutorialRef', 'setTutorialRef', 'subscription',
'errorCount'
],
name: 'localReducer'
}));

View File

@ -1,28 +1,13 @@
import { Button, LoadingSpinner, Text } from '@tlon/indigo-react';
import React from 'react';
import useLocalState from '~/logic/state/local';
import api from '~/logic/api';
const ReconnectButton = () => {
const { set, subscription } = useLocalState();
const reconnect = () => {
(async () => {
try {
await api.eventSource();
set((state) => {
state.subscription = 'connected';
});
} catch (e) {
set((state) => {
state.subscription = 'connected';
});
}
})();
};
const { subscription, bootstrap } = useLocalState();
if (subscription === 'disconnected') {
return (
<Button onClick={reconnect} borderColor='red' px={2}>
<Button onClick={bootstrap} borderColor='red' px={2}>
<Text display={['none', 'inline']} textAlign='center' color='red'>Reconnect</Text>
<Text color='red'> </Text>
</Button>

View File

@ -193,7 +193,6 @@ export class Urbit {
return; // everything's good
} else {
reject();
throw new Error();
}
},
onmessage: (event: EventSourceMessage) => {