<commit_before>var assign = require('object-assign');
var MyEthActions = require('../actions/MyEthActions');

function EthService(url, options) {
  this.options = assign({
    blocksLimit: 20
  }, options||{});

  this.web3 = require('web3');
  this.web3.setProvider(new this.web3.providers.HttpProvider(url));
}

EthService.prototype.getNumber = function() {
  return this.web3.eth.blockNumber;
};

EthService.prototype.getBlocks = function() {
  return this.blocks;
};

EthService.prototype.start = function() {
  var number = this.getNumber();

  this.blocks = [];
  for(var i=0; i < this.options.blocksLimit; i++) {
    this.blocks.push( this.web3.eth.getBlock(number-i, false) );
  }

  MyEthActions.setupBlocks(this.blocks);

  var callback = function() {
    var number = this.getNumber();
    for(var i=this.blocks[0].number+1; i <= number; i++) {
      this.blocks.unshift(this.web3.eth.getBlock(i, false))
      MyEthActions.newBlock(this.blocks[0]);
    }
  };
  this.watcher = window.setInterval(callback.bind(this), 2000);
};

EthService.prototype.stop = function() {
  window.clearInterval(this.watcher);
};

module.exports = EthService;<commit_msg>refactor EthService<commit_after>var assign = require('object-assign');
var MyEthActions = require('../actions/MyEthActions');

function EthService(url, options) {
  this.options = assign({
    blocksLimit: 20
  }, options||{});

  this.web3 = require('web3');
  this.web3.setProvider(new this.web3.providers.HttpProvider(url));

  this.watchers = {};
}

assign(EthService.prototype, {
  getNumber: function() {
    return this.web3.eth.blockNumber;
  },

  getBlocks: function() {
    return this.blocks;
  },

  getNetStats: function() {
    return {
      listening: this.web3.net.listening,
      peerCount: this.web3.net.peerCount
    };
  },

  start: function() {
    var number = this.getNumber();

    this.blocks = [];
    for(var i=0; i < this.options.blocksLimit; i++) {
      this.blocks.push( this.web3.eth.getBlock(number-i, false) );
    }

    MyEthActions.setupBlocks(this.blocks);
    //MyEthActions.setupNetStats(this.getNetStats());

    this.watchers['blocks'] = window.setInterval(this._onNewBlock.bind(this), 2000);
  },

  stop: function() {
    for(var name in this.watchers) {
      window.clearInterval(this.watchers[name]);
    }
  },

  _onNewBlock: function() {
    var number = this.getNumber();
    for(var i=this.blocks[0].number+1; i <= number; i++) {
      this.blocks.unshift(this.web3.eth.getBlock(i, false))
      MyEthActions.newBlock(this.blocks[0]);
    }
  }
});

module.exports = EthService;
{'fraction_non_alphanumeric': 0.16551193225558122, 'fraction_numerical': 0.013856812933025405, 'mean_word_length': 3.7513711151736744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3363067', 'n_tokens_mistral': 911, 'n_tokens_neox': 870, 'n_words': 139}