diff --git a/src/index.ts b/src/index.ts index e44283c..ee133d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ let moduleLoaded: Promise = new Promise((resolve) => { addHandler("presentKey", handlePresentKey); addHandler("register", handleRegister); +addHandler("status", handleStatus, { receiveUpdates: true }); addHandler("ready", handleReady); addHandler("query", handleQuery); @@ -199,3 +200,38 @@ async function handleRegister(aq: ActiveQuery) { aq.respond(); } + +async function handleStatus(aq: ActiveQuery) { + let chainProgress = node.chain.getProgress(); + let chainPeers = node.pool.peers.size(); + + const chainProgressListener = node.chain.on("tip", () => { + chainProgress = node.chain.getProgress(); + sendUpdate(); + }); + + function peersListener() { + chainPeers = node.pool.peers.size(); + sendUpdate(); + } + + node.pool.on("peer", peersListener); + node.pool.on("peer close", peersListener); + + function sendUpdate() { + aq.sendUpdate({ + sync: chainProgress * 100, + peers: chainPeers, + ready: node.chain.synced, + }); + } + + aq.setReceiveUpdate?.(() => { + node.chain.off("tip", chainProgressListener); + node.pool.off("peer", peersListener); + node.pool.off("peer close", peersListener); + aq.respond(); + }); + + sendUpdate(); +}