diff --git a/src/index.ts b/src/index.ts index 5fc7aed..4da6cd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -178,25 +178,26 @@ export default class DHTCache extends EventEmitter { protected addPeerHandler(peer: any) { const id = peer.remotePublicKey; const stringId = id.toString("hex"); - if (this.connectedTo.has(stringId)) { - return; - } // Already know we're connected here + if (!this.connectedTo.has(stringId)) { + this.connectedTo.add(stringId); + } - this.connectedTo.add(stringId); - this._ensurePeer(id); - this._addEntityConnection(this.id, id); - this.emit("peer-add", id); + if (!this._hasSeenEntity(id)) { + this._ensurePeer(id); + this._addEntityConnection(this.id, id); + this.emit("peer-add", id); - this._recalculate(); + this._recalculate(); - this._broadcastMessage({ - type: Type.CONNECTED, - id, - }); + this._broadcastMessage({ + type: Type.CONNECTED, + id, + }); - this._emitHeartbeat(peer); + this._emitHeartbeat(peer); - this.log.debug(`Relay peer connected: ${stringId}`); + this.log.debug(`Relay peer connected: ${stringId}`); + } if (this.bootstrapped) { return;