Skip to content

Commit 275b166

Browse files
authored
feat!: add custom events to Relay and make observers private (#1213)
1 parent a20b797 commit 275b166

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

packages/core/src/lib/relay/index.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@chainsafe/libp2p-gossipsub";
77
import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types";
88
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
9+
import { CustomEvent } from "@libp2p/interfaces/events";
910
import type {
1011
Callback,
1112
IDecoder,
@@ -35,6 +36,10 @@ export type Observer<T extends IDecodedMessage> = {
3536
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts;
3637
export type ContentTopic = string;
3738

39+
type BasicEventPayload = {
40+
contentTopic: string;
41+
};
42+
3843
/**
3944
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
4045
* Must be passed as a `pubsub` module to a `Libp2p` instance.
@@ -50,7 +55,7 @@ class Relay extends GossipSub implements IRelay {
5055
* observers called when receiving new message.
5156
* Observers under key `""` are always called.
5257
*/
53-
public observers: Map<ContentTopic, Set<unknown>>;
58+
private observers: Map<ContentTopic, Set<unknown>>;
5459

5560
constructor(
5661
components: GossipSubComponents,
@@ -111,12 +116,30 @@ class Relay extends GossipSub implements IRelay {
111116
decoder,
112117
callback,
113118
};
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+
);
115130

116131
return () => {
117-
const observers = this.observers.get(decoder.contentTopic);
132+
const observers = this.observers.get(contentTopic);
118133
if (observers) {
119134
observers.delete(observer);
135+
136+
this.dispatchEvent(
137+
new CustomEvent<BasicEventPayload>("observer:removed", {
138+
detail: {
139+
contentTopic,
140+
},
141+
})
142+
);
120143
}
121144
};
122145
}

packages/interfaces/src/relay.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
1+
import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub";
2+
import type { EventEmitter } from "@libp2p/interfaces/events";
23

34
import type {
45
IDecodedMessage,
@@ -8,11 +9,20 @@ import type {
89
} from "./message.js";
910
import type { Callback, SendResult } from "./protocols.js";
1011

11-
export interface IRelay extends GossipSub {
12+
export interface RelayEvents {
13+
"observer:added": CustomEvent;
14+
"observer:removed": CustomEvent;
15+
}
16+
17+
type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;
18+
19+
interface IRelayAPI extends GossipSub {
1220
send: (encoder: IEncoder, message: IMessage) => Promise<SendResult>;
1321
addObserver: <T extends IDecodedMessage>(
1422
decoder: IDecoder<T>,
1523
callback: Callback<T>
1624
) => () => void;
1725
getMeshPeers: () => string[];
1826
}
27+
28+
export type IRelay = IRelayAPI & IRelayEmitter;

0 commit comments

Comments
 (0)