<commit_before>import { Location } from "./location"
import { defer } from "./util"

export interface HistoryDelegate {
  historyPoppedToLocationWithRestorationIdentifier(location: Location, restorationIdentifier: string): void
}

type HistoryMethod = (state: any, title: string, url?: string | null | undefined) => void

export class History {
  readonly delegate: HistoryDelegate
  started = false
  pageLoaded = false

  constructor(delegate: HistoryDelegate) {
    this.delegate = delegate
  }

  start() {
    if (!this.started) {
      addEventListener("popstate", this.onPopState, false)
      addEventListener("load", this.onPageLoad, false)
      this.started = true
    }
  }

  stop() {
    if (this.started) {
      removeEventListener("popstate", this.onPopState, false)
      removeEventListener("load", this.onPageLoad, false)
      this.started = false
    }
  }

  push(location: Location, restorationIdentifier: string) {
    this.update(history.pushState, location, restorationIdentifier)
  }

  replace(location: Location, restorationIdentifier: string) {
    this.update(history.replaceState, location, restorationIdentifier)
  }

  // Event handlers

  onPopState = (event: PopStateEvent) => {
    if (this.shouldHandlePopState()) {
      const { turbolinks } = event.state
      if (turbolinks) {
        const location = Location.currentLocation
        const { restorationIdentifier } = turbolinks
        this.delegate.historyPoppedToLocationWithRestorationIdentifier(location, restorationIdentifier)
      }
    }
  }

  onPageLoad = (event: Event) => {
    defer(() => {
      this.pageLoaded = true
    })
  }

  // Private<commit_msg>Handle onPopState when turbolinks is disabled (#523)<commit_after>import { Location } from "./location"
import { defer } from "./util"

export interface HistoryDelegate {
  historyPoppedToLocationWithRestorationIdentifier(location: Location, restorationIdentifier: string): void
}

type HistoryMethod = (state: any, title: string, url?: string | null | undefined) => void

export class History {
  readonly delegate: HistoryDelegate
  started = false
  pageLoaded = false

  constructor(delegate: HistoryDelegate) {
    this.delegate = delegate
  }

  start() {
    if (!this.started) {
      addEventListener("popstate", this.onPopState, false)
      addEventListener("load", this.onPageLoad, false)
      this.started = true
    }
  }

  stop() {
    if (this.started) {
      removeEventListener("popstate", this.onPopState, false)
      removeEventListener("load", this.onPageLoad, false)
      this.started = false
    }
  }

  push(location: Location, restorationIdentifier: string) {
    this.update(history.pushState, location, restorationIdentifier)
  }

  replace(location: Location, restorationIdentifier: string) {
    this.update(history.replaceState, location, restorationIdentifier)
  }

  // Event handlers

  onPopState = (event: PopStateEvent) => {
    if (!this.shouldHandlePopState()) return
    if (!event.state) return

    const { turbolinks } = event.state
    if (!turbolinks) return
    
    const location = Location.currentLocation
    const { restorationIdentifier } = turbolinks
    this.delegate.historyPoppedToLocationWithRestorationIdentifier(location, restorationIdentifier)
  }

  onPageLoad = (event: Event) => {
    defer(() => {
      this.pageLoaded = true
    })
  }

  // Private
{'fraction_non_alphanumeric': 0.10726950354609929, 'fraction_numerical': 0.0008865248226950354, 'mean_word_length': 3.681881051175657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6133644', 'n_tokens_mistral': 950, 'n_tokens_neox': 908, 'n_words': 218}