Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make subscribe comply with ipfs interface #389

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,31 @@ module.exports = (node) => {
node._floodSub = floodSub

return {
subscribe: promisify((topic, options, handler, callback) => {
/**
* Subscribe the given handler to a pubsub topic
*
* @param {string} topic
* @param {function} handler The handler to subscribe
* @param {object|null} [options]
* @param {function} [callback] An optional callback
*
* @returns {Promise|void} A promise is returned if no callback is provided
*
* @example <caption>Subscribe a handler to a topic</caption>
*
* // `null` must be passed for options until subscribe is no longer using promisify
* const handler = (message) => { }
* await libp2p.subscribe(topic, handler, null)
*
* @example <caption>Use a callback instead of the Promise api</caption>
*
* // `options` may be passed or omitted when supplying a callback
* const handler = (message) => { }
* libp2p.subscribe(topic, handler, callback)
*/
subscribe: promisify((topic, handler, options, callback) => {
if (typeof options === 'function') {
callback = handler
handler = options
callback = options
options = {}
}

Expand Down
4 changes: 2 additions & 2 deletions test/pubsub.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('.pubsub', () => {
cb(err)
}),
// subscribe on the first
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, null, cb),
// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('.pubsub', () => {
cb(err)
}),
// subscribe on the first
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, {}, cb),
// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
Expand Down