<commit_before>'use strict';
const EventEmitter = require('events').EventEmitter;
const resolve = require('dns').resolve;
const lookup = require('dns').lookup;
const exec = require('child_process').exec;
const os = require('os');

/**
 * IsConnected check if connected to internet on regular intervals and emit events accordingly.
 */
class IsConnected extends EventEmitter {
    /**
     * constructs IsConnected
     * @param  {Integer} checkInterval check interval defaults to 10000 seconds ie 10 seconds
     * @param  {String} hostName      hostname of server to check internet using
     */
    constructor(checkInterval, hostName) {
            super();
            this.checkInterval = (checkInterval) ? checkInterval : 10000;
            this.state = false;
            this.hostName = (hostName) ? hostName : 'www.google.com';
        }
        /**
         * start Connectivity monitoring process
         * @param  {String} type type of check dns|ping
         */
    init(type) {
            switch (type) {
                case 'dns':
                    this._dns();
                    break;
                case 'ping':
                    this._ping();
                    break;
                default:
                    this._dns();
            }
        }
        /**
         * dns method to check internet connectivity emits 2 events connected|disconnected
         * used dns lookup instead of resolve for any OS other than linux as there was a
         * change in behaviour in windows. raised in issue #1
         */
    _dns() {
            if (os.platform() === 'linux') {
                setInterval(function() {
                    resolve(this.hostName, function(err) {
                        if (err) {
                            this.state = false;
                            this.emit('disconnected');
                        } else {
                            this.state = true;
                            this.emit('connected');
                        }
                    }.bind(this));
                }.bind(this), this.checkInterval);
            } else {
                setInterval(function() {
                    lookup(this.hostName, function(err) {
                        if (err) {
                            this.state = false;
                            this.emit('disconnected');
                        } else {
                            this.state = true;
                            this.emit('connected');
                        }
                    }.bind(this));
                }.bind(this), this.checkInterval);
            }

        }
        /**
         * ping method to check internet connectivity emits 2 events connected|disconnected
         */
    _ping() {
        let cmd = 'ping -c 1 :hostName'.replace(/:hostName/, this.hostName);
        setInterval(function() {
            exec(cmd, function ping(err) {
                if (err) {
                    this.state = false;
                    this.emit('disconnected');
                } else {
                    this.state = true;
                    this.emit('connected');
                }
            }.bind(this));
        }.bind(this), this.checkInterval);
    }
}

module.exports = IsConnected;<commit_msg>changed from setInterval to setTimeout to avoid overlapping checks<commit_after>'use strict';
const EventEmitter = require('events').EventEmitter;
const resolve = require('dns').resolve;
const lookup = require('dns').lookup;
const exec = require('child_process').exec;
const os = require('os');

/**
 * IsConnected check if connected to internet on regular intervals and emit events accordingly.
 */
class IsConnected extends EventEmitter {
    /**
     * constructs IsConnected
     * @param  {Integer} checkInterval check interval defaults to 10000 seconds ie 10 seconds
     * @param  {String} hostName      hostname of server to check internet using
     */
    constructor(checkInterval, hostName) {
            super();
            this.checkInterval = (checkInterval) ? checkInterval : 10000;
            this.state = false;
            this.hostName = (hostName) ? hostName : 'www.google.com';
        }
        /**
         * start Connectivity monitoring process
         * @param  {String} type type of check dns|ping
         */
    init(type) {
            switch (type) {
                case 'dns':
                    this._dns();
                    break;
                case 'ping':
                    this._ping();
                    break;
                default:
                    this._dns();
            }
        }
        /**
         * dns method to check internet connectivity emits 2 events connected|disconnected
         * used dns lookup instead of resolve for any OS other than linux as there was a
         * change in behaviour in windows. raised in issue #1
         */
    _dns() {
            if (os.platform() === 'linux') {
                setTimeout(function() {
                    resolve(this.hostName, function(err) {
                        if (err) {
                            this.state = false;
                            this.emit('disconnected');
                        } else {
                            this.state = true;
                            this.emit('connected');
                        }
                        process.nextTick(function() { this._dns(); }.bind(this));
                    }.bind(this));
                }.bind(this), this.checkInterval);
            } else {
                setTimeout(function() {
                    lookup(this.hostName, function(err) {
                        if (err) {
                            this.state = false;
                            this.emit('disconnected');
                        } else {
                            this.state = true;
                            this.emit('connected');
                        }
                        process.nextTick(function() { this._dns(); }.bind(this));
                    }.bind(this));
                }.bind(this), this.checkInterval);
            }

        }
        /**
         * ping method to check internet connectivity emits 2 events connected|disconnected
         */
    _ping() {
        let cmd = 'ping -c 1 :hostName'.replace(/:hostName/, this.hostName);
        setTimeout(function() {
            exec(cmd, function ping(err) {
                if (err) {
                    this.state = false;
                    this.emit('disconnected');
                } else {
                    this.state = true;
                    this.emit('connected');
                }
                process.nextTick(function() { this._ping(); }.bind(this));
            }.bind(this));
        }.bind(this), this.checkInterval);
    }
}

module.exports = IsConnected;
{'fraction_non_alphanumeric': 0.10421872703219168, 'fraction_numerical': 0.0047038071439071, 'mean_word_length': 1.2589641434262948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4760718', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1561, 'n_words': 418}