mirror of
https://github.com/ilyakooo0/urbit.git
synced 2025-01-03 04:40:50 +03:00
interface: fix reconnect backoff
This commit is contained in:
parent
0a29d95cb8
commit
daea523a93
@ -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');
|
||||
}
|
||||
})();
|
||||
};
|
||||
|
@ -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'
|
||||
}));
|
||||
|
@ -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>
|
||||
|
@ -193,7 +193,6 @@ export class Urbit {
|
||||
return; // everything's good
|
||||
} else {
|
||||
reject();
|
||||
throw new Error();
|
||||
}
|
||||
},
|
||||
onmessage: (event: EventSourceMessage) => {
|
||||
|
Loading…
Reference in New Issue
Block a user