Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: make pubsub.unsubscribe async and alter pubsub.subscribe signature
Browse files Browse the repository at this point in the history
BREAKING CHANGE: pubsub.unsubscribe is now async and argument order for pubsub.subscribe has changed

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw authored and daviddias committed May 14, 2018
1 parent 31f97eb commit a115829
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 11 additions & 6 deletions src/core/components/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
}

Expand All @@ -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) => {
Expand Down
7 changes: 2 additions & 5 deletions src/http/api/resources/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a115829

Please sign in to comment.