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

Commit a115829

Browse files
alanshawdaviddias
authored andcommittedMay 14, 2018
fix: make pubsub.unsubscribe async and alter pubsub.subscribe signature
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>
1 parent 31f97eb commit a115829

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
 

‎src/core/components/pubsub.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const promisify = require('promisify-es6')
44

55
module.exports = function pubsub (self) {
66
return {
7-
subscribe: (topic, options, handler, callback) => {
7+
subscribe: (topic, handler, options, callback) => {
88
if (typeof options === 'function') {
9-
callback = handler
10-
handler = options
9+
callback = options
1110
options = {}
1211
}
1312

@@ -20,13 +19,19 @@ module.exports = function pubsub (self) {
2019
resolve()
2120
})
2221
})
23-
} else {
24-
self._libp2pNode.pubsub.subscribe(topic, options, handler, callback)
2522
}
23+
24+
self._libp2pNode.pubsub.subscribe(topic, options, handler, callback)
2625
},
2726

28-
unsubscribe: (topic, handler) => {
27+
unsubscribe: (topic, handler, callback) => {
2928
self._libp2pNode.pubsub.unsubscribe(topic, handler)
29+
30+
if (!callback) {
31+
return Promise.resolve()
32+
}
33+
34+
process.nextTick(() => callback())
3035
},
3136

3237
publish: promisify((topic, data, callback) => {

‎src/http/api/resources/pubsub.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ exports.subscribe = {
3333
res.write('{}\n')
3434

3535
const unsubscribe = () => {
36-
ipfs.pubsub.unsubscribe(topic, handler)
37-
res.end()
36+
ipfs.pubsub.unsubscribe(topic, handler, () => res.end())
3837
}
3938

4039
request.once('disconnect', unsubscribe)
4140
request.once('finish', unsubscribe)
4241

43-
ipfs.pubsub.subscribe(topic, {
44-
discover: discover
45-
}, handler, (err) => {
42+
ipfs.pubsub.subscribe(topic, handler, { discover: discover }, (err) => {
4643
if (err) {
4744
return reply(err)
4845
}

0 commit comments

Comments
 (0)
This repository has been archived.