feat!: add custom events to Relay and make observers private (#1213)

This commit is contained in:
Sasha 2023-03-15 21:47:56 +01:00 committed by GitHub
parent a20b797c5b
commit 275b16641e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import {
} from "@chainsafe/libp2p-gossipsub"; } from "@chainsafe/libp2p-gossipsub";
import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types"; import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types";
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types"; import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
import { CustomEvent } from "@libp2p/interfaces/events";
import type { import type {
Callback, Callback,
IDecoder, IDecoder,
@ -35,6 +36,10 @@ export type Observer<T extends IDecodedMessage> = {
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts; export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts;
export type ContentTopic = string; export type ContentTopic = string;
type BasicEventPayload = {
contentTopic: string;
};
/** /**
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/). * Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
* Must be passed as a `pubsub` module to a `Libp2p` instance. * Must be passed as a `pubsub` module to a `Libp2p` instance.
@ -50,7 +55,7 @@ class Relay extends GossipSub implements IRelay {
* observers called when receiving new message. * observers called when receiving new message.
* Observers under key `""` are always called. * Observers under key `""` are always called.
*/ */
public observers: Map<ContentTopic, Set<unknown>>; private observers: Map<ContentTopic, Set<unknown>>;
constructor( constructor(
components: GossipSubComponents, components: GossipSubComponents,
@ -111,12 +116,30 @@ class Relay extends GossipSub implements IRelay {
decoder, decoder,
callback, 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 () => { return () => {
const observers = this.observers.get(decoder.contentTopic); const observers = this.observers.get(contentTopic);
if (observers) { if (observers) {
observers.delete(observer); observers.delete(observer);
this.dispatchEvent(
new CustomEvent<BasicEventPayload>("observer:removed", {
detail: {
contentTopic,
},
})
);
} }
}; };
} }

View File

@ -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 { import type {
IDecodedMessage, IDecodedMessage,
@ -8,7 +9,14 @@ import type {
} from "./message.js"; } from "./message.js";
import type { Callback, SendResult } from "./protocols.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>; send: (encoder: IEncoder, message: IMessage) => Promise<SendResult>;
addObserver: <T extends IDecodedMessage>( addObserver: <T extends IDecodedMessage>(
decoder: IDecoder<T>, decoder: IDecoder<T>,
@ -16,3 +24,5 @@ export interface IRelay extends GossipSub {
) => () => void; ) => () => void;
getMeshPeers: () => string[]; getMeshPeers: () => string[];
} }
export type IRelay = IRelayAPI & IRelayEmitter;