Skip to content

Commit

Permalink
chore: refactor _pubsub logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 31, 2019
1 parent ebbeee6 commit 46d0c18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
"cids": "~0.6.0",
"debug": "^4.1.1",
"length-prefixed-stream": "github:jacobheun/length-prefixed-stream#v2.0.0-rc.1",
"libp2p": "libp2p/js-libp2p#feat/integrate-gossipsub-by-default",
"libp2p": "~0.26.0-rc.0",
"libp2p-bootstrap": "~0.9.7",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#master",
"libp2p-gossipsub": "~0.0.4",
"libp2p-kad-dht": "~0.15.2",
"libp2p-secio": "~0.11.1",
"libp2p-tcp": "~0.13.0",
Expand Down
21 changes: 16 additions & 5 deletions src/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Pubsub {
*/
subscribe (topic, options, handler) {
return new Promise((resolve, reject) => {
this.libp2p._pubSub.subscribe(topic, options, handler, (err) => {
this.libp2p._pubsub.subscribe(topic, options, handler, (err) => {
if (err) return reject(err)
resolve()
})
Expand All @@ -139,7 +139,7 @@ class Pubsub {
*/
publish (topic, data) {
return new Promise((resolve, reject) => {
this.libp2p._pubSub.publish(topic, data, (err) => {
this.libp2p._pubsub.publish(topic, data, (err) => {
if (err) return reject(err)
resolve()
})
Expand All @@ -152,12 +152,20 @@ class Pubsub {
*/
getTopics () {
return new Promise((resolve, reject) => {
this.libp2p._pubSub.ls((err, topics) => {
this.libp2p._pubsub.ls((err, topics) => {
if (err) return reject(err)
resolve(topics)
})
})
}

start (cb) {
this.libp2p._pubsub.start(cb)
}

stop (cb) {
this.libp2p._pubsub.stop(cb)
}
}

class DHT {
Expand Down Expand Up @@ -256,8 +264,11 @@ class DaemonLibp2p extends Libp2p {
super(libp2pOpts)
this.announceAddrs = announceAddrs
this.needsPullStream = libp2pOpts.config.pubsub.enabled
this._pubSub = this.pubsub
this.pubsub = new Pubsub(this)

if (libp2pOpts.config.pubsub.enabled) {
this._pubsub = this.pubsub
this.pubsub = new Pubsub(this)
}
}
get contentRouting () {
return this._contentRouting
Expand Down

0 comments on commit 46d0c18

Please sign in to comment.