6
6
} from "@chainsafe/libp2p-gossipsub" ;
7
7
import type { PeerIdStr , TopicStr } from "@chainsafe/libp2p-gossipsub/types" ;
8
8
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types" ;
9
+ import { CustomEvent } from "@libp2p/interfaces/events" ;
9
10
import type {
10
11
Callback ,
11
12
IDecoder ,
@@ -35,6 +36,10 @@ export type Observer<T extends IDecodedMessage> = {
35
36
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts ;
36
37
export type ContentTopic = string ;
37
38
39
+ type BasicEventPayload = {
40
+ contentTopic : string ;
41
+ } ;
42
+
38
43
/**
39
44
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
40
45
* Must be passed as a `pubsub` module to a `Libp2p` instance.
@@ -50,7 +55,7 @@ class Relay extends GossipSub implements IRelay {
50
55
* observers called when receiving new message.
51
56
* Observers under key `""` are always called.
52
57
*/
53
- public observers : Map < ContentTopic , Set < unknown > > ;
58
+ private observers : Map < ContentTopic , Set < unknown > > ;
54
59
55
60
constructor (
56
61
components : GossipSubComponents ,
@@ -111,12 +116,30 @@ class Relay extends GossipSub implements IRelay {
111
116
decoder,
112
117
callback,
113
118
} ;
114
- pushOrInitMapSet ( this . observers , decoder . contentTopic , observer ) ;
119
+ const contentTopic = decoder . contentTopic ;
120
+
121
+ pushOrInitMapSet ( this . observers , contentTopic , observer ) ;
122
+
123
+ this . dispatchEvent (
124
+ new CustomEvent < BasicEventPayload > ( "observer:added" , {
125
+ detail : {
126
+ contentTopic,
127
+ } ,
128
+ } )
129
+ ) ;
115
130
116
131
return ( ) => {
117
- const observers = this . observers . get ( decoder . contentTopic ) ;
132
+ const observers = this . observers . get ( contentTopic ) ;
118
133
if ( observers ) {
119
134
observers . delete ( observer ) ;
135
+
136
+ this . dispatchEvent (
137
+ new CustomEvent < BasicEventPayload > ( "observer:removed" , {
138
+ detail : {
139
+ contentTopic,
140
+ } ,
141
+ } )
142
+ ) ;
120
143
}
121
144
} ;
122
145
}
0 commit comments