Ethereum network status dashboard for PoW and PoA networks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ethstats-server/models/collection.js

71 lines
1.2 KiB

10 years ago
var _ = require('lodash');
var Node = require('./node');
var Collection = function Collection()
{
this._list = [];
return this;
}
Collection.prototype.add = function(data)
{
var node = this.getNodeOrNew({ id : data.id }, data);
node.setInfo(data);
10 years ago
return node.getInfo();
10 years ago
}
Collection.prototype.update = function(id, stats)
10 years ago
{
var node = this.getNode({ id: id });
if(!node)
return false;
node.stats = stats;
return node.getStats();
10 years ago
}
Collection.prototype.getIndex = function(search)
{
return _.findIndex(this._list, search);
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
if(index >= 0)
10 years ago
return this._list[index];
return false;
}
Collection.prototype.getNodeByIndex = function(index)
{
if(this._list[index])
return this._list[index];
return false;
}
Collection.prototype.getIndexOrNew = function(search, data)
{
var index = this.getIndex(search);
return (index >= 0 ? index : this._list.push(new Node(data)) - 1);
}
Collection.prototype.getNodeOrNew = function(search, data)
10 years ago
{
return this.getNodeByIndex(this.getIndexOrNew(search, data));
10 years ago
}
Collection.prototype.all = function()
10 years ago
{
return this._list;
10 years ago
}
module.exports = Collection;