<commit_before>let timerInterval
let alertInterval
let secondsRemaining
let alertSeconds
let callback

let mobbers = []

let currentMobber = 0
let secondsPerTurn = 600
let millisecondsPerSecond = 1000

function reset() {
  secondsRemaining = secondsPerTurn
  callback('timerChange', secondsRemaining)
}

function getCurrentAndNextMobbers() {
  if (!mobbers.length) {
    return { current: null, next: null }
  }
  return {
    current: mobbers[currentMobber],
    next: mobbers[(currentMobber + 1) % mobbers.length]
  }
}

function startAlerts() {
  if (!alertInterval) {
    alertSeconds = 0
    alertInterval = setInterval(() => {
      alertSeconds++
      callback('alert', alertSeconds)
    }, millisecondsPerSecond)
  }
}

function stopAlerts() {
  if (alertInterval) {
    clearInterval(alertInterval)
    alertInterval = null
  }
  callback('stopAlerts')
}

function start() {
  if (!timerInterval) {
    timerInterval = setInterval(() => {
      secondsRemaining--
      callback('timerChange', secondsRemaining)
      if (secondsRemaining < 0) {
        pause()
        rotate()
        callback('turnEnded')
        startAlerts()
      }
    }, millisecondsPerSecond)
  }
  callback('started')
  stopAlerts()
}

function pause() {
  if (timerInterval) {
    clearInterval(timerInterval)
    timerInterval = null
  }
  callback('paused')
  stopAlerts()
}

function rotate() {
  reset()
  currentMobber = mobbers.length ? (currentMobber + 1) % mobbers.length : 0
  callback('rotated', getCurrentAndNextMobbers())
}

function initialize() {
  rotate()
  callback('turnEnded')
}

function publishConfig() {
  callback('configUpdated', {
    mobbers,
    secondsPerTurn
  })
  callback('rotated', getCurrentAndNextMobbers())
}

function addMobber(mobber) {
  mobbers.push(mobber)
  publishConfig()
  callback('rotated', getCurrentAndNextMobbers())
}

function removeMobber(mobber) {
  let removedMobberIndex = mobbers.findIndex(x => x.name === mobber.name)
  mobbers = mobbers.filter(m => m.name !== mobber.name)

  if (currentMobber === removedMobberIndex) {
    pause()
    reset()
    callback('turnEnded')
  }

  if (currentMobber >= mobbers.length) {
    currentMobber = 0
  }

  publishConfig()
  callback('rotated', getCurrentAndNextMobbers())
}

function setSecondsPerTurn(value) {
  secondsPerTurn = value
  publishConfig()
  reset()
}

function setTestingSpeed(value) {
  millisecondsPerSecond = value
}

function getState() {
  return {
    mobbers: mobbers,
    secondsPerTurn: secondsPerTurn
  }
}

function loadState(state) {
  if(state.mobbers) {
    for(var i=0;i<state.mobbers.length;i++){
      addMobber(state.mobbers[i])
    }    
  }
  
  setSecondsPerTurn(state.secondsPerTurn || secondsPerTurn)
}

module.exports = {
  setCallback(cb) {
    callback = cb
  },
  reset,
  start,
  pause,
  rotate,
  initialize,
  publishConfig,
  addMobber,
  removeMobber,
  setSecondsPerTurn,
  setTestingSpeed,
  getState,
  loadState
}
<commit_msg>Update timer-state to use new timer class<commit_after>const Timer = require('./state/timer')

let mainTimer
let alertsTimer
let callback

let mobbers = []

let currentMobber = 0
let secondsPerTurn = 600
let millisecondsPerSecond = 1000

function createTimers() {
  if (mainTimer) {
    mainTimer.pause()
  }
  mainTimer = new Timer({countDown: true, time: secondsPerTurn, rateMilliseconds: millisecondsPerSecond}, secondsRemaining => {
    callback('timerChange', secondsRemaining)
    if (secondsRemaining < 0) {
      pause()
      rotate()
      callback('turnEnded')
      startAlerts()
    }
  })

  if (alertsTimer) {
    alertsTimer.pause()
  }
  alertsTimer = new Timer({countDown: false, rateMilliseconds: millisecondsPerSecond}, alertSeconds => {
    callback('alert', alertSeconds)
  })
}

function reset() {
  mainTimer.reset(secondsPerTurn)
  callback('timerChange', secondsPerTurn)
}

function getCurrentAndNextMobbers() {
  if (!mobbers.length) {
    return { current: null, next: null }
  }
  return {
    current: mobbers[currentMobber],
    next: mobbers[(currentMobber + 1) % mobbers.length]
  }
}

function startAlerts() {
  alertsTimer.reset(0)
  alertsTimer.start()
}

function stopAlerts() {
  alertsTimer.pause()
  callback('stopAlerts')
}

function start() {
  mainTimer.start()
  callback('started')
  stopAlerts()
}

function pause() {
  mainTimer.pause()
  callback('paused')
  stopAlerts()
}

function rotate() {
  reset()
  currentMobber = mobbers.length ? (currentMobber + 1) % mobbers.length : 0
  callback('rotated', getCurrentAndNextMobbers())
}

function initialize() {
  rotate()
  callback('turnEnded')
}

function publishConfig() {
  callback('configUpdated', {
    mobbers,
    secondsPerTurn
  })
  callback('rotated', getCurrentAndNextMobbers())
}

function addMobber(mobber) {
  mobbers.push(mobber)
  publishConfig()
  callback('rotated', getCurrentAndNextMobbers())
}

function removeMobber(mobber) {
  let removedMobberIndex = mobbers.findIndex(x => x.name === mobber.name)
  mobbers = mobbers.filter(m => m.name !== mobber.name)

  if (currentMobber === removedMobberIndex) {
    pause()
    reset()
    callback('turnEnded')
  }

  if (currentMobber >= mobbers.length) {
    currentMobber = 0
  }

  publishConfig()
  callback('rotated', getCurrentAndNextMobbers())
}

function setSecondsPerTurn(value) {
  secondsPerTurn = value
  publishConfig()
  reset()
}

function setTestingSpeed(value) {
  millisecondsPerSecond = value
  createTimers()
}

function getState() {
  return {
    mobbers: mobbers,
    secondsPerTurn: secondsPerTurn
  }
}

function loadState(state) {
  if(state.mobbers) {
    for(var i=0;i<state.mobbers.length;i++){
      addMobber(state.mobbers[i])
    }
  }

  setSecondsPerTurn(state.secondsPerTurn || secondsPerTurn)
}

createTimers()

module.exports = {
  setCallback(cb) {
    callback = cb
  },
  reset,
  start,
  pause,
  rotate,
  initialize,
  publishConfig,
  addMobber,
  removeMobber,
  setSecondsPerTurn,
  setTestingSpeed,
  getState,
  loadState
}

{'fraction_non_alphanumeric': 0.11631587276467796, 'fraction_numerical': 0.004747586643456243, 'mean_word_length': 3.1253263707571803, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '2837110', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2026, 'n_words': 381}