From de187ba87270dda3dbba7dca61dde0a7609393cc Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 27 May 2015 10:00:23 +0300 Subject: [PATCH 1/2] added more logging --- lib/node.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/node.js b/lib/node.js index 0a5ac60..d6cdc5e 100644 --- a/lib/node.js +++ b/lib/node.js @@ -277,6 +277,8 @@ Node.prototype.emit = function(message, payload) { try { socket.emit(message, payload); + console.success('wsc', 'Socket emited message:', chalk.reset.cyan(message)); + console.success('wsc', payload); } catch (err) { console.error('wsc', 'Socket emit error:', err); @@ -502,7 +504,7 @@ Node.prototype.getHistory = function (range) } } - socket.emit('history', { + self.emit('history', { id: self.id, history: results.reverse() }); @@ -540,14 +542,16 @@ Node.prototype.prepareStats = function () Node.prototype.sendUpdate = function (force) { - if( this.changed() || force ) + if( this.changed() || force ) { + console.info("wsc", "Sending", chalk.reset.blue((force ? "forced" : "changed")), chalk.bold.white("update")); this.emit('update', this.prepareStats()); + } } Node.prototype.ping = function() { this._latency = _.now(); - this.emit('node-ping', { id: this.id }); + socket.emit('node-ping', { id: this.id }); }; Node.prototype.setWatches = function() From 12685615676e31385b7c7d8b88ac7ffc1b53f803 Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 27 May 2015 10:15:22 +0300 Subject: [PATCH 2/2] refactoring --- lib/node.js | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/lib/node.js b/lib/node.js index d6cdc5e..6133112 100644 --- a/lib/node.js +++ b/lib/node.js @@ -199,8 +199,8 @@ Node.prototype.setupSockets = function() self._socket = true; console.success('wsc', 'The socket connection has been established.'); - self.updateBlock(); - self.update(true); + self.getLatestBlock(); + self.getStats(true); }) .on('data', function incoming(data) { @@ -342,7 +342,7 @@ Node.prototype.formatBlock = function (block) return false; } -Node.prototype.getStatsBlock = function () +Node.prototype.getLatestBlock = function () { var self = this; @@ -353,12 +353,12 @@ Node.prototype.getStatsBlock = function () { this._startBlockFetch = _.now(); web3.eth.getBlock('latest', false, function(error, result) { - self.sendStatsBlock(error, result); + self.validateLatestBlock(error, result); }); } } -Node.prototype.sendStatsBlock = function (error, result) +Node.prototype.validateLatestBlock = function (error, result) { if( !error ) { @@ -369,9 +369,7 @@ Node.prototype.sendStatsBlock = function (error, result) if( this.stats.block.number !== block.number ) { this.stats.block = block; - console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); - this.sendUpdate(); } else @@ -386,7 +384,7 @@ Node.prototype.sendStatsBlock = function (error, result) } else { - console.error("xx>", "getStatsBlock couldn't fetch block..."); + console.error("xx>", "getLatestBlock couldn't fetch block..."); console.error("xx>", error); } } @@ -489,8 +487,6 @@ Node.prototype.getHistory = function (range) }, function (err, results) { - console.timeEnd('=H=', 'his', 'Got history in'); - if (err) { console.error('his', 'history fetch failed:', err); @@ -508,23 +504,11 @@ Node.prototype.getHistory = function (range) id: self.id, history: results.reverse() }); + + console.timeEnd('=H=', 'his', 'Got history in'); }); } -Node.prototype.updateBlock = function() -{ - this.getStatsBlock(); - - return this; -}; - -Node.prototype.update = function(forced) -{ - this.getStats(forced); - - return this; -}; - Node.prototype.changed = function () { var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) ); @@ -570,12 +554,12 @@ Node.prototype.setWatches = function() if(time > 50) { - self.updateBlock(); + self.getLatestBlock(); } else { debounce(function() { - self.updateBlock(); + self.getLatestBlock(); }, 50); } }); @@ -598,12 +582,12 @@ Node.prototype.setWatches = function() if(time > 50) { - self.update(true); + self.getStats(true); } else { debounce(function() { - self.update(true); + self.getStats(true); }, 50); } }); @@ -615,7 +599,7 @@ Node.prototype.setWatches = function() } this.updateInterval = setInterval( function(){ - self.update(); + self.getStats(); }, UPDATE_INTERVAL); this.pingInterval = setInterval( function(){