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 custom events to Relay #1213

Merged
merged 9 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 26 additions & 3 deletions packages/core/src/lib/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 {
Callback,
IDecoder,
Expand Down Expand Up @@ -35,6 +36,10 @@ 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 All @@ -50,7 +55,7 @@ class Relay extends GossipSub implements IRelay {
* observers called when receiving new message.
* Observers under key `""` are always called.
*/
public observers: Map<ContentTopic, Set<unknown>>;
private observers: Map<ContentTopic, Set<unknown>>;

constructor(
components: GossipSubComponents,
Expand Down Expand Up @@ -111,12 +116,30 @@ class Relay extends GossipSub implements IRelay {
decoder,
callback,
};
pushOrInitMapSet(this.observers, decoder.contentTopic, observer);
const contentTopic = decoder.contentTopic;

pushOrInitMapSet(this.observers, contentTopic, observer);

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

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

this.dispatchEvent(
new CustomEvent<BasicEventPayload>("observer:removed", {
detail: {
contentTopic,
},
})
);
}
};
}
Expand Down
14 changes: 12 additions & 2 deletions packages/interfaces/src/relay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub";
import type { EventEmitter } from "@libp2p/interfaces/events";

import type {
IDecodedMessage,
Expand All @@ -8,11 +9,20 @@ import type {
} from "./message.js";
import type { Callback, SendResult } from "./protocols.js";

export interface IRelay extends GossipSub {
export interface RelayEvents {
"observer:added": CustomEvent;
"observer:removed": CustomEvent;
}

type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;

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

export type IRelay = IRelayAPI & IRelayEmitter;