Skip to content

Commit

Permalink
feat: state events and query changes (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte authored and daviddias committed Jul 7, 2017
1 parent 2d40f3a commit 73f2f6d
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const multiaddr = require('multiaddr')

exports = module.exports

const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'

class Node extends EventEmitter {
constructor (_modules, _peerInfo, _peerBook, _options) {
Expand All @@ -28,7 +28,7 @@ class Node extends EventEmitter {
this.modules = _modules
this.peerInfo = _peerInfo
this.peerBook = _peerBook || new PeerBook()
this.isOnline = false
this._isStarted = false

this.swarm = new Swarm(this.peerInfo, this.peerBook)

Expand Down Expand Up @@ -156,7 +156,6 @@ class Node extends EventEmitter {
}
})
this.peerInfo.multiaddrs.replace(maOld, maNew)

const multiaddrs = this.peerInfo.multiaddrs.toArray()

transports.forEach((transport) => {
Expand All @@ -174,9 +173,6 @@ class Node extends EventEmitter {
series([
(cb) => this.swarm.listen(cb),
(cb) => {
// listeners on, libp2p is on
this.isOnline = true

if (ws) {
// always add dialing on websockets
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
Expand All @@ -189,10 +185,17 @@ class Node extends EventEmitter {
cb()
},
(cb) => {
// TODO: chicken-and-egg problem:
// have to set started here because DHT requires libp2p is already started
this._isStarted = true
if (this._dht) {
return this._dht.start(cb)
}
cb()
},
(cb) => {
this.emit('start')
cb()
}
], callback)
}
Expand All @@ -201,7 +204,7 @@ class Node extends EventEmitter {
* Stop the libp2p node by closing its listeners and open connections
*/
stop (callback) {
this.isOnline = false
this._isStarted = false

if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
Expand All @@ -216,16 +219,20 @@ class Node extends EventEmitter {
}
cb()
},
(cb) => this.swarm.close(cb)
(cb) => this.swarm.close(cb),
(cb) => {
this.emit('stop')
cb()
}
], callback)
}

isOn () {
return this.isOnline
isStarted () {
return this._isStarted
}

ping (peer, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) {
return callback(err)
Expand All @@ -236,7 +243,7 @@ class Node extends EventEmitter {
}

dial (peer, protocol, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)

if (typeof protocol === 'function') {
callback = protocol
Expand All @@ -259,7 +266,7 @@ class Node extends EventEmitter {
}

hangUp (peer, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)

this._getPeerInfo(peer, (err, peerInfo) => {
if (err) {
Expand Down

0 comments on commit 73f2f6d

Please sign in to comment.