<commit_before>import { activateTab } from '../libs'
import actions from '../libs/actions'

export default class TabHistory {
  constructor (background) {
    this.root = background
    const { onActivated, onFocusChanged, onRemoved } = this
    chrome.tabs.onActivated.addListener(onActivated)
    chrome.tabs.onRemoved.addListener(onRemoved)
    chrome.windows.onFocusChanged.addListener(onFocusChanged)
    this.actionMap = {
      [actions.lastActiveTab()]: this.activateTab
    }
  }

  tabHistory = []
  count = 0
  resetCountHandler = null

  resetCount = () => {
    if (this.resetCountHandler != null) {
      clearTimeout(this.resetCountHandler)
    }
    this.resetCountHandler = setTimeout(
      () => {
        const { length } = this.tabHistory
        const index = Math.max(length - 1 - this.count, 0)
        const untouchedTabs = this.tabHistory.slice(0, index)
        const touchedTabs = this.tabHistory.slice(index, length - 1).reverse()
        const lastTab = this.tabHistory[length - 1]
        this.tabHistory = [ ...untouchedTabs, ...touchedTabs, lastTab ]
        this.count = 0
      },
      1000
    )
  }

  add = ({ tabId, windowId, title }) => {
    this.remove(tabId)
    this.tabHistory.push({ tabId, windowId, title })
  }

  remove = (tabId) => {
    const index = this.tabHistory.findIndex(x => x.tabId === tabId)
    if (index !== -1) {
      this.tabHistory.splice(index, 1)
    }
  }

  activateTab = () => {
    this.count += 1
    this.resetCount()
    const { length } = this.tabHistory
    if (length > 1) {
      let index = length - 1 - this.count
      if (index < 0) {
        index += length
      }
      activateTab(this.tabHistory[index].tabId)
    }
  }

  onActivated = async (activeInfo) => {
    const { tabId, windowId } = activeInfo
    const tab = await chrome.tabs.get(tabId)
    this.add({ tabId, windowId, title: tab.title })
  }

  onFocusChanged = async (windowId) => {<commit_msg>Fix bug in last active tab<commit_after>import { activateTab } from '../libs'
import actions from '../libs/actions'

export default class TabHistory {
  constructor (background) {
    this.root = background
    const { onActivated, onFocusChanged, onRemoved } = this
    chrome.tabs.onActivated.addListener(onActivated)
    chrome.tabs.onRemoved.addListener(onRemoved)
    chrome.windows.onFocusChanged.addListener(onFocusChanged)
    this.actionMap = {
      [actions.lastActiveTab()]: this.activateTab
    }
  }

  tabHistory = []
  count = 0
  resetCountHandler = null

  resetCount = () => {
    if (this.resetCountHandler != null) {
      clearTimeout(this.resetCountHandler)
    }
    this.resetCountHandler = setTimeout(
      () => {
        const { length } = this.tabHistory
        const index = Math.max(length - 1 - this.count, 0)
        const untouchedTabs = this.tabHistory.slice(0, index)
        const touchedTabs = this.tabHistory.slice(index, length - 1).reverse()
        const lastTab = this.tabHistory[length - 1]
        this.tabHistory = [ ...untouchedTabs, ...touchedTabs, lastTab ]
        this.count = 0
      },
      1000
    )
  }

  add = ({ tabId, windowId, title }) => {
    if (!tabId || windowId === -1) {
      return
    }
    this.remove(tabId)
    this.tabHistory.push({ tabId, windowId, title })
  }

  remove = (tabId) => {
    const index = this.tabHistory.findIndex(x => x.tabId === tabId)
    if (index !== -1) {
      this.tabHistory.splice(index, 1)
    }
  }

  activateTab = () => {
    this.count += 1
    this.resetCount()
    const { length } = this.tabHistory
    if (length > 1) {
      const index = length - 1 - this.count
      if (index < 0) {
        this.count = 0
        const tabs = this.tabHistory.slice(0, length - 1).reverse()
        this.tabHistory = [ ...tabs, this.tabHistory[length - 1] ]
        return this.activateTab()
      }
      activateTab(this.tabHistory[index].tabId)
    }
  }

  onActivated = async (activeInfo) => {
    const { tabId, windowId } = activeInfo
    const tab = await chrome.tabs.get(tabId)
    this.add({ tabId, windowId, title: tab.title })
  }

  onFocusChanged = async (windowId) => {
{'fraction_non_alphanumeric': 0.13307524800387127, 'fraction_numerical': 0.009436244858456327, 'mean_word_length': 2.5182978723404257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '102933', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1306, 'n_words': 279}