Merge pull request #5686 from urbit/lf/fix-reconnects-three-point-oh-you-cannot-redo

interface: fix reconnect bugs
This commit is contained in:
Hunter Miller 2022-03-31 17:24:15 -05:00 committed by GitHub
commit 51c65247fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 10 deletions

View File

@ -12,9 +12,17 @@ import useStorageState from '../state/storage';
import gcpManager from '../lib/gcpManager';
export async function bootstrapApi() {
airlock.reset();
airlock.onError = (e) => {
(async () => {
const { reconnect } = useLocalState.getState();
const { reconnect, errorCount, set } = useLocalState.getState();
console.log(errorCount);
if(errorCount > 1) {
set(s => {
s.subscription = 'disconnected';
});
return;
}
try {
await reconnect();
} catch (e) {

View File

@ -1,7 +1,7 @@
import Urbit from '@urbit/http-api';
const api = new Urbit('', '', (window as any).desk);
api.ship = window.ship;
// api.verbose = true;
api.verbose = true;
// @ts-ignore TODO window typings
window.api = api;

View File

@ -82,19 +82,17 @@ 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' });
if(errorCount > 1) {
return;
}
await wait(Math.pow(2, errorCount) * 750);
set(s => ({ subscription: 'reconnecting', errorCount: s.errorCount + 1 }));
console.log(get().errorCount);
try {
airlock.reset();
await bootstrapApi();
} catch (e) {
console.error(`Retrying connection, attempt #${errorCount}`);
console.error(e);
set({ subscription: 'disconnected' });
}
},
bootstrap: async () => {

View File

@ -14,6 +14,7 @@ import {
SSEOptions,
PokeHandlers,
Message,
FatalError,
} from './types';
import { hexString } from './utils';
@ -291,7 +292,7 @@ export class Urbit {
},
onerror: (error) => {
console.warn(error);
if (this.errorCount++ < 4) {
if (!(error instanceof FatalError) && this.errorCount++ < 4) {
this.onRetry && this.onRetry();
return Math.pow(2, this.errorCount - 1) * 750;
}
@ -311,6 +312,9 @@ export class Urbit {
*
*/
reset() {
if(this.verbose) {
console.log('resetting');
}
this.delete();
this.abort.abort();
this.abort = new AbortController();