Skip to content

Commit 73f2f6d

Browse files
pgtedaviddias
authored andcommitted
feat: state events and query changes (#100)
1 parent 2d40f3a commit 73f2f6d

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/index.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const multiaddr = require('multiaddr')
1717

1818
exports = module.exports
1919

20-
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
20+
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
2121

2222
class Node extends EventEmitter {
2323
constructor (_modules, _peerInfo, _peerBook, _options) {
@@ -28,7 +28,7 @@ class Node extends EventEmitter {
2828
this.modules = _modules
2929
this.peerInfo = _peerInfo
3030
this.peerBook = _peerBook || new PeerBook()
31-
this.isOnline = false
31+
this._isStarted = false
3232

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

@@ -156,7 +156,6 @@ class Node extends EventEmitter {
156156
}
157157
})
158158
this.peerInfo.multiaddrs.replace(maOld, maNew)
159-
160159
const multiaddrs = this.peerInfo.multiaddrs.toArray()
161160

162161
transports.forEach((transport) => {
@@ -174,9 +173,6 @@ class Node extends EventEmitter {
174173
series([
175174
(cb) => this.swarm.listen(cb),
176175
(cb) => {
177-
// listeners on, libp2p is on
178-
this.isOnline = true
179-
180176
if (ws) {
181177
// always add dialing on websockets
182178
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
@@ -189,10 +185,17 @@ class Node extends EventEmitter {
189185
cb()
190186
},
191187
(cb) => {
188+
// TODO: chicken-and-egg problem:
189+
// have to set started here because DHT requires libp2p is already started
190+
this._isStarted = true
192191
if (this._dht) {
193192
return this._dht.start(cb)
194193
}
195194
cb()
195+
},
196+
(cb) => {
197+
this.emit('start')
198+
cb()
196199
}
197200
], callback)
198201
}
@@ -201,7 +204,7 @@ class Node extends EventEmitter {
201204
* Stop the libp2p node by closing its listeners and open connections
202205
*/
203206
stop (callback) {
204-
this.isOnline = false
207+
this._isStarted = false
205208

206209
if (this.modules.discovery) {
207210
this.modules.discovery.forEach((discovery) => {
@@ -216,16 +219,20 @@ class Node extends EventEmitter {
216219
}
217220
cb()
218221
},
219-
(cb) => this.swarm.close(cb)
222+
(cb) => this.swarm.close(cb),
223+
(cb) => {
224+
this.emit('stop')
225+
cb()
226+
}
220227
], callback)
221228
}
222229

223-
isOn () {
224-
return this.isOnline
230+
isStarted () {
231+
return this._isStarted
225232
}
226233

227234
ping (peer, callback) {
228-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
235+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
229236
this._getPeerInfo(peer, (err, peerInfo) => {
230237
if (err) {
231238
return callback(err)
@@ -236,7 +243,7 @@ class Node extends EventEmitter {
236243
}
237244

238245
dial (peer, protocol, callback) {
239-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
246+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
240247

241248
if (typeof protocol === 'function') {
242249
callback = protocol
@@ -259,7 +266,7 @@ class Node extends EventEmitter {
259266
}
260267

261268
hangUp (peer, callback) {
262-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
269+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
263270

264271
this._getPeerInfo(peer, (err, peerInfo) => {
265272
if (err) {

0 commit comments

Comments
 (0)