<commit_before>'use strict';

var util = require('util');
var async = require('async');
var request = require('request');
var bitcore = require('bitcore');
var io = require('socket.io-client');
var log = require('../../log');

var EventEmitter = require('events').EventEmitter;
var preconditions = require('preconditions').singleton();

/*
  This class lets interfaces with the blockchain, making general queries and
  subscribing to transactions on adressess and blocks.

  Opts: 
    - host
    - port
    - schema
    - reconnection (optional)
    - reconnectionDelay (optional)

  Events:
    - tx: activity on subscribed address.
    - block: a new block that includes a subscribed address.
    - connect: the connection with the blockchain is ready.
    - disconnect: the connection with the blochckain is unavailable.  
*/

var Insight = function(opts) {
  this.status = this.STATUS.DISCONNECTED;
  this.subscribed = {};
  this.listeningBlocks = false;

  preconditions.checkArgument(opts).shouldBeObject(opts)
    .checkArgument(opts.host)
    .checkArgument(opts.port)
    .checkArgument(opts.schema);

  this.url = opts.schema + '://' + opts.host + ':' + opts.port;
  this.opts = {
    'reconnection': opts.reconnection || true,
    'reconnectionDelay': opts.reconnectionDelay || 1000,
    'secure': opts.schema === 'https'
  };

}

util.inherits(Insight, EventEmitter);

Insight.prototype.STATUS = {
  CONNECTED: 'connected',
  DISCONNECTED: 'disconnected',
  DESTROYED: 'destroyed'
}

/** @private */
Insight.prototype.subscribeToBlocks = function() {
  var socket = this.getSocket();
  if (this.listeningBlocks || !socket.connected) return;

  var self = this;
  socket.emit('subscribe', 'inv');
  socket.on('block', function(blockHash) {
    self.emit('block', blockHash);
  });
  this.listeningBlocks = true;
}

/** @private */
Insight.prototype._getSocketIO = function(url, opts) {
  return io(this.url, this.opts);
};


Insight.prototype._setMainHandlers = function(url, opts) {
  // Emmit connection events
  var self = this;
  this.socket.on('connect', function() {
    self.status = self.STATUS.CONNECTED;
    self.subscribeToBlocks();
    self.emit('connect', 0);
  });

  this.socket.on('connect_error', function() {
    if (self.status != self.STATUS.CONNECTED) return;
    self.status = self.STATUS.DISCONNECTED;
    self.emit('disconnect');
  });

  this.socket.on('connect_timeout', function() {
    if (self.status != self.STATUS.CONNECTED) return;
    self.status = self.STATUS.DISCONNECTED;
    self.emit('disconnect');
  });

  this.socket.on('reconnect', function(attempt) {
    if (self.status != self.STATUS.DISCONNECTED) return;
    self.emit('reconnect', attempt);
    self.reSubscribe();
    self.status = self.STATUS.CONNECTED;
  });
};


/** @private */
Insight.prototype.getSocket = function(url, opts) {

  if (!this.socket) {<commit_msg>settings: Insight.js refactor<commit_after>'use strict';

var util = require('util');
var async = require('async');
var request = require('request');
var bitcore = require('bitcore');
var io = require('socket.io-client');
var log = require('../../log');

var EventEmitter = require('events').EventEmitter;
var preconditions = require('preconditions').singleton();

/*
  This class lets interfaces with the blockchain, making general queries and
  subscribing to transactions on adressess and blocks.

  Opts: 
    - host
    - port
    - schema
    - reconnection (optional)
    - reconnectionDelay (optional)

  Events:
    - tx: activity on subscribed address.
    - block: a new block that includes a subscribed address.
    - connect: the connection with the blockchain is ready.
    - disconnect: the connection with the blochckain is unavailable.  
*/

var Insight = function(opts) {
  this.status = this.STATUS.DISCONNECTED;
  this.subscribed = {};
  this.listeningBlocks = false;

  preconditions.checkArgument(opts).shouldBeObject(opts)
    .checkArgument(opts.host)
    .checkArgument(opts.port)
    .checkArgument(opts.schema);

  this.url = opts.schema + '://' + opts.host + ':' + opts.port;
  this.opts = {
    'reconnection': opts.reconnection || true,
    'reconnectionDelay': opts.reconnectionDelay || 1000,
    'secure': opts.schema === 'https'
  };

  this.socket = this.getSocket();
}

util.inherits(Insight, EventEmitter);

Insight.prototype.STATUS = {
  CONNECTED: 'connected',
  DISCONNECTED: 'disconnected',
  DESTROYED: 'destroyed'
}

/** @private */
Insight.prototype.subscribeToBlocks = function() {
  var socket = this.getSocket();
  if (this.listeningBlocks || !socket.connected) return;

  var self = this;
  socket.emit('subscribe', 'inv');
  socket.on('block', function(blockHash) {
    self.emit('block', blockHash);
  });
  this.listeningBlocks = true;
}

/** @private */
Insight.prototype._getSocketIO = function(url, opts) {
  return io(this.url, this.opts);
};


Insight.prototype._setMainHandlers = function(url, opts) {
  // Emmit connection events
  var self = this;
  this.socket.on('connect', function() {
    self.status = self.STATUS.CONNECTED;
    self.subscribeToBlocks();
    self.emit('connect', 0);
  });

  this.socket.on('connect_error', function() {
    if (self.status != self.STATUS.CONNECTED) return;
    self.status = self.STATUS.DISCONNECTED;
    self.emit('disconnect');
  });

  this.socket.on('connect_timeout', function() {
    if (self.status != self.STATUS.CONNECTED) return;
    self.status = self.STATUS.DISCONNECTED;
    self.emit('disconnect');
  });

  this.socket.on('reconnect', function(attempt) {
    if (self.status != self.STATUS.DISCONNECTED) return;
    self.emit('reconnect', attempt);
    self.reSubscribe();
    self.status = self.STATUS.CONNECTED;
  });
};


/** @private */
Insight.prototype.getSocket = function() {

  if (!this.socket) {
{'fraction_non_alphanumeric': 0.15128514749008107, 'fraction_numerical': 0.0017250301880282904, 'mean_word_length': 3.7996688741721854, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5085391', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1797, 'n_words': 397}