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 15, 2019
1 parent ebbeee6 commit eeb0f98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"libp2p": "libp2p/js-libp2p#feat/integrate-gossipsub-by-default",
"libp2p-bootstrap": "~0.9.7",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#master",
"libp2p-gossipsub": "~0.0.3",
"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 eeb0f98

Please sign in to comment.