@@ -11,12 +11,12 @@ import {
11
11
signMessage ,
12
12
verifySignature
13
13
} from './sign.js'
14
- import type { PeerId } from '@libp2p/interfaces/ peer-id'
15
- import type { IncomingStreamData } from '@libp2p/interfaces/ registrar'
16
- import type { Connection } from '@libp2p/interfaces/ connection'
17
- import type { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult } from '@libp2p/interfaces/ pubsub'
14
+ import type { PeerId } from '@libp2p/interface- peer-id'
15
+ import type { IncomingStreamData } from '@libp2p/interface- registrar'
16
+ import type { Connection } from '@libp2p/interface- connection'
17
+ import type { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult } from '@libp2p/interface- pubsub'
18
18
import { PeerMap , PeerSet } from '@libp2p/peer-collections'
19
- import { Components , Initializable } from '@libp2p/interfaces/ components'
19
+ import { Components , Initializable } from '@libp2p/components'
20
20
21
21
const log = logger ( 'libp2p:pubsub' )
22
22
@@ -63,7 +63,7 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
63
63
public multicodecs : string [ ]
64
64
public components : Components = new Components ( )
65
65
66
- private _registrarTopologyId : string | undefined
66
+ private _registrarTopologyIds : string [ ] | undefined
67
67
protected enabled : boolean
68
68
69
69
constructor ( props : PubSubInit ) {
@@ -112,17 +112,18 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
112
112
113
113
log ( 'starting' )
114
114
115
+ const registrar = this . components . getRegistrar ( )
115
116
// Incoming streams
116
117
// Called after a peer dials us
117
- await this . components . getRegistrar ( ) . handle ( this . multicodecs , this . _onIncomingStream )
118
+ await Promise . all ( this . multicodecs . map ( async multicodec => await registrar . handle ( multicodec , this . _onIncomingStream ) ) )
118
119
119
120
// register protocol with topology
120
121
// Topology callbacks called on connection manager changes
121
122
const topology = createTopology ( {
122
123
onConnect : this . _onPeerConnected ,
123
124
onDisconnect : this . _onPeerDisconnected
124
125
} )
125
- this . _registrarTopologyId = await this . components . getRegistrar ( ) . register ( this . multicodecs , topology )
126
+ this . _registrarTopologyIds = await Promise . all ( this . multicodecs . map ( async multicodec => await registrar . register ( multicodec , topology ) ) )
126
127
127
128
log ( 'started' )
128
129
this . started = true
@@ -136,12 +137,14 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
136
137
return
137
138
}
138
139
140
+ const registrar = this . components . getRegistrar ( )
141
+
139
142
// unregister protocol and handlers
140
- if ( this . _registrarTopologyId != null ) {
141
- this . components . getRegistrar ( ) . unregister ( this . _registrarTopologyId )
143
+ if ( this . _registrarTopologyIds != null ) {
144
+ this . _registrarTopologyIds ?. map ( id => registrar . unregister ( id ) )
142
145
}
143
146
144
- await this . components . getRegistrar ( ) . unhandle ( this . multicodecs )
147
+ await Promise . all ( this . multicodecs . map ( async multicodec => await registrar . unhandle ( multicodec ) ) )
145
148
146
149
log ( 'stopping' )
147
150
for ( const peerStreams of this . peers . values ( ) ) {
@@ -553,7 +556,7 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
553
556
/**
554
557
* Get a list of the peer-ids that are subscribed to one topic.
555
558
*/
556
- getSubscribers ( topic : string ) {
559
+ getSubscribers ( topic : string ) : PeerId [ ] {
557
560
if ( ! this . started ) {
558
561
throw errcode ( new Error ( 'not started yet' ) , 'ERR_NOT_STARTED_YET' )
559
562
}
@@ -676,7 +679,7 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
676
679
return Array . from ( this . subscriptions )
677
680
}
678
681
679
- getPeers ( ) {
682
+ getPeers ( ) : PeerId [ ] {
680
683
if ( ! this . started ) {
681
684
throw new Error ( 'Pubsub is not started' )
682
685
}
0 commit comments