@@ -17,7 +17,7 @@ const multiaddr = require('multiaddr')
17
17
18
18
exports = module . exports
19
19
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'
21
21
22
22
class Node extends EventEmitter {
23
23
constructor ( _modules , _peerInfo , _peerBook , _options ) {
@@ -28,7 +28,7 @@ class Node extends EventEmitter {
28
28
this . modules = _modules
29
29
this . peerInfo = _peerInfo
30
30
this . peerBook = _peerBook || new PeerBook ( )
31
- this . isOnline = false
31
+ this . _isStarted = false
32
32
33
33
this . swarm = new Swarm ( this . peerInfo , this . peerBook )
34
34
@@ -156,7 +156,6 @@ class Node extends EventEmitter {
156
156
}
157
157
} )
158
158
this . peerInfo . multiaddrs . replace ( maOld , maNew )
159
-
160
159
const multiaddrs = this . peerInfo . multiaddrs . toArray ( )
161
160
162
161
transports . forEach ( ( transport ) => {
@@ -174,9 +173,6 @@ class Node extends EventEmitter {
174
173
series ( [
175
174
( cb ) => this . swarm . listen ( cb ) ,
176
175
( cb ) => {
177
- // listeners on, libp2p is on
178
- this . isOnline = true
179
-
180
176
if ( ws ) {
181
177
// always add dialing on websockets
182
178
this . swarm . transport . add ( ws . tag || ws . constructor . name , ws )
@@ -189,10 +185,17 @@ class Node extends EventEmitter {
189
185
cb ( )
190
186
} ,
191
187
( cb ) => {
188
+ // TODO: chicken-and-egg problem:
189
+ // have to set started here because DHT requires libp2p is already started
190
+ this . _isStarted = true
192
191
if ( this . _dht ) {
193
192
return this . _dht . start ( cb )
194
193
}
195
194
cb ( )
195
+ } ,
196
+ ( cb ) => {
197
+ this . emit ( 'start' )
198
+ cb ( )
196
199
}
197
200
] , callback )
198
201
}
@@ -201,7 +204,7 @@ class Node extends EventEmitter {
201
204
* Stop the libp2p node by closing its listeners and open connections
202
205
*/
203
206
stop ( callback ) {
204
- this . isOnline = false
207
+ this . _isStarted = false
205
208
206
209
if ( this . modules . discovery ) {
207
210
this . modules . discovery . forEach ( ( discovery ) => {
@@ -216,16 +219,20 @@ class Node extends EventEmitter {
216
219
}
217
220
cb ( )
218
221
} ,
219
- ( cb ) => this . swarm . close ( cb )
222
+ ( cb ) => this . swarm . close ( cb ) ,
223
+ ( cb ) => {
224
+ this . emit ( 'stop' )
225
+ cb ( )
226
+ }
220
227
] , callback )
221
228
}
222
229
223
- isOn ( ) {
224
- return this . isOnline
230
+ isStarted ( ) {
231
+ return this . _isStarted
225
232
}
226
233
227
234
ping ( peer , callback ) {
228
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
235
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
229
236
this . _getPeerInfo ( peer , ( err , peerInfo ) => {
230
237
if ( err ) {
231
238
return callback ( err )
@@ -236,7 +243,7 @@ class Node extends EventEmitter {
236
243
}
237
244
238
245
dial ( peer , protocol , callback ) {
239
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
246
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
240
247
241
248
if ( typeof protocol === 'function' ) {
242
249
callback = protocol
@@ -259,7 +266,7 @@ class Node extends EventEmitter {
259
266
}
260
267
261
268
hangUp ( peer , callback ) {
262
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
269
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
263
270
264
271
this . _getPeerInfo ( peer , ( err , peerInfo ) => {
265
272
if ( err ) {
0 commit comments