Skip to content

Commit

Permalink
Add de-duped peer discovery event
Browse files Browse the repository at this point in the history
  • Loading branch information
JustMaier committed May 31, 2019
1 parent c94041c commit a2165c3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const bs58 = require('bs58')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const EventEmitter = require('events').EventEmitter

function getB58Str (peer) {
let b58Str
Expand All @@ -22,8 +23,12 @@ function getB58Str (peer) {
return b58Str
}

class PeerBook {
/**
* @fires PeerBook#peer:discovery Emitted when a new peer is discovered
*/
class PeerBook extends EventEmitter {
constructor () {
super()
this._peers = {}
}

Expand All @@ -47,6 +52,7 @@ class PeerBook {
// insert if doesn't exist or replace if replace flag is true
if (!localPeerInfo || replace) {
this._peers[peerInfo.id.toB58String()] = peerInfo
if(!replace) this.emit('peer:discovery', peerInfo)
return peerInfo
}

Expand Down

0 comments on commit a2165c3

Please sign in to comment.