From eadb7a50c7d95f7f2060711c49752638d012ece6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 6 Jun 2014 16:20:50 -0500 Subject: [PATCH 1/2] pool: add version and peer events for peers. --- lib/bcoin/peer.js | 1 + lib/bcoin/pool.js | 8 ++++++++ 2 files changed, 9 insertions(+) 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 eed4b085..f67a12ff 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) { From 7b928f079c1045838cb00a28133fab2c20be0f2d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 6 Jun 2014 16:22:47 -0500 Subject: [PATCH 2/2] utils: improve nextTick. --- lib/bcoin/utils.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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() {