From 45284db963d6d4c90a014391551604c236906b88 Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 21 Mar 2023 02:44:35 +0100 Subject: [PATCH] feat: add getActiveSubscriptions method (#1249) --- packages/core/src/lib/relay/index.ts | 30 ++++++++-------------------- packages/interfaces/src/relay.ts | 14 ++++++------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/packages/core/src/lib/relay/index.ts b/packages/core/src/lib/relay/index.ts index 296282133b..40365e4f93 100644 --- a/packages/core/src/lib/relay/index.ts +++ b/packages/core/src/lib/relay/index.ts @@ -6,9 +6,10 @@ import { } from "@chainsafe/libp2p-gossipsub"; import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types"; import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types"; -import { CustomEvent } from "@libp2p/interfaces/events"; import type { + ActiveSubscriptions, Callback, + IDecodedMessage, IDecoder, IEncoder, IMessage, @@ -16,7 +17,6 @@ import type { ProtocolCreateOptions, SendResult, } from "@waku/interfaces"; -import { IDecodedMessage } from "@waku/interfaces"; import debug from "debug"; import { DefaultPubSubTopic } from "../constants.js"; @@ -36,10 +36,6 @@ export type Observer = { export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts; export type ContentTopic = string; -type BasicEventPayload = { - contentTopic: string; -}; - /** * Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/). * Must be passed as a `pubsub` module to a `Libp2p` instance. @@ -120,30 +116,20 @@ class Relay extends GossipSub implements IRelay { pushOrInitMapSet(this.observers, contentTopic, observer); - this.dispatchEvent( - new CustomEvent("observer:added", { - detail: { - contentTopic, - }, - }) - ); - return () => { const observers = this.observers.get(contentTopic); if (observers) { observers.delete(observer); - - this.dispatchEvent( - new CustomEvent("observer:removed", { - detail: { - contentTopic, - }, - }) - ); } }; } + public getActiveSubscriptions(): ActiveSubscriptions { + const map = new Map(); + map.set(this.pubSubTopic, this.observers.keys()); + return map; + } + private async processIncomingMessage( pubSubTopic: string, bytes: Uint8Array diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index 70b8225deb..c602322cd1 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -1,16 +1,13 @@ -import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub"; -import type { EventEmitter } from "@libp2p/interfaces/events"; +import type { GossipSub } from "@chainsafe/libp2p-gossipsub"; import type { IDecodedMessage, IDecoder } from "./message.js"; import type { Callback } from "./protocols.js"; import type { ISender } from "./sender.js"; -export interface RelayEvents { - "observer:added": CustomEvent; - "observer:removed": CustomEvent; -} +type PubSubTopic = string; +type ContentTopic = string; -type IRelayEmitter = EventEmitter; +export type ActiveSubscriptions = Map; interface IRelayAPI { addObserver: ( @@ -18,6 +15,7 @@ interface IRelayAPI { callback: Callback ) => () => void; getMeshPeers: () => string[]; + getActiveSubscriptions: () => ActiveSubscriptions | undefined; } -export type IRelay = ISender & GossipSub & IRelayAPI & IRelayEmitter; +export type IRelay = IRelayAPI & GossipSub & ISender;