<commit_before>'use strict'
var bulkAddEventListener = function bulkAddEventListener (object, events, callback) {
  events.forEach(function (event) {
    object.addEventListener(event, function __idle (event) {
      callback(event)
    })
  })
}

var bulkRemoveEventListener = function bulkRemoveEventListener (object, events) {
  events.forEach(function (event) {
    object.removeEventListener(event, __idle)
  })
}

class IdleJs {
  constructor (options) {
    this.defaults = {
      idle: 10000, // idle time in ms
      events: ['mousemove', 'keydown', 'mousedown', 'touchstart'], // events that will trigger the idle resetter
      onIdle: function () {}, // callback function to be executed after idle time
      onActive: function () {}, // callback function to be executed after back form idleness
      onHide: function () {}, // callback function to be executed when window become hidden
      onShow: function () {}, // callback function to be executed when window become visible
      keepTracking: true, // set it to false of you want to track only once
      startAtIdle: false, // set it to true if you want to start in the idle state
      recurIdleCall: false
    }
    this.settings = Object.assign({}, this.defaults, options)
    this.visibilityEvents = ['visibilitychange', 'webkitvisibilitychange', 'mozvisibilitychange', 'msvisibilitychange']
    this.lastId = null

    this.reset()

    this.stopListener = (event) => {
      this.stop()
    }
  }

  resetTimeout (id, settings, keepTracking = this.settings.keepTracking) {
    if (this.idle) {
      this.idle = false
      settings.onActive.call()
    }
    if (id) {
      clearTimeout(id)
    }
    if (keepTracking) {
      return this.timeout(this.settings)
    }
  }

  timeout (settings) {
    var timer = (this.settings.recurIdleCall) ? setInterval : setTimeout
    var id
    id = timer(function () {
      this.idle = true
      this.settings.onIdle.call()
    }.bind(this), this.settings.idle)
    return id
  }

  start () {
    window.addEventListener('idle:stop', this.stopListener)
    this.lastId = this.timeout(this.settings)
    bulkAddEventListener(window, this.settings.events, function (event) {
      this.lastId = this.resetTimeout(this.lastId, this.settings)
    }.bind(this))
    if (this.settings.onShow || this.settings.onHide) {
      bulkAddEventListener(document, this.visibilityEvents, function (event) {
        if (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
          if (this.visible) {
            this.visible = false
            this.settings.onHide.call()
          }
        } else {
          if (!this.visible) {
            this.visible = true
            this.settings.onShow.call()
          }
        }
      }.bind(this))
    }
    return this
  }

  stop () {
    window.removeEventListener('idle:stop', this.stopListener)

    bulkRemoveEventListener(window, this.settings.events)
    this.lastId = this.resetTimeout(this.lastId, this.settings, false)

    if (this.settings.onShow || this.settings.onHide) {
      bulkRemoveEventListener(document, this.visibilityEvents)
    }
  }

  reset ({ idle = this.settings.startAtIdle, visible = !this.settings.startAtIdle } = {}) {
    this.idle = idle
    this.visible = visible
  }
}

module.exports = IdleJs
<commit_msg>Make stop() and reset() return this, same as start()<commit_after>'use strict'
var bulkAddEventListener = function bulkAddEventListener (object, events, callback) {
  events.forEach(function (event) {
    object.addEventListener(event, function __idle (event) {
      callback(event)
    })
  })
}

var bulkRemoveEventListener = function bulkRemoveEventListener (object, events) {
  events.forEach(function (event) {
    object.removeEventListener(event, __idle)
  })
}

class IdleJs {
  constructor (options) {
    this.defaults = {
      idle: 10000, // idle time in ms
      events: ['mousemove', 'keydown', 'mousedown', 'touchstart'], // events that will trigger the idle resetter
      onIdle: function () {}, // callback function to be executed after idle time
      onActive: function () {}, // callback function to be executed after back form idleness
      onHide: function () {}, // callback function to be executed when window become hidden
      onShow: function () {}, // callback function to be executed when window become visible
      keepTracking: true, // set it to false of you want to track only once
      startAtIdle: false, // set it to true if you want to start in the idle state
      recurIdleCall: false
    }
    this.settings = Object.assign({}, this.defaults, options)
    this.visibilityEvents = ['visibilitychange', 'webkitvisibilitychange', 'mozvisibilitychange', 'msvisibilitychange']
    this.lastId = null

    this.reset()

    this.stopListener = (event) => {
      this.stop()
    }
  }

  resetTimeout (id, settings, keepTracking = this.settings.keepTracking) {
    if (this.idle) {
      this.idle = false
      settings.onActive.call()
    }
    if (id) {
      clearTimeout(id)
    }
    if (keepTracking) {
      return this.timeout(this.settings)
    }
  }

  timeout (settings) {
    var timer = (this.settings.recurIdleCall) ? setInterval : setTimeout
    var id
    id = timer(function () {
      this.idle = true
      this.settings.onIdle.call()
    }.bind(this), this.settings.idle)
    return id
  }

  start () {
    window.addEventListener('idle:stop', this.stopListener)
    this.lastId = this.timeout(this.settings)
    bulkAddEventListener(window, this.settings.events, function (event) {
      this.lastId = this.resetTimeout(this.lastId, this.settings)
    }.bind(this))
    if (this.settings.onShow || this.settings.onHide) {
      bulkAddEventListener(document, this.visibilityEvents, function (event) {
        if (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
          if (this.visible) {
            this.visible = false
            this.settings.onHide.call()
          }
        } else {
          if (!this.visible) {
            this.visible = true
            this.settings.onShow.call()
          }
        }
      }.bind(this))
    }

    return this
  }

  stop () {
    window.removeEventListener('idle:stop', this.stopListener)

    bulkRemoveEventListener(window, this.settings.events)
    this.lastId = this.resetTimeout(this.lastId, this.settings, false)

    if (this.settings.onShow || this.settings.onHide) {
      bulkRemoveEventListener(document, this.visibilityEvents)
    }

    return this
  }

  reset ({ idle = this.settings.startAtIdle, visible = !this.settings.startAtIdle } = {}) {
    this.idle = idle
    this.visible = visible

    return this
  }
}

module.exports = IdleJs

{'fraction_non_alphanumeric': 0.1163652534835458, 'fraction_numerical': 0.0014823599169878447, 'mean_word_length': 2.8598398169336385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2912206', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1936, 'n_words': 484}