feat: add status api

This commit is contained in:
Derrick Hammer 2023-07-23 08:49:33 -04:00
parent cfeb769924
commit 024c810edf
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 36 additions and 0 deletions

View File

@ -25,6 +25,7 @@ let moduleLoaded: Promise<void> = new Promise((resolve) => {
addHandler("presentKey", handlePresentKey); addHandler("presentKey", handlePresentKey);
addHandler("register", handleRegister); addHandler("register", handleRegister);
addHandler("status", handleStatus, { receiveUpdates: true });
addHandler("ready", handleReady); addHandler("ready", handleReady);
addHandler("query", handleQuery); addHandler("query", handleQuery);
@ -199,3 +200,38 @@ async function handleRegister(aq: ActiveQuery) {
aq.respond(); 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();
}