Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

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

Merged
merged 3 commits into from
May 12, 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 @@ -235,8 +235,8 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P
- [`ipfs.dht.put()`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DHT.md#put)

- [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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"is-pull-stream": "0.0.0",
"is-stream": "^1.1.0",
"libp2p-crypto": "^0.13.0",
"lru-cache": "^4.1.2",
"lru-cache": "^4.1.3",
"multiaddr": "^5.0.0",
"multibase": "~0.4.0",
"multihashes": "~0.4.13",
Expand All @@ -57,7 +57,7 @@
"pump": "^3.0.0",
"qs": "^6.5.2",
"readable-stream": "^2.3.6",
"stream-http": "^2.8.1",
"stream-http": "^2.8.2",
"stream-to-pull-stream": "^1.7.2",
"streamifier": "^0.1.1",
"tar-stream": "^1.6.0"
Expand All @@ -74,15 +74,15 @@
"aegir": "^13.1.0",
"browser-process-platform": "^0.1.1",
"chai": "^4.1.2",
"cross-env": "^5.1.4",
"cross-env": "^5.1.5",
"dirty-chai": "^2.0.1",
"eslint-plugin-react": "^7.7.0",
"eslint-plugin-react": "^7.8.1",
"go-ipfs-dep": "^0.4.14",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.64.3",
"interface-ipfs-core": "~0.65.5",
"ipfs": "~0.28.2",
"ipfsd-ctl": "~0.33.1",
"pull-stream": "^3.6.7",
"ipfsd-ctl": "~0.33.2",
"pull-stream": "^3.6.8",
"socket.io": "^2.1.0",
"socket.io-client": "^2.1.0",
"stream-equal": "^1.1.1"
Expand Down
38 changes: 27 additions & 11 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ module.exports = (arg) => {
const subscriptions = {}
ps.id = Math.random()
return {
subscribe: (topic, options, handler, callback) => {
subscribe: (topic, handler, options, callback) => {
const defaultOptions = {
discover: false
}

if (typeof options === 'function') {
callback = handler
handler = options
callback = options
options = defaultOptions
}

Expand All @@ -39,14 +38,15 @@ module.exports = (arg) => {
if (!callback) {
return Promise.reject(NotSupportedError())
}
return callback(NotSupportedError())

return process.nextTick(() => callback(NotSupportedError()))
}

// promisify doesn't work as we always pass a
// function as last argument (`handler`)
if (!callback) {
return new Promise((resolve, reject) => {
subscribe(topic, options, handler, (err) => {
subscribe(topic, handler, options, (err) => {
if (err) {
return reject(err)
}
Expand All @@ -55,24 +55,40 @@ module.exports = (arg) => {
})
}

subscribe(topic, options, handler, callback)
subscribe(topic, handler, options, callback)
},
unsubscribe: (topic, handler) => {
unsubscribe: (topic, handler, callback) => {
if (!isNode) {
throw NotSupportedError()
if (!callback) {
return Promise.reject(NotSupportedError())
}

return process.nextTick(() => callback(NotSupportedError()))
}

if (ps.listenerCount(topic) === 0 || !subscriptions[topic]) {
throw new Error(`Not subscribed to '${topic}'`)
const err = new Error(`Not subscribed to '${topic}'`)

if (!callback) {
return Promise.reject(err)
}

return process.nextTick(() => callback(err))
}

ps.removeListener(topic, handler)

// Drop the request once we are actualy done
// Drop the request once we are actually done
if (ps.listenerCount(topic) === 0) {
subscriptions[topic].abort()
subscriptions[topic] = null
}

if (!callback) {
return Promise.resolve()
}

process.nextTick(() => callback())
},
publish: promisify((topic, data, callback) => {
if (!isNode) {
Expand Down Expand Up @@ -118,7 +134,7 @@ module.exports = (arg) => {
}
}

function subscribe (topic, options, handler, callback) {
function subscribe (topic, handler, options, callback) {
ps.on(topic, handler)

if (subscriptions[topic]) {
Expand Down
11 changes: 4 additions & 7 deletions test/pubsub-in-browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('.pubsub is not supported in the browser, yet!', function () {
describe('.subscribe', () => {
const handler = () => {}
it('throws an error if called in the browser', (done) => {
ipfs.pubsub.subscribe(topic, {}, handler, (err, topics) => {
ipfs.pubsub.subscribe(topic, handler, {}, (err, topics) => {
expect(err).to.exist()
expect(err.message).to.equal(expectedError)
done()
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('.pubsub is not supported in the browser, yet!', function () {
describe('.subscribe', () => {
const handler = () => {}
it('throws an error if called in the browser', (done) => {
ipfs.pubsub.subscribe(topic, {}, handler)
ipfs.pubsub.subscribe(topic, handler, {})
.catch((err) => {
expect(err).to.exist()
expect(err.message).to.equal(expectedError)
Expand Down Expand Up @@ -148,14 +148,11 @@ describe('.pubsub is not supported in the browser, yet!', function () {

describe('.unsubscribe', () => {
it('throws an error if called in the browser', (done) => {
try {
ipfs.pubsub.unsubscribe()
done('unsubscribe() didn\'t throw an error')
} catch (err) {
ipfs.pubsub.unsubscribe('test', () => {}, (err) => {
expect(err).to.exist()
expect(err.message).to.equal(expectedError)
done()
}
})
})
})
})
Expand Down