mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-21 04:51:33 +00:00
feat: gossipsub 1.1 (#733)
* feat: gossipsub 1.1 BREAKING CHANGE: pubsub implementation is now directly exposed and its API was updated according to the new pubsub interface in js-libp2p-interfaces repo * chore: use gossipsub branch with src added * fix: add pubsub handlers adapter * chore: fix deps * chore: update pubsub docs and examples * chore: apply suggestions from code review Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: use new floodsub * chore: change validator doc set Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: add new gossipsub src Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
40
src/pubsub-adapter.js
Normal file
40
src/pubsub-adapter.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
// Pubsub adapter to keep API with handlers while not removed.
|
||||
module.exports = (PubsubRouter, libp2p, options) => {
|
||||
class Pubsub extends PubsubRouter {
|
||||
/**
|
||||
* Subscribes to a given topic.
|
||||
* @override
|
||||
* @param {string} topic
|
||||
* @param {function(msg: InMessage)} [handler]
|
||||
* @returns {void}
|
||||
*/
|
||||
subscribe (topic, handler) {
|
||||
// Bind provided handler
|
||||
handler && this.on(topic, handler)
|
||||
super.subscribe(topic)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from the given topic.
|
||||
* @override
|
||||
* @param {string} topic
|
||||
* @param {function(msg: InMessage)} [handler]
|
||||
* @returns {void}
|
||||
*/
|
||||
unsubscribe (topic, handler) {
|
||||
if (!handler) {
|
||||
this.removeAllListeners(topic)
|
||||
} else {
|
||||
this.removeListener(topic, handler)
|
||||
}
|
||||
|
||||
if (this.listenerCount(topic) === 0) {
|
||||
super.unsubscribe(topic)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Pubsub(libp2p, options)
|
||||
}
|
Reference in New Issue
Block a user