mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-21 13:01:33 +00:00
chore: add typedefs (#802)
This commit is contained in:
committed by
Vasco Santos
parent
7809e6444e
commit
169bb806a7
@ -1,42 +1,54 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/pubsub').InMessage} InMessage
|
||||
* @typedef {import('libp2p-interfaces/src/pubsub')} PubsubRouter
|
||||
*/
|
||||
|
||||
// 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)
|
||||
function pubsubAdapter (PubsubRouter, libp2p, options) {
|
||||
const pubsub = new PubsubRouter(libp2p, options)
|
||||
pubsub._subscribeAdapter = pubsub.subscribe
|
||||
pubsub._unsubscribeAdapter = pubsub.unsubscribe
|
||||
|
||||
/**
|
||||
* Subscribes to a given topic.
|
||||
*
|
||||
* @override
|
||||
* @param {string} topic
|
||||
* @param {(msg: InMessage) => void} [handler]
|
||||
* @returns {void}
|
||||
*/
|
||||
function subscribe (topic, handler) {
|
||||
// Bind provided handler
|
||||
handler && pubsub.on(topic, handler)
|
||||
pubsub._subscribeAdapter(topic)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from the given topic.
|
||||
*
|
||||
* @override
|
||||
* @param {string} topic
|
||||
* @param {(msg: InMessage) => void} [handler]
|
||||
* @returns {void}
|
||||
*/
|
||||
function unsubscribe (topic, handler) {
|
||||
if (!handler) {
|
||||
pubsub.removeAllListeners(topic)
|
||||
} else {
|
||||
pubsub.removeListener(topic, handler)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
if (pubsub.listenerCount(topic) === 0) {
|
||||
pubsub._unsubscribeAdapter(topic)
|
||||
}
|
||||
}
|
||||
|
||||
return new Pubsub(libp2p, options)
|
||||
pubsub.subscribe = subscribe
|
||||
pubsub.unsubscribe = unsubscribe
|
||||
|
||||
return pubsub
|
||||
}
|
||||
|
||||
module.exports = pubsubAdapter
|
||||
|
Reference in New Issue
Block a user