Sha256: ada071712b407e43ce09709a1d942addfb56b32807f6450aa39554ec1ce55a99

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

var portastic = require('./portastic');
var events = require('events');
var util = require('util');

var Monitor = function(ports, options) {
  this._ports = ports;
  this._options = options || {};
  this._watchers = [];

  if (this._options.autostart === false)
    return;

  this.start();
};
util.inherits(Monitor, events.EventEmitter);

Monitor.prototype.start = function() {
  if (this._watchers.length)
    return this.emit('error', new Error('Monitor already started'));

  var that = this;
  this._ports.forEach(function(port) {
    that._watchers.push(that._watcher(port));
  });
};

Monitor.prototype.stop = function() {
  this._watchers.forEach(function(watcher) {
    clearInterval(watcher.intervar);
  });

  this._watchers = [];
};

Monitor.prototype._watcher = function(port) {
  var that = this;
  var setup = {
    state: null,
    interval: setInterval(function() {
      portastic.test(port)
        .then(function(open) {
          if (setup.state === open)
            return;

          that.emit(open ? 'open' : 'close', port);
          setup.state = open;
        })
        .catch(function(err) {
          process.nextTick(function() {
            that.emit('error', err);
          });
        });
    }, that._options.interval || 100)
  };

  return setup;
};

module.exports = Monitor;

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
proxy_chain_rb-0.1.2 lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/lib/monitor.js
proxy_chain_rb-0.1.0 lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.3.1 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.3.0 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.2.9 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.2.8 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.2.7 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js
browsed-0.2.6 lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js