From 2a079f340f5f1a61809aebf92ea038a97665684a Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 23 Jul 2023 11:44:29 -0400 Subject: [PATCH] feat: add status api --- src/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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(); +}