treaty: more robust 'not found' logic

This commit is contained in:
Hunter Miller 2023-06-23 19:17:14 -05:00
parent a5d20d0732
commit b96b8750b2
4 changed files with 44 additions and 32 deletions

View File

@ -6,8 +6,7 @@ export type DocketHref = DocketHrefSite | DocketHrefGlob;
export interface DocketHrefGlob {
glob: {
base: string;
}
};
}
export interface DocketHrefSite {
@ -43,7 +42,6 @@ export interface SiteChad {
}
export interface InstallChad {
install: null;
}
export interface SuspendChad {
suspend: null;
@ -61,7 +59,7 @@ export interface Charges {
}
export interface Treaties {
[ref: string]: Treaty;
[ref: string]: Treaty | null;
}
export type Charter = string[];
@ -76,31 +74,38 @@ export interface Provider {
status?: string;
}
export type ChargeUpdate = ChargeUpdateInitial | ChargeUpdateAdd | ChargeUpdateDel;
export type ChargeUpdate =
| ChargeUpdateInitial
| ChargeUpdateAdd
| ChargeUpdateDel;
export interface ChargeUpdateInitial {
initial: {
[desk: string]: Charge;
}
};
}
export interface ChargeUpdateAdd {
'add-charge': {
desk: string;
charge: Charge;
}
};
}
export interface ChargeUpdateDel {
'del-charge': string;
}
export type AllyUpdate = AllyUpdateIni | AllyUpdateAdd | AllyUpdateDel | AllyUpdateNew;
export type AllyUpdate =
| AllyUpdateIni
| AllyUpdateAdd
| AllyUpdateDel
| AllyUpdateNew;
export interface AllyUpdateIni {
ini: {
[ship: string]: string[];
}
};
}
export interface AllyUpdateAdd {
@ -115,7 +120,7 @@ export interface AllyUpdateNew {
new: {
ship: string;
alliance: string[];
}
};
}
export type TreatyUpdate = TreatyUpdateIni | TreatyUpdateAdd | TreatyUpdateDel;
@ -123,7 +128,7 @@ export type TreatyUpdate = TreatyUpdateIni | TreatyUpdateAdd | TreatyUpdateDel;
export interface TreatyUpdateIni {
ini: {
[foreignDesk: string]: Treaty;
}
};
}
export interface TreatyUpdateAdd {

View File

@ -1,4 +1,11 @@
import { Yarn, isYarnEmph, isYarnShip, Docket, DocketHref, Treaty } from '@/gear';
import {
Yarn,
isYarnEmph,
isYarnShip,
Docket,
DocketHref,
Treaty,
} from '@/gear';
import { findLast } from 'lodash';
import { hsla, parseToHsla, parseToRgba } from 'color2k';
import _ from 'lodash';
@ -127,3 +134,14 @@ export function randomElement<T>(a: T[]) {
export function randomIntInRange(min: number, max: number) {
return Math.round(Math.random() * (max - min) + min);
}
export async function asyncWithDefault<T>(
cb: () => Promise<T>,
def: T
): Promise<T> {
try {
return await cb();
} catch (error) {
return def;
}
}

View File

@ -18,17 +18,7 @@ export const TreatyInfo = () => {
const charge = useCharge(desk);
const name = treaty ? getAppName(treaty) : `${host}/${desk}`;
const { data, showConnection } = useConnectivityCheck(host);
// TODO replace with checking alliance to determine if still available
const [unavailable, setUnavailable] = React.useState(false);
const treatyNotFound =
!treaty && unavailable && data && 'complete' in data.status;
useEffect(() => {
const timeout = setTimeout(() => {
setUnavailable(true);
}, 20 * 1000); // matches timeout in requestTreaty
return () => clearTimeout(timeout);
}, []);
const treatyNotFound = treaty === null && data && 'complete' in data.status;
useEffect(() => {
if (!charge) {

View File

@ -23,7 +23,7 @@ import {
allyShip,
} from '@/gear';
import api from '@/api';
import { normalizeUrbitColor } from '@/logic/utils';
import { asyncWithDefault, normalizeUrbitColor } from '@/logic/utils';
import { Status } from '@/logic/useAsyncCall';
import { ConnectionStatus, useConnectivityCheck } from './vitals';
@ -46,7 +46,7 @@ interface DocketState {
defaultAlly: string | null;
fetchCharges: () => Promise<void>;
fetchDefaultAlly: () => Promise<void>;
requestTreaty: (ship: string, desk: string) => Promise<Treaty>;
requestTreaty: (ship: string, desk: string) => Promise<void>;
fetchAllies: () => Promise<Allies>;
fetchAllyTreaties: (ally: string) => Promise<Treaties>;
toggleDocket: (desk: string) => Promise<void>;
@ -94,19 +94,18 @@ const useDocketState = create<DocketState>((set, get) => ({
const key = `${ship}/${desk}`;
if (key in treaties) {
return treaties[key];
return;
}
const result = await api.subscribeOnce<Treaty>(
'treaty',
`/treaty/${key}`,
20000
const result = await asyncWithDefault(
() => api.subscribeOnce<Treaty>('treaty', `/treaty/${key}`, 20000),
null
);
const treaty = { ...normalizeDocket(result, desk), ship };
const treaty = result ? { ...normalizeDocket(result, desk), ship } : null;
set((state) => ({
treaties: { ...state.treaties, [key]: treaty },
}));
return treaty;
},
installDocket: async (ship: string, desk: string) => {
const treaty = get().treaties[`${ship}/${desk}`];