fix: promisified methods (#398)

* chore: update ws rendezvous dep

test(fix): fix tests with latest ws rendezvous server

* fix: promisification of libp2p methods

test: add tests to verify promisify support until async/await endeavor is complete

* chore: fix linting
This commit is contained in:
Jacob Heun
2019-08-06 10:53:23 +02:00
committed by GitHub
parent 9a8d609a59
commit ff7a6c86a0
9 changed files with 169 additions and 108 deletions

View File

@ -187,6 +187,11 @@ class Libp2p extends EventEmitter {
})
this._peerDiscovered = this._peerDiscovered.bind(this)
// promisify all instance methods
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
this[method] = promisify(this[method], { context: this })
})
}
/**
@ -295,6 +300,13 @@ class Libp2p extends EventEmitter {
})
}
/**
* Disconnects from the given peer
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
* @param {function(Error)} callback
* @returns {void}
*/
hangUp (peer, callback) {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
@ -303,6 +315,13 @@ class Libp2p extends EventEmitter {
})
}
/**
* Pings the provided peer
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
* @param {function(Error, Ping)} callback
* @returns {void}
*/
ping (peer, callback) {
if (!this.isStarted()) {
return callback(notStarted('ping', this.state._state))
@ -550,11 +569,6 @@ class Libp2p extends EventEmitter {
}
}
// promisify all instance methods
['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
Libp2p[method] = promisify(Libp2p[method])
})
module.exports = Libp2p
/**
* Like `new Libp2p(options)` except it will create a `PeerInfo`