fix: update syncState and how status updates work and their types

This commit is contained in:
Derrick Hammer 2023-10-09 02:35:36 -04:00
parent 349fa936d5
commit 0551582887
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 11 deletions

View File

@ -10,11 +10,13 @@ import type { ReactNode } from "react";
import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client"; import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client";
import { createNetworkClient } from "@lumeweb/libkernel/module"; import { createNetworkClient } from "@lumeweb/libkernel/module";
type SyncState = "done" | "syncing" | "error";
export interface Network extends NetworkStatus { export interface Network extends NetworkStatus {
name: string; name: string;
id: string; id: string;
type: string; type: string;
syncState: "done" | "syncing" | "error"; syncState: SyncState;
} }
interface NetworkStatus { interface NetworkStatus {
@ -42,14 +44,17 @@ 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((id: string, newStatus: Network) => { const handleStatusUpdate = useCallback(
setLume((prevLume) => { (id: string, newNetwork: NetworkStatus & { syncState: SyncState }) => {
const updatedNetworks = prevLume.networks.map((network) => setLume((prevLume) => {
network.id === id ? { ...network, ...newStatus } : network const updatedNetworks = prevLume.networks.map((network) =>
); network.id === id ? { ...network, ...newNetwork } : 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();
@ -75,7 +80,7 @@ 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) => {
let syncState = "syncing"; let syncState: SyncState = "syncing";
if (newStatus.ready) { if (newStatus.ready) {
syncState = "done"; syncState = "done";
@ -83,7 +88,10 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
syncState = "error"; syncState = "error";
} }
handleStatusUpdate(module, { ...newStatus, syncState }); handleStatusUpdate(module, {
...newStatus,
syncState,
});
}); });
newStatusUnsubs.set(module, statusUnsub); newStatusUnsubs.set(module, statusUnsub);