Fix book loading (#1086)

* Fix book loading

* Fix Robot page

* Code Review
This commit is contained in:
KoalaSat
2024-01-24 00:30:16 +01:00
committed by GitHub
parent 30f7c047c6
commit c648132163
6 changed files with 46 additions and 29 deletions

View File

@ -65,11 +65,10 @@ const RobotPage = (): JSX.Element => {
setInputToken(token); setInputToken(token);
genKey(token) genKey(token)
.then((key) => { .then((key) => {
garage.createRobot(token, sortedCoordinators[0], { garage.createRobot(token, sortedCoordinators, {
token, token,
pubKey: key.publicKeyArmored, pubKey: key.publicKeyArmored,
encPrivKey: key.encryptedPrivateKeyArmored, encPrivKey: key.encryptedPrivateKeyArmored,
shortAlias: sortedCoordinators[0],
}); });
void federation.fetchRobot(garage, token); void federation.fetchRobot(garage, token);
garage.currentSlot = token; garage.currentSlot = token;

View File

@ -113,9 +113,6 @@ const BookTable = ({
const { t } = useTranslation(); const { t } = useTranslation();
const theme = useTheme(); const theme = useTheme();
const orders = orderList ?? federation.book; const orders = orderList ?? federation.book;
const loadingProgress = useMemo(() => {
return (federation.exchange.onlineCoordinators / federation.exchange.totalCoordinators) * 100;
}, [coordinatorUpdatedAt]);
const [paginationModel, setPaginationModel] = useState<GridPaginationModel>({ const [paginationModel, setPaginationModel] = useState<GridPaginationModel>({
pageSize: 0, pageSize: 0,
@ -942,7 +939,11 @@ const BookTable = ({
}, },
loadingOverlay: { loadingOverlay: {
variant: 'determinate', variant: 'determinate',
value: loadingProgress, value:
((federation.exchange.enabledCoordinators -
federation.exchange.loadingCoordinators) /
federation.exchange.enabledCoordinators) *
100,
}, },
}} }}
paginationModel={paginationModel} paginationModel={paginationModel}

View File

@ -61,13 +61,17 @@ export const updateExchangeInfo = (federation: Federation): ExchangeInfo => {
export interface Exchange { export interface Exchange {
info: ExchangeInfo; info: ExchangeInfo;
enabledCoordinators: number;
onlineCoordinators: number; onlineCoordinators: number;
loadingCoordinators: number;
totalCoordinators: number; totalCoordinators: number;
} }
export const defaultExchange: Exchange = { export const defaultExchange: Exchange = {
info: defaultExchangeInfo, info: defaultExchangeInfo,
enabledCoordinators: 0,
onlineCoordinators: 0, onlineCoordinators: 0,
loadingCoordinators: 0,
totalCoordinators: 0, totalCoordinators: 0,
}; };

View File

@ -61,8 +61,10 @@ export class Federation {
this.book = Object.values(this.coordinators).reduce<PublicOrder[]>((array, coordinator) => { this.book = Object.values(this.coordinators).reduce<PublicOrder[]>((array, coordinator) => {
return [...array, ...coordinator.book]; return [...array, ...coordinator.book];
}, []); }, []);
this.loading = false;
this.triggerHook('onCoordinatorUpdate'); this.triggerHook('onCoordinatorUpdate');
this.exchange.loadingCoordinators =
this.exchange.loadingCoordinators < 1 ? 0 : this.exchange.loadingCoordinators - 1;
this.loading = this.exchange.loadingCoordinators > 0;
if (Object.values(this.coordinators).every((coor) => coor.isUpdated())) { if (Object.values(this.coordinators).every((coor) => coor.isUpdated())) {
this.updateExchange(); this.updateExchange();
this.triggerHook('onFederationReady'); this.triggerHook('onFederationReady');
@ -76,9 +78,13 @@ export class Federation {
this.onCoordinatorSaved(); this.onCoordinatorSaved();
}; };
this.loading = true; this.loading = true;
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
for (const coor of Object.values(this.coordinators)) { for (const coor of Object.values(this.coordinators)) {
if (coor.enabled) {
await coor.start(origin, settings, hostUrl, onCoordinatorStarted); await coor.start(origin, settings, hostUrl, onCoordinatorStarted);
} }
}
this.updateEnabledCoordinators();
}; };
// On Testnet/Mainnet change // On Testnet/Mainnet change
@ -87,10 +93,12 @@ export class Federation {
for (const coor of Object.values(this.coordinators)) { for (const coor of Object.values(this.coordinators)) {
await coor.updateUrl(settings, origin, hostUrl); await coor.updateUrl(settings, origin, hostUrl);
} }
this.loading = false;
}; };
update = async (): Promise<void> => { update = async (): Promise<void> => {
this.loading = false; this.loading = true;
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
for (const coor of Object.values(this.coordinators)) { for (const coor of Object.values(this.coordinators)) {
await coor.update(() => { await coor.update(() => {
this.onCoordinatorSaved(); this.onCoordinatorSaved();
@ -99,7 +107,8 @@ export class Federation {
}; };
updateBook = async (): Promise<void> => { updateBook = async (): Promise<void> => {
this.loading = false; this.loading = true;
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
for (const coor of Object.values(this.coordinators)) { for (const coor of Object.values(this.coordinators)) {
await coor.updateBook(() => { await coor.updateBook(() => {
this.onCoordinatorSaved(); this.onCoordinatorSaved();
@ -125,14 +134,22 @@ export class Federation {
disableCoordinator = (shortAlias: string): void => { disableCoordinator = (shortAlias: string): void => {
this.coordinators[shortAlias].disable(); this.coordinators[shortAlias].disable();
this.updateEnabledCoordinators();
this.triggerHook('onCoordinatorUpdate'); this.triggerHook('onCoordinatorUpdate');
}; };
enableCoordinator = (shortAlias: string): void => { enableCoordinator = (shortAlias: string): void => {
this.coordinators[shortAlias].enable(() => { this.coordinators[shortAlias].enable(() => {
this.updateEnabledCoordinators();
this.triggerHook('onCoordinatorUpdate'); this.triggerHook('onCoordinatorUpdate');
}); });
}; };
updateEnabledCoordinators = (): void => {
this.exchange.enabledCoordinators = Object.values(this.coordinators).filter(
(c) => c.enabled,
).length;
};
} }
export default Federation; export default Federation;

View File

@ -59,10 +59,13 @@ class Garage {
const rawSlots = JSON.parse(slotsDump); const rawSlots = JSON.parse(slotsDump);
Object.values(rawSlots).forEach((rawSlot: Record<any, any>) => { Object.values(rawSlots).forEach((rawSlot: Record<any, any>) => {
if (rawSlot?.token) { if (rawSlot?.token) {
this.slots[rawSlot.token] = new Slot(rawSlot.token, Object.keys(rawSlot.robots), {});
Object.keys(rawSlot.robots).forEach((shortAlias) => { Object.keys(rawSlot.robots).forEach((shortAlias) => {
const rawRobot = rawSlot.robots[shortAlias]; const rawRobot = rawSlot.robots[shortAlias];
this.createRobot(rawRobot.token, shortAlias, rawRobot); this.updateRobot(rawSlot.token, shortAlias, rawRobot);
}); });
this.currentSlot = rawSlot?.token; this.currentSlot = rawSlot?.token;
} }
}); });
@ -102,16 +105,15 @@ class Garage {
}; };
// Robots // Robots
createRobot: (token: string, shortAlias: string, attributes: Record<any, any>) => void = ( createRobot: (token: string, shortAliases: string[], attributes: Record<any, any>) => void = (
token, token,
shortAlias, shortAliases,
attributes, attributes,
) => { ) => {
if (!token || !shortAlias) return; if (!token || !shortAliases) return;
if (this.getSlot(token) === null) { if (this.getSlot(token) === null) {
this.slots[token] = new Slot(token); this.slots[token] = new Slot(token, shortAliases, attributes);
this.slots[token]?.createRobot(shortAlias, attributes);
this.save(); this.save();
this.triggerHook('onRobotUpdate'); this.triggerHook('onRobotUpdate');
} }

View File

@ -4,7 +4,7 @@ import { robohash } from '../components/RobotAvatar/RobohashGenerator';
import { generate_roboname } from 'robo-identities-wasm'; import { generate_roboname } from 'robo-identities-wasm';
class Slot { class Slot {
constructor(token: string) { constructor(token: string, shortAliases: string[], robotAttributes: Record<any, any>) {
this.token = token; this.token = token;
this.hashId = sha256(sha256(this.token)); this.hashId = sha256(sha256(this.token));
@ -13,7 +13,10 @@ class Slot {
void robohash.generate(this.hashId, 'small'); void robohash.generate(this.hashId, 'small');
void robohash.generate(this.hashId, 'large'); void robohash.generate(this.hashId, 'large');
this.robots = {}; this.robots = shortAliases.reduce((acc: Record<string, Robot>, shortAlias: string) => {
acc[shortAlias] = new Robot(robotAttributes);
return acc;
}, {});
this.order = null; this.order = null;
this.activeShortAlias = null; this.activeShortAlias = null;
@ -47,25 +50,16 @@ class Slot {
return null; return null;
}; };
createRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
if (this.robots[shortAlias] === undefined) {
this.robots[shortAlias] = new Robot(attributes ?? {});
return this.robots[shortAlias];
}
return null;
};
updateRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => { updateRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
this.robots[shortAlias].update(attributes); this.robots[shortAlias].update(attributes);
if (attributes.lastOrderId !== undefined && attributes.lastOrderId != null) { if (attributes.lastOrderId) {
this.lastShortAlias = shortAlias; this.lastShortAlias = shortAlias;
if (this.activeShortAlias === shortAlias) { if (this.activeShortAlias === shortAlias) {
this.activeShortAlias = null; this.activeShortAlias = null;
} }
} }
if (attributes.activeOrderId !== undefined && attributes.activeOrderId != null) { if (attributes.activeOrderId) {
this.activeShortAlias = attributes.shortAlias; this.activeShortAlias = attributes.shortAlias;
} }