<commit_before>/* global document */
/* global window */

import React from 'react'
import ReactDOM from 'react-dom'
import 'normalize.css'
import mitt from 'mitt'

import Init from './components/Init'
import Api from './api'
import { getTitle } from './common'
import './styles/main.pcss'

(function () {

  const renderApp = (state, emitter) => {

    document.title = getTitle(state.data)

    ReactDOM.render(
      <Init {...state} emitter={emitter} />,
      document.getElementById('root')
    )
  }

  document.addEventListener('DOMContentLoaded', () => {

    const api = new Api(window.__APP_INITIAL_STATE__.apiConfig)

    const emitter = mitt()
    // Log all emissions
    emitter.on('*', (type, e) => console.info(type, e))
    // Save data to the API
    emitter.on('save', data => api.save(data, response => {
      const state = window.__APP_INITIAL_STATE__
      state.data = response
      renderApp(state, emitter)
    }))
    // Read data from the API
    emitter.on('load', url => {
      const parser = document.createElement('a')
      parser.href = url
      window.location.hash = parser.hash
      window.history.pushState(null, null, url)
      window.dispatchEvent(new window.PopStateEvent('popstate'))
    })
    emitter.on('getOptions', ({term, types, callback}) => api.find(term, types, callback))

    let currentPathname = window.location.pathname + window.location.search
    window.addEventListener('popstate', () => {
      const url = window.location.pathname + window.location.search
      if (currentPathname === url) {
        return
      } else {
        currentPathname = url
      }
      api.load(url, response => {
        const state = window.__APP_INITIAL_STATE__
        state.data = response
        renderApp(state, emitter)
      })
    })

    renderApp(window.__APP_INITIAL_STATE__, emitter)

  })


})()
<commit_msg>Prevent triggering unnecessary popstate events<commit_after>/* global document */
/* global window */

import React from 'react'
import ReactDOM from 'react-dom'
import 'normalize.css'
import mitt from 'mitt'

import Init from './components/Init'
import Api from './api'
import { getTitle } from './common'
import './styles/main.pcss'

(function () {

  const renderApp = (state, emitter) => {

    document.title = getTitle(state.data)

    ReactDOM.render(
      <Init {...state} emitter={emitter} />,
      document.getElementById('root')
    )
  }

  document.addEventListener('DOMContentLoaded', () => {

    const api = new Api(window.__APP_INITIAL_STATE__.apiConfig)

    const emitter = mitt()
    // Log all emissions
    emitter.on('*', (type, e) => console.info(type, e))
    // Save data to the API
    emitter.on('save', data => api.save(data, response => {
      const state = window.__APP_INITIAL_STATE__
      state.data = response
      renderApp(state, emitter)
    }))
    // Read data from the API
    emitter.on('load', url => {
      if (window.location.pathname + window.location.search !== url) {
        const parser = document.createElement('a')
        parser.href = url
        window.location.hash = parser.hash
        window.history.pushState(null, null, url)
        window.dispatchEvent(new window.PopStateEvent('popstate'))
      }
    })
    emitter.on('getOptions', ({term, types, callback}) => api.find(term, types, callback))

    window.addEventListener('popstate', () => {
      const url = window.location.pathname + window.location.search
      api.load(url, response => {
        const state = window.__APP_INITIAL_STATE__
        state.data = response
        renderApp(state, emitter)
      })
    })

    renderApp(window.__APP_INITIAL_STATE__, emitter)

  })


})()

{'fraction_non_alphanumeric': 0.1378561736770692, 'fraction_numerical': 0.0, 'mean_word_length': 2.9129511677282376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '2702567', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1067, 'n_words': 257}