Skip to content

Commit 45284db

Browse files
authored
feat: add getActiveSubscriptions method (#1249)
1 parent 0f6a594 commit 45284db

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

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

+8-22
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ 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";
109
import type {
10+
ActiveSubscriptions,
1111
Callback,
12+
IDecodedMessage,
1213
IDecoder,
1314
IEncoder,
1415
IMessage,
1516
IRelay,
1617
ProtocolCreateOptions,
1718
SendResult,
1819
} from "@waku/interfaces";
19-
import { IDecodedMessage } from "@waku/interfaces";
2020
import debug from "debug";
2121

2222
import { DefaultPubSubTopic } from "../constants.js";
@@ -36,10 +36,6 @@ export type Observer<T extends IDecodedMessage> = {
3636
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts;
3737
export type ContentTopic = string;
3838

39-
type BasicEventPayload = {
40-
contentTopic: string;
41-
};
42-
4339
/**
4440
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
4541
* Must be passed as a `pubsub` module to a `Libp2p` instance.
@@ -120,30 +116,20 @@ class Relay extends GossipSub implements IRelay {
120116

121117
pushOrInitMapSet(this.observers, contentTopic, observer);
122118

123-
this.dispatchEvent(
124-
new CustomEvent<BasicEventPayload>("observer:added", {
125-
detail: {
126-
contentTopic,
127-
},
128-
})
129-
);
130-
131119
return () => {
132120
const observers = this.observers.get(contentTopic);
133121
if (observers) {
134122
observers.delete(observer);
135-
136-
this.dispatchEvent(
137-
new CustomEvent<BasicEventPayload>("observer:removed", {
138-
detail: {
139-
contentTopic,
140-
},
141-
})
142-
);
143123
}
144124
};
145125
}
146126

127+
public getActiveSubscriptions(): ActiveSubscriptions {
128+
const map = new Map();
129+
map.set(this.pubSubTopic, this.observers.keys());
130+
return map;
131+
}
132+
147133
private async processIncomingMessage<T extends IDecodedMessage>(
148134
pubSubTopic: string,
149135
bytes: Uint8Array

packages/interfaces/src/relay.ts

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

43
import type { IDecodedMessage, IDecoder } from "./message.js";
54
import type { Callback } from "./protocols.js";
65
import type { ISender } from "./sender.js";
76

8-
export interface RelayEvents {
9-
"observer:added": CustomEvent;
10-
"observer:removed": CustomEvent;
11-
}
7+
type PubSubTopic = string;
8+
type ContentTopic = string;
129

13-
type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;
10+
export type ActiveSubscriptions = Map<PubSubTopic, ContentTopic[]>;
1411

1512
interface IRelayAPI {
1613
addObserver: <T extends IDecodedMessage>(
1714
decoder: IDecoder<T>,
1815
callback: Callback<T>
1916
) => () => void;
2017
getMeshPeers: () => string[];
18+
getActiveSubscriptions: () => ActiveSubscriptions | undefined;
2119
}
2220

23-
export type IRelay = ISender & GossipSub & IRelayAPI & IRelayEmitter;
21+
export type IRelay = IRelayAPI & GossipSub & ISender;

0 commit comments

Comments
 (0)