diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index d5825960e2..a5530226c4 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -4,10 +4,9 @@ const promisify = require('promisify-es6') module.exports = function pubsub (self) { return { - subscribe: (topic, options, handler, callback) => { + subscribe: (topic, handler, options, callback) => { if (typeof options === 'function') { - callback = handler - handler = options + callback = options options = {} } @@ -20,13 +19,19 @@ module.exports = function pubsub (self) { resolve() }) }) - } else { - self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) } + + self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) }, - unsubscribe: (topic, handler) => { + unsubscribe: (topic, handler, callback) => { self._libp2pNode.pubsub.unsubscribe(topic, handler) + + if (!callback) { + return Promise.resolve() + } + + process.nextTick(() => callback()) }, publish: promisify((topic, data, callback) => { diff --git a/src/http/api/resources/pubsub.js b/src/http/api/resources/pubsub.js index 065746c62e..8b88bd3dce 100644 --- a/src/http/api/resources/pubsub.js +++ b/src/http/api/resources/pubsub.js @@ -33,16 +33,13 @@ exports.subscribe = { res.write('{}\n') const unsubscribe = () => { - ipfs.pubsub.unsubscribe(topic, handler) - res.end() + ipfs.pubsub.unsubscribe(topic, handler, () => res.end()) } request.once('disconnect', unsubscribe) request.once('finish', unsubscribe) - ipfs.pubsub.subscribe(topic, { - discover: discover - }, handler, (err) => { + ipfs.pubsub.subscribe(topic, handler, { discover: discover }, (err) => { if (err) { return reply(err) }