diff --git a/src/index.ts b/src/index.ts index 5386a1e..479eb02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,7 @@ let rpc: RpcNetwork; addHandler("presentKey", handlePresentKey); addHandler("register", handleRegister); +addHandler("status", handleStatus, { receiveUpdates: true }); addHandler("ready", handleReady); [ @@ -178,3 +179,30 @@ async function handleRegister(aq: ActiveQuery) { aq.respond(); } + +async function handleStatus(aq: ActiveQuery) { + await clientInitDefer.promise; + let chainProgress = 0; + + const chainProgressListener = (currentUpdate, totalUpdates) => { + chainProgress = Math.round((currentUpdate / totalUpdates) * 100) / 100; + sendUpdate(); + }; + + client.on("update", chainProgressListener); + + function sendUpdate() { + aq.sendUpdate({ + sync: Math.floor(chainProgress * 100), + peers: 1, + ready: client.isSynced, + }); + } + + aq.setReceiveUpdate?.(() => { + client.off("update", chainProgressListener); + aq.respond(); + }); + + sendUpdate(); +}