refactor: keep syncState defined and add error

This commit is contained in:
Derrick Hammer 2023-10-09 02:06:08 -04:00
parent 9911ab0168
commit ba09919807
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 21 additions and 14 deletions

View File

@ -14,12 +14,14 @@ export interface Network extends NetworkStatus {
name: string; name: string;
id: string; id: string;
type: string; type: string;
syncState: "done" | "syncing" | "error";
} }
interface NetworkStatus { interface NetworkStatus {
sync: number; sync: number;
peers: number; peers: number;
ready: boolean; ready: boolean;
error?: string;
} }
type LumeObject = { type LumeObject = {
@ -40,17 +42,14 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
// Map to store unsubscribe functions for client.status subscriptions // Map to store unsubscribe functions for client.status subscriptions
const statusUnsubs = useRef(new Map()); const statusUnsubs = useRef(new Map());
const handleStatusUpdate = useCallback( const handleStatusUpdate = useCallback((id: string, newStatus: Network) => {
(id: string, newStatus: NetworkStatus) => { setLume((prevLume) => {
setLume((prevLume) => { const updatedNetworks = prevLume.networks.map((network) =>
const updatedNetworks = prevLume.networks.map((network) => network.id === id ? { ...network, ...newStatus } : network
network.id === id ? { ...network, ...newStatus } : network );
); return { ...prevLume, networks: updatedNetworks };
return { ...prevLume, networks: updatedNetworks }; });
}); }, []);
},
[]
);
const update = async () => { const update = async () => {
const types = await networkRegistry.getTypes(); const types = await networkRegistry.getTypes();
@ -74,9 +73,17 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
}; };
// Subscribe to status updates // Subscribe to status updates
const statusUnsub = client.status((newStatus: NetworkStatus) => const statusUnsub = client.status((newStatus: NetworkStatus) => {
handleStatusUpdate(module, newStatus) let syncState = "syncing";
);
if (newStatus.ready) {
syncState = "done";
} else if (newStatus.error) {
syncState = "error";
}
handleStatusUpdate(module, { ...newStatus, syncState });
});
newStatusUnsubs.set(module, statusUnsub); newStatusUnsubs.set(module, statusUnsub);
newNetworksMap.set(module, network); // Store network in map to prevent duplicates newNetworksMap.set(module, network); // Store network in map to prevent duplicates