Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 2ead6aa

Browse files
committed
feat: emit event on peer connected
1 parent 59d1a94 commit 2ead6aa

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { EventEmitter } = require('events')
34
const libp2pRecord = require('libp2p-record')
45
const MemoryStore = require('interface-datastore').MemoryDatastore
56
const waterfall = require('async/waterfall')
@@ -27,7 +28,7 @@ const assert = require('assert')
2728
*
2829
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
2930
*/
30-
class KadDHT {
31+
class KadDHT extends EventEmitter {
3132
/**
3233
* Create a new KadDHT.
3334
*
@@ -40,6 +41,7 @@ class KadDHT {
4041
* @param {object} options.selectors selectors object with namespace as keys and function(key, records)
4142
*/
4243
constructor (sw, options) {
44+
super()
4345
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
4446
options = options || {}
4547
options.validators = options.validators
@@ -608,6 +610,10 @@ class KadDHT {
608610
], callback)
609611
})
610612
}
613+
614+
_peerConnected (peerInfo) {
615+
this.emit('peer', peerInfo)
616+
}
611617
}
612618

613619
module.exports = KadDHT

src/network.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Network {
122122
return this._log.error('Failed to add to the routing table', err)
123123
}
124124

125+
this.dht._peerConnected(peer)
126+
125127
this._log('added to the routing table: %s', peer.id.toB58String())
126128
})
127129
})

src/query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function execQuery (next, query, path, callback) {
205205
if (query.dht._isSelf(closer.id)) {
206206
return cb()
207207
}
208+
query.dht._peerConnected(closer)
208209
closer = query.dht.peerBook.put(closer)
209210
addPeerToQuery(closer.id, query.dht, path, cb)
210211
}, callback)

0 commit comments

Comments
 (0)