diff --git a/README.md b/README.md index 423176602..92d564941 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index 0206bf56a..3784ed332 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" @@ -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" diff --git a/src/pubsub.js b/src/pubsub.js index e5ed5d54b..8dffb1362 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -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 } @@ -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) } @@ -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) { @@ -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]) { diff --git a/test/pubsub-in-browser.spec.js b/test/pubsub-in-browser.spec.js index ce2f1139e..274839805 100644 --- a/test/pubsub-in-browser.spec.js +++ b/test/pubsub-in-browser.spec.js @@ -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() @@ -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) @@ -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() - } + }) }) }) })