Skip to content

Commit 9554b05

Browse files
authored
fix: make subscribe comply with ipfs interface (#389)
BREAKING CHANGE: The ipfs interface specified that options should be provided after the handler, not before. https://github.com/ipfs/interface-js-ipfs-core/blob/v0.109.0/SPEC/PUBSUB.md#pubsubsubscribe This corrects the order of parameters. See the jsdocs examples for subscribe to see how it should be used.
1 parent df6ef45 commit 9554b05

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/pubsub.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,31 @@ module.exports = (node) => {
1313
node._floodSub = floodSub
1414

1515
return {
16-
subscribe: promisify((topic, options, handler, callback) => {
16+
/**
17+
* Subscribe the given handler to a pubsub topic
18+
*
19+
* @param {string} topic
20+
* @param {function} handler The handler to subscribe
21+
* @param {object|null} [options]
22+
* @param {function} [callback] An optional callback
23+
*
24+
* @returns {Promise|void} A promise is returned if no callback is provided
25+
*
26+
* @example <caption>Subscribe a handler to a topic</caption>
27+
*
28+
* // `null` must be passed for options until subscribe is no longer using promisify
29+
* const handler = (message) => { }
30+
* await libp2p.subscribe(topic, handler, null)
31+
*
32+
* @example <caption>Use a callback instead of the Promise api</caption>
33+
*
34+
* // `options` may be passed or omitted when supplying a callback
35+
* const handler = (message) => { }
36+
* libp2p.subscribe(topic, handler, callback)
37+
*/
38+
subscribe: promisify((topic, handler, options, callback) => {
1739
if (typeof options === 'function') {
18-
callback = handler
19-
handler = options
40+
callback = options
2041
options = {}
2142
}
2243

test/pubsub.node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('.pubsub', () => {
7272
cb(err)
7373
}),
7474
// subscribe on the first
75-
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
75+
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, null, cb),
7676
// Wait a moment before publishing
7777
(cb) => setTimeout(cb, 500),
7878
// publish on the second
@@ -110,7 +110,7 @@ describe('.pubsub', () => {
110110
cb(err)
111111
}),
112112
// subscribe on the first
113-
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
113+
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, {}, cb),
114114
// Wait a moment before publishing
115115
(cb) => setTimeout(cb, 500),
116116
// publish on the second

0 commit comments

Comments
 (0)