Merge pull request #148 from cubedro/develop

Refactoring
This commit is contained in:
Marian OANCΞA 2015-05-27 12:12:26 +03:00
commit e21ba9f982

View File

@ -199,8 +199,8 @@ Node.prototype.setupSockets = function()
self._socket = true; self._socket = true;
console.success('wsc', 'The socket connection has been established.'); console.success('wsc', 'The socket connection has been established.');
self.updateBlock(); self.getLatestBlock();
self.update(true); self.getStats(true);
}) })
.on('data', function incoming(data) .on('data', function incoming(data)
{ {
@ -277,6 +277,8 @@ Node.prototype.emit = function(message, payload)
{ {
try { try {
socket.emit(message, payload); socket.emit(message, payload);
console.success('wsc', 'Socket emited message:', chalk.reset.cyan(message));
console.success('wsc', payload);
} }
catch (err) { catch (err) {
console.error('wsc', 'Socket emit error:', err); console.error('wsc', 'Socket emit error:', err);
@ -340,7 +342,7 @@ Node.prototype.formatBlock = function (block)
return false; return false;
} }
Node.prototype.getStatsBlock = function () Node.prototype.getLatestBlock = function ()
{ {
var self = this; var self = this;
@ -351,12 +353,12 @@ Node.prototype.getStatsBlock = function ()
{ {
this._startBlockFetch = _.now(); this._startBlockFetch = _.now();
web3.eth.getBlock('latest', false, function(error, result) { 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 ) if( !error )
{ {
@ -367,9 +369,7 @@ Node.prototype.sendStatsBlock = function (error, result)
if( this.stats.block.number !== block.number ) if( this.stats.block.number !== block.number )
{ {
this.stats.block = block; this.stats.block = block;
console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms'));
this.sendUpdate(); this.sendUpdate();
} }
else else
@ -384,7 +384,7 @@ Node.prototype.sendStatsBlock = function (error, result)
} }
else else
{ {
console.error("xx>", "getStatsBlock couldn't fetch block..."); console.error("xx>", "getLatestBlock couldn't fetch block...");
console.error("xx>", error); console.error("xx>", error);
} }
} }
@ -487,8 +487,6 @@ Node.prototype.getHistory = function (range)
}, },
function (err, results) function (err, results)
{ {
console.timeEnd('=H=', 'his', 'Got history in');
if (err) { if (err) {
console.error('his', 'history fetch failed:', err); console.error('his', 'history fetch failed:', err);
@ -502,27 +500,15 @@ Node.prototype.getHistory = function (range)
} }
} }
socket.emit('history', { self.emit('history', {
id: self.id, id: self.id,
history: results.reverse() 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 () Node.prototype.changed = function ()
{ {
var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) ); var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) );
@ -540,14 +526,16 @@ Node.prototype.prepareStats = function ()
Node.prototype.sendUpdate = function (force) 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()); this.emit('update', this.prepareStats());
}
} }
Node.prototype.ping = function() Node.prototype.ping = function()
{ {
this._latency = _.now(); this._latency = _.now();
this.emit('node-ping', { id: this.id }); socket.emit('node-ping', { id: this.id });
}; };
Node.prototype.setWatches = function() Node.prototype.setWatches = function()
@ -566,12 +554,12 @@ Node.prototype.setWatches = function()
if(time > 50) if(time > 50)
{ {
self.updateBlock(); self.getLatestBlock();
} }
else else
{ {
debounce(function() { debounce(function() {
self.updateBlock(); self.getLatestBlock();
}, 50); }, 50);
} }
}); });
@ -594,12 +582,12 @@ Node.prototype.setWatches = function()
if(time > 50) if(time > 50)
{ {
self.update(true); self.getStats(true);
} }
else else
{ {
debounce(function() { debounce(function() {
self.update(true); self.getStats(true);
}, 50); }, 50);
} }
}); });
@ -611,7 +599,7 @@ Node.prototype.setWatches = function()
} }
this.updateInterval = setInterval( function(){ this.updateInterval = setInterval( function(){
self.update(); self.getStats();
}, UPDATE_INTERVAL); }, UPDATE_INTERVAL);
this.pingInterval = setInterval( function(){ this.pingInterval = setInterval( function(){