*add basic change detection on heartbeat

This commit is contained in:
Derrick Hammer 2022-12-05 14:14:56 -05:00
parent 29478e9a5a
commit b4eaa6c0b6
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 12 additions and 1 deletions

View File

@ -469,16 +469,27 @@ export default class DHTCache extends EventEmitter {
} }
private _heartbeatCheck() { private _heartbeatCheck() {
let changed = false;
for (const peer of this.connectedTo) { for (const peer of this.connectedTo) {
const pubkey = b4a.from(peer, "hex"); 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 conn = this.swarm._allConnections.get(pubkey);
const online = const online =
conn && heartbeat > 0 && Date.now() - heartbeat > 60 * 1000; conn && heartbeat > 0 && Date.now() - heartbeat > 60 * 1000;
if (node?.online !== online) {
changed = true;
}
this._setEntity(peer, { online }); this._setEntity(peer, { online });
} }
if (changed) {
this._recalculate();
}
} }
private _emitHeartbeat(peer?: any) { private _emitHeartbeat(peer?: any) {