From eb521b4dbd28e1d6ee644ddf0719008a4773bf1a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 9 Jun 2021 10:13:41 +1000 Subject: [PATCH] Extract subscription logic from start method --- src/lib/waku_relay/index.ts | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index b11eef2a1d..c1dabf8d71 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -98,24 +98,8 @@ export class WakuRelay extends Gossipsub implements Pubsub { * @returns {void} */ public start(): void { - this.on(constants.DefaultPubsubTopic, (event) => { - const wakuMsg = WakuMessage.decode(event.data); - if (this.observers['']) { - this.observers[''].forEach((callbackFn) => { - callbackFn(wakuMsg); - }); - } - if (wakuMsg.contentTopic) { - if (this.observers[wakuMsg.contentTopic]) { - this.observers[wakuMsg.contentTopic].forEach((callbackFn) => { - callbackFn(wakuMsg); - }); - } - } - }); - super.start(); - super.subscribe(constants.DefaultPubsubTopic); + this.subscribe(constants.DefaultPubsubTopic); } /** @@ -168,12 +152,37 @@ export class WakuRelay extends Gossipsub implements Pubsub { }); } + /** + * Subscribe to a pubsub topic and start emitting Waku messages to observers. + * + * @override + */ + subscribe(pubsubTopic: string): void { + this.on(pubsubTopic, (event) => { + const wakuMsg = WakuMessage.decode(event.data); + if (this.observers['']) { + this.observers[''].forEach((callbackFn) => { + callbackFn(wakuMsg); + }); + } + if (wakuMsg.contentTopic) { + if (this.observers[wakuMsg.contentTopic]) { + this.observers[wakuMsg.contentTopic].forEach((callbackFn) => { + callbackFn(wakuMsg); + }); + } + } + }); + + super.subscribe(pubsubTopic); + } + /** * Join pubsub topic. * This is present to override the behavior of Gossipsub and should not * be used by API Consumers * - * @ignore + * @internal * @param {string} topic * @returns {void} * @override