Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getObservers method #1249

Merged
merged 8 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions packages/core/src/lib/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ 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,
IRelay,
ProtocolCreateOptions,
SendResult,
} from "@waku/interfaces";
import { IDecodedMessage } from "@waku/interfaces";
import debug from "debug";

import { DefaultPubSubTopic } from "../constants.js";
Expand All @@ -36,10 +36,6 @@ export type Observer<T extends IDecodedMessage> = {
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.
Expand Down Expand Up @@ -120,30 +116,20 @@ class Relay extends GossipSub implements IRelay {

pushOrInitMapSet(this.observers, contentTopic, observer);

this.dispatchEvent(
new CustomEvent<BasicEventPayload>("observer:added", {
detail: {
contentTopic,
},
})
);

return () => {
const observers = this.observers.get(contentTopic);
if (observers) {
observers.delete(observer);

this.dispatchEvent(
new CustomEvent<BasicEventPayload>("observer:removed", {
detail: {
contentTopic,
},
})
);
}
};
}

public getActiveSubscriptions(): ActiveSubscriptions | undefined {
const map = new Map();
map.set(this.pubSubTopic, this.observers.keys());
return map;
}

private async processIncomingMessage<T extends IDecodedMessage>(
pubSubTopic: string,
bytes: Uint8Array
Expand Down
16 changes: 6 additions & 10 deletions packages/interfaces/src/relay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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,
Expand All @@ -9,20 +8,17 @@ import type {
} from "./message.js";
import type { Callback, SendResult } from "./protocols.js";

export interface RelayEvents {
"observer:added": CustomEvent;
"observer:removed": CustomEvent;
}
type PubSubTopic = string;
type ContentTopic = string;
Comment on lines +7 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future work: Would be good to actually use these two types across the codebase.

Copy link
Collaborator Author

@weboko weboko Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point from the perspective of improving readability but I would try to refrain from pushing it's usage on the consumers since these types are primitive, at least for now.

Here is the issue #1254


type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;
export type ActiveSubscriptions = Map<PubSubTopic, ContentTopic[]>;

interface IRelayAPI extends GossipSub {
export interface IRelay extends GossipSub {
send: (encoder: IEncoder, message: IMessage) => Promise<SendResult>;
addObserver: <T extends IDecodedMessage>(
decoder: IDecoder<T>,
callback: Callback<T>
) => () => void;
getMeshPeers: () => string[];
getActiveSubscriptions: () => ActiveSubscriptions | undefined;
}

export type IRelay = IRelayAPI & IRelayEmitter;