diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 99775da5..b5b80f5b 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -322,6 +322,7 @@ Peer.prototype._handleVersion = function handleVersion(payload) { // ACK this._write(this.framer.verack()); this.version = payload; + this.emit('version', payload); }; Peer.prototype._handleGetData = function handleGetData(items) { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 91df06c8..0d411a56 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -340,6 +340,14 @@ Pool.prototype._addPeer = function _addPeer(backoff) { peer.on('txs', function(txs) { self.emit('txs', txs, peer); }); + + peer.on('version', function(version) { + self.emit('version', version, peer); + }); + + utils.nextTick(function() { + self.emit('peer', peer); + }); }; Pool.prototype._removePeer = function _removePeer(peer) { diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 5de833ce..44ac8387 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -372,9 +372,18 @@ utils.isEqual = function isEqual(a, b) { return true; }; -// TODO(indutny): use process.nextTick in node.js utils.nextTick = function nextTick(fn) { - setTimeout(fn, 0); + if (typeof setImmediate === 'function') { + setImmediate(fn); + return; + } + + if (typeof process === 'object' && process.nextTick) { + process.nextTick(fn); + return; + } + + setTimeout(fn, 1); }; function RequestCache() {