feat: add status api

This commit is contained in:
Derrick Hammer 2023-07-23 11:44:29 -04:00
parent 4a7feb9a98
commit 2a079f340f
1 changed files with 28 additions and 0 deletions

View File

@ -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();
}