From 16dcaafd781ee3957a489c73b8027b2eabca648f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 23 Jul 2023 09:53:21 -0400 Subject: [PATCH] feat: add status api --- src/index.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/index.ts b/src/index.ts index 2cbd274..00d2b83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,7 @@ onmessage = handleMessage; const moduleDefer = defer(); let activeIpfsPeersDefer = defer(); let networkPeersAvailable = defer(); +let networkReady = true; const networkRegistry = createNetworkRegistryClient(); let swarm; @@ -58,6 +59,7 @@ BigInt.prototype.toJSON = function () { addHandler("presentKey", handlePresentKey); addHandler("register", handleRegister); +addHandler("status", handleStatus, { receiveUpdates: true }); addHandler("ready", handleReady); addHandler("stat", handleStat); addHandler("ls", handleLs, { receiveUpdates: true }); @@ -94,6 +96,7 @@ async function handlePresentKey() { if (!ipfs.libp2p.isStarted()) { await ipfs.start(); networkPeersAvailable.resolve(); + networkReady = true; } }); @@ -295,3 +298,56 @@ async function handleRegister(aq: ActiveQuery) { aq.respond(); } + +async function handleStatus(aq: ActiveQuery) { + function sendUpdate() { + aq.sendUpdate({ + peers: netPeers, + ready: netPeers > 0, + }); + } + + let netPeers = 0; + if (!networkReady) { + sendUpdate(); + await ready(); + getPeers(); + } + + function getPeers() { + netPeers = ipfs.libp2p.getPeers().length; + } + + function peersListener() { + getPeers(); + sendUpdate(); + } + + const peerEvents = ["connection:prune", "peer:connect", "peer:disconnect"]; + peerEvents.forEach((ev) => { + // @ts-ignore + ipfs.libp2p.components.connectionManager.events.addEventListener( + ev, + peersListener, + ); + }); + + // @ts-ignore + ipfs.libp2p.components.connectionManager.events.addEventListener( + "peer:disconnect", + peersListener, + ); + + aq.setReceiveUpdate?.(() => { + peerEvents.forEach((ev) => { + // @ts-ignore + ipfs.libp2p.components.connectionManager.events.removeEventListener( + ev, + peersListener, + ); + }); + aq.respond(); + }); + + sendUpdate(); +}