Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pubsub #467

Merged
merged 9 commits into from
Nov 15, 2019
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
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,18 @@ class Node extends Libp2p {

**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.

#### Create a Node - `Libp2p.createLibp2p(options, callback)`
#### Create a Node - `Libp2p.create(options)`

> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead

```js
const { createLibp2p } = require('libp2p')
createLibp2p(options, (err, libp2p) => {
if (err) throw err
libp2p.start((err) => {
if (err) throw err
})
})
const { create } = require('libp2p')
const libp2p = await create(options)

await libp2p.start()
```

- `options`: Object of libp2p configuration options
- `callback`: Function with signature `function (Error, Libp2p) {}`

#### Create a Node alternative - `new Libp2p(options)`

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@
"it-protocol-buffers": "^0.2.0",
"latency-monitor": "~0.2.1",
"libp2p-crypto": "^0.17.1",
"libp2p-interfaces": "^0.1.3",
"libp2p-interfaces": "^0.1.5",
"mafmt": "^7.0.0",
"merge-options": "^1.0.1",
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
"moving-average": "^1.0.0",
"multiaddr": "^7.1.0",
"multistream-select": "^0.15.0",
"once": "^1.4.0",
"p-map": "^3.0.0",
"p-queue": "^6.1.1",
"p-settle": "^3.1.0",
"peer-id": "^0.13.3",
Expand Down Expand Up @@ -90,8 +91,8 @@
"libp2p-bootstrap": "^0.9.7",
"libp2p-delegated-content-routing": "^0.2.2",
"libp2p-delegated-peer-routing": "^0.2.2",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "~0.0.4",
"libp2p-floodsub": "^0.19.0",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#beta/async",
"libp2p-kad-dht": "^0.15.3",
"libp2p-mdns": "^0.12.3",
"libp2p-mplex": "^0.9.1",
Expand All @@ -103,6 +104,7 @@
"lodash.times": "^4.3.2",
"nock": "^10.0.6",
"p-defer": "^3.0.0",
"p-wait-for": "^3.1.0",
"portfinder": "^1.0.20",
"pull-goodbye": "0.0.2",
"pull-length-prefixed": "^1.3.3",
Expand Down
108 changes: 0 additions & 108 deletions src/connection-manager/topology.js

This file was deleted.

39 changes: 23 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

const FSM = require('fsm-event')
const EventEmitter = require('events').EventEmitter
const { EventEmitter } = require('events')
const debug = require('debug')
const log = debug('libp2p')
log.error = debug('libp2p:error')
const errCode = require('err-code')
const promisify = require('promisify-es6')

const each = require('async/each')
const nextTick = require('async/nextTick')

const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
Expand Down Expand Up @@ -66,6 +65,8 @@ class Libp2p extends EventEmitter {
this._transport = [] // Transport instances/references
this._discovery = [] // Discovery service instances/references

this.peerStore = new PeerStore()

// create the switch, and listen for errors
this._switch = new Switch(this.peerInfo, this.peerStore, this._options.switch)

Expand Down Expand Up @@ -147,7 +148,7 @@ class Libp2p extends EventEmitter {
}

// start pubsub
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
if (this._modules.pubsub) {
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
}

Expand Down Expand Up @@ -251,6 +252,7 @@ class Libp2p extends EventEmitter {
this.state('stop')

try {
this.pubsub && await this.pubsub.stop()
await this.transportManager.close()
await this._switch.stop()
} catch (err) {
Expand Down Expand Up @@ -385,10 +387,16 @@ class Libp2p extends EventEmitter {
const multiaddrs = this.peerInfo.multiaddrs.toArray()

// Start parallel tasks
const tasks = [
this.transportManager.listen(multiaddrs)
]

if (this._config.pubsub.enabled) {
this.pubsub && this.pubsub.start()
}

try {
await Promise.all([
this.transportManager.listen(multiaddrs)
])
await Promise.all(tasks)
} catch (err) {
log.error(err)
this.emit('error', err)
Expand Down Expand Up @@ -483,16 +491,15 @@ module.exports = Libp2p
* Like `new Libp2p(options)` except it will create a `PeerInfo`
* instance if one is not provided in options.
* @param {object} options Libp2p configuration options
* @param {function(Error, Libp2p)} callback
* @returns {void}
* @returns {Libp2p}
*/
module.exports.createLibp2p = promisify((options, callback) => {
module.exports.create = async (options = {}) => {
if (options.peerInfo) {
return nextTick(callback, null, new Libp2p(options))
return new Libp2p(options)
}
PeerInfo.create((err, peerInfo) => {
if (err) return callback(err)
options.peerInfo = peerInfo
callback(null, new Libp2p(options))
})
})

const peerInfo = await PeerInfo.create()

options.peerInfo = peerInfo
return new Libp2p(options)
}
Loading