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

fix: make pubsub.unsubscribe async and alter pubsub.subscribe signature #1348

Merged
merged 7 commits into from
May 14, 2018
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ The core API is grouped into several areas:
- [dht (not implemented, yet!)](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)

- [pubsub](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md)
- [`ipfs.pubsub.subscribe(topic, options, handler, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe)
- [`ipfs.pubsub.unsubscribe(topic, handler)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubunsubscribe)
- [`ipfs.pubsub.subscribe(topic, handler, options, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe)
- [`ipfs.pubsub.unsubscribe(topic, handler, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubunsubscribe)
- [`ipfs.pubsub.publish(topic, data, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubpublish)
- [`ipfs.pubsub.ls(topic, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubls)
- [`ipfs.pubsub.peers(topic, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubpeers)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
"expose-loader": "^0.7.5",
"form-data": "^2.3.2",
"hat": "0.0.3",
"interface-ipfs-core": "~0.64.3",
"ipfsd-ctl": "~0.33.1",
"interface-ipfs-core": "~0.65.5",
"ipfsd-ctl": "~0.34.0",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
"ncp": "^2.0.0",
Expand Down Expand Up @@ -105,7 +105,7 @@
"hapi-set-header": "^1.0.2",
"hoek": "^5.0.3",
"human-to-milliseconds": "^1.0.0",
"ipfs-api": "^20.2.1",
"ipfs-api": "^21.0.0",
"ipfs-bitswap": "~0.20.0",
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
Expand Down
18 changes: 12 additions & 6 deletions src/core/components/pubsub.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const promisify = require('promisify-es6')
const setImmediate = require('async/setImmediate')

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 +20,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()
}

setImmediate(() => 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