feat: add status api
This commit is contained in:
parent
cfeb769924
commit
024c810edf
36
src/index.ts
36
src/index.ts
|
@ -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();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue