From b4eaa6c0b6152085eb2c418ca94011b4f0f80209 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 5 Dec 2022 14:14:56 -0500 Subject: [PATCH] *add basic change detection on heartbeat --- src/index.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index f580ea5..15f801c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -469,16 +469,27 @@ export default class DHTCache extends EventEmitter { } private _heartbeatCheck() { + let changed = false; + for (const peer of this.connectedTo) { const pubkey = b4a.from(peer, "hex"); - const heartbeat = this.graph.node.get(peer)?.heartbeat; + const node = this.graph.node.get(peer); + const heartbeat = node?.heartbeat; const conn = this.swarm._allConnections.get(pubkey); const online = conn && heartbeat > 0 && Date.now() - heartbeat > 60 * 1000; + if (node?.online !== online) { + changed = true; + } + this._setEntity(peer, { online }); } + + if (changed) { + this._recalculate(); + } } private _emitHeartbeat(peer?: any) {