Skip to content

Commit 8e5c0a4

Browse files
pgtedryajov
authored andcommitted
feat: state events and query changes (#100)
1 parent 2c1b685 commit 8e5c0a4

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Circuit = require('libp2p-circuit')
1818

1919
exports = module.exports
2020

21-
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
21+
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
2222

2323
class Node extends EventEmitter {
2424
constructor (_modules, _peerInfo, _peerBook, _options) {
@@ -29,7 +29,7 @@ class Node extends EventEmitter {
2929
this.modules = _modules
3030
this.peerInfo = _peerInfo
3131
this.peerBook = _peerBook || new PeerBook()
32-
this.isOnline = false
32+
this._isStarted = false
3333
this.relayCircuit = null
3434

3535
this.swarm = new Swarm(this.peerInfo, this.peerBook)
@@ -176,7 +176,6 @@ class Node extends EventEmitter {
176176
}
177177
})
178178
this.peerInfo.multiaddrs.replace(maOld, maNew)
179-
180179
const multiaddrs = this.peerInfo.multiaddrs.toArray()
181180

182181
transports.forEach((transport) => {
@@ -194,9 +193,6 @@ class Node extends EventEmitter {
194193
series([
195194
(cb) => this.swarm.listen(cb),
196195
(cb) => {
197-
// listeners on, libp2p is on
198-
this.isOnline = true
199-
200196
if (ws) {
201197
// always add dialing on websockets
202198
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
@@ -209,10 +205,17 @@ class Node extends EventEmitter {
209205
cb()
210206
},
211207
(cb) => {
208+
// TODO: chicken-and-egg problem:
209+
// have to set started here because DHT requires libp2p is already started
210+
this._isStarted = true
212211
if (this._dht) {
213212
return this._dht.start(cb)
214213
}
215214
cb()
215+
},
216+
(cb) => {
217+
this.emit('start')
218+
cb()
216219
}
217220
], callback)
218221
}
@@ -221,7 +224,7 @@ class Node extends EventEmitter {
221224
* Stop the libp2p node by closing its listeners and open connections
222225
*/
223226
stop (callback) {
224-
this.isOnline = false
227+
this._isStarted = false
225228

226229
if (this.modules.discovery) {
227230
this.modules.discovery.forEach((discovery) => {
@@ -236,16 +239,20 @@ class Node extends EventEmitter {
236239
}
237240
cb()
238241
},
239-
(cb) => this.swarm.close(cb)
242+
(cb) => this.swarm.close(cb),
243+
(cb) => {
244+
this.emit('stop')
245+
cb()
246+
}
240247
], callback)
241248
}
242249

243-
isOn () {
244-
return this.isOnline
250+
isStarted () {
251+
return this._isStarted
245252
}
246253

247254
ping (peer, callback) {
248-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
255+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
249256
this._getPeerInfo(peer, (err, peerInfo) => {
250257
if (err) {
251258
return callback(err)
@@ -256,7 +263,7 @@ class Node extends EventEmitter {
256263
}
257264

258265
dial (peer, protocol, callback) {
259-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
266+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
260267

261268
if (typeof protocol === 'function') {
262269
callback = protocol
@@ -279,7 +286,7 @@ class Node extends EventEmitter {
279286
}
280287

281288
hangUp (peer, callback) {
282-
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
289+
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
283290

284291
this._getPeerInfo(peer, (err, peerInfo) => {
285292
if (err) {

0 commit comments

Comments
 (0)