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

Commit

Permalink
fix: correct pubsub methods for next libp2p release
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Jul 31, 2019
1 parent 6a47db6 commit 0315c72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"just-flatten-it": "^2.1.0",
"just-safe-set": "^2.1.0",
"kind-of": "^6.0.2",
"libp2p": "libp2p/js-libp2p#feat/integrate-gossipsub-by-default",
"libp2p": "~0.26.0-rc.0",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.16.0",
"libp2p-delegated-content-routing": "^0.2.3",
Expand Down
27 changes: 9 additions & 18 deletions src/core/components/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,56 @@ module.exports = function pubsub (self) {
options = {}
}

if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
return callback
? setImmediate(() => callback(errPubsubDisabled()))
: Promise.reject(errPubsubDisabled())
}

if (!callback) {
return new Promise((resolve, reject) => {
self.libp2p.pubsub.subscribe(topic, options, handler, (err) => {
if (err) {
return reject(err)
}
resolve()
})
})
return self.libp2p.pubsub.subscribe(topic, handler, options)
}

self.libp2p.pubsub.subscribe(topic, options, handler, callback)
},

unsubscribe: (topic, handler, callback) => {
if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
return callback
? setImmediate(() => callback(errPubsubDisabled()))
: Promise.reject(errPubsubDisabled())
}

self.libp2p.pubsub.unsubscribe(topic, handler)

if (!callback) {
return Promise.resolve()
return self.libp2p.pubsub.unsubscribe(topic, handler)
}

setImmediate(() => callback())
self.libp2p.pubsub.unsubscribe(topic, handler, callback)
},

publish: promisify((topic, data, callback) => {
if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self.libp2p.pubsub.publish(topic, data, callback)
}),

ls: promisify((callback) => {
if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self.libp2p.pubsub.ls(callback)
}),

peers: promisify((topic, callback) => {
if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self.libp2p.pubsub.peers(topic, callback)
}),

setMaxListeners (n) {
if (!self._options.EXPERIMENTAL.pubsub) {
if (!self.libp2p.pubsub) {
throw errPubsubDisabled()
}
self.libp2p.pubsub.setMaxListeners(n)
Expand Down

0 comments on commit 0315c72

Please sign in to comment.