Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
feat: add listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 11, 2020
1 parent 2e7c1b3 commit 3b8279d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,21 @@ class FloodSub extends BaseProtocol {
* Subscribe to the given topic(s).
* @override
* @param {Array<string>|string} topics
* @param {function} [handler]
* @returns {void}
*/
subscribe (topics) {
subscribe (topics, handler) {
if (!this.started) {
throw new Error('FloodSub is not started')
}

topics = ensureArray(topics)
topics.forEach((topic) => this.subscriptions.add(topic))
topics.forEach((topic) => {
this.subscriptions.add(topic)

// Bind provider handler
handler && this.on(topic, handler)
})

this.peers.forEach((_, id) => this._sendSubscriptions(id, topics, true))
}
Expand All @@ -275,16 +281,24 @@ class FloodSub extends BaseProtocol {
* Unsubscribe from the given topic(s).
* @override
* @param {Array<string>|string} topics
* @param {function} [handler]
* @returns {void}
*/
unsubscribe (topics) {
unsubscribe (topics, handler) {
if (!this.started) {
throw new Error('FloodSub is not started')
}

topics = ensureArray(topics)

topics.forEach((topic) => this.subscriptions.delete(topic))
topics.forEach((topic) => {
this.subscriptions.delete(topic)

// Remove bind handlers
if (!handler) {

}
})

this.peers.forEach((_, id) => this._sendSubscriptions(id, topics, false))
}
Expand Down

0 comments on commit 3b8279d

Please sign in to comment.