*add basic change detection on heartbeat
This commit is contained in:
parent
29478e9a5a
commit
b4eaa6c0b6
13
src/index.ts
13
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) {
|
||||
|
|
Loading…
Reference in New Issue