Merge pull request #25 from indutny/peer-events

Peer events
This commit is contained in:
Christopher Jeffrey 2014-06-07 03:48:43 -05:00
commit 2697d37753
3 changed files with 20 additions and 2 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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() {