@@ -23,7 +23,7 @@ import { DefaultUpgrader } from './upgrader.js'
2323import * as pkg from './version.js'
2424import type { Components } from './components.js'
2525import type { Libp2p , Libp2pInit , Libp2pOptions } from './index.js'
26- import type { PeerRouting , ContentRouting , Libp2pEvents , PendingDial , ServiceMap , AbortOptions , ComponentLogger , Logger , Connection , NewStreamOptions , Stream , Metrics , PeerId , PeerInfo , PeerStore , Topology } from '@libp2p/interface'
26+ import type { PeerRouting , ContentRouting , Libp2pEvents , PendingDial , ServiceMap , AbortOptions , ComponentLogger , Logger , Connection , NewStreamOptions , Stream , Metrics , PeerId , PeerInfo , PeerStore , Topology , Libp2pStatus } from '@libp2p/interface'
2727import type { StreamHandler , StreamHandlerOptions } from '@libp2p/interface-internal'
2828
2929export class Libp2pNode < T extends ServiceMap = Record < string , unknown > > extends TypedEventEmitter < Libp2pEvents > implements Libp2p < T > {
@@ -34,14 +34,16 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
3434 public metrics ?: Metrics
3535 public services : T
3636 public logger : ComponentLogger
37+ public status : Libp2pStatus
3738
3839 public components : Components
39- #started: boolean
4040 private readonly log : Logger
4141
4242 constructor ( init : Libp2pInit < T > ) {
4343 super ( )
4444
45+ this . status = 'stopped'
46+
4547 // event bus - components can listen to this emitter to be notified of system events
4648 // and also cause them to be emitted
4749 const events = new TypedEventEmitter < Libp2pEvents > ( )
@@ -58,7 +60,6 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
5860 // This emitter gets listened to a lot
5961 setMaxListeners ( Infinity , events )
6062
61- this . #started = false
6263 this . peerId = init . peerId
6364 this . logger = init . logger ?? defaultLogger ( )
6465 this . log = this . logger . forComponent ( 'libp2p' )
@@ -196,11 +197,11 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
196197 * Starts the libp2p node and all its subsystems
197198 */
198199 async start ( ) : Promise < void > {
199- if ( this . #started ) {
200+ if ( this . status !== 'stopped' ) {
200201 return
201202 }
202203
203- this . #started = true
204+ this . status = 'starting'
204205
205206 this . log ( 'libp2p is starting' )
206207
@@ -209,6 +210,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
209210 await this . components . start ( )
210211 await this . components . afterStart ?.( )
211212
213+ this . status = 'started'
212214 this . safeDispatchEvent ( 'start' , { detail : this } )
213215 this . log ( 'libp2p has started' )
214216 } catch ( err : any ) {
@@ -222,26 +224,23 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
222224 * Stop the libp2p node by closing its listeners and open connections
223225 */
224226 async stop ( ) : Promise < void > {
225- if ( ! this . # started) {
227+ if ( this . status !== ' started' ) {
226228 return
227229 }
228230
229231 this . log ( 'libp2p is stopping' )
230232
231- this . #started = false
233+ this . status = 'stopping'
232234
233235 await this . components . beforeStop ?.( )
234236 await this . components . stop ( )
235237 await this . components . afterStop ?.( )
236238
239+ this . status = 'stopped'
237240 this . safeDispatchEvent ( 'stop' , { detail : this } )
238241 this . log ( 'libp2p has stopped' )
239242 }
240243
241- isStarted ( ) : boolean {
242- return this . #started
243- }
244-
245244 getConnections ( peerId ?: PeerId ) : Connection [ ] {
246245 return this . components . connectionManager . getConnections ( peerId )
247246 }
0 commit comments