Extract subscription logic from start method

This commit is contained in:
Franck Royer 2021-06-09 10:13:41 +10:00
parent 4366618bda
commit eb521b4dbd
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 27 additions and 18 deletions

View File

@ -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