<commit_before># We get a clue that the browser might be offline from suspicious requests or
# the HTML5 offline api.  If we suspect it's offline, we make a request for any random path
# which (probably) doesn't exist.  If we get a response, we're still online, if not,
# we trigger an event and update our status.

extendNative = (to, from) ->
  for key of from::
    try
      val = from::[key]

      if not to[key]? and typeof val isnt 'function'
        to[key] = val
    catch e

Offline = {}

Offline.options = if window.Offline then window.Offline.options or {} else {}
defaultOptions =
  checks:
    xhr:
      url: ->
        # This can be any endpoint, even one that will 404.
        "/favicon.ico?_=#{ (new Date()).getTime() }"
      timeout: 5000
      type: 'HEAD'
    image:
      url: ->
        # This can be any image, this is the better option if your image is on a different domain, otherwise just use XHR
        "/favicon.ico?_=#{ (new Date()).getTime() }"

    active: 'xhr'

  checkOnLoad: false

  interceptRequests: true

  reconnect: true

  deDupBody: false

grab = (obj, key) ->
  cur = obj
  parts = key.split('.')
  for part, i in parts
    cur = cur[part]
    break if typeof cur isnt 'object'

  if i is parts.length - 1
    cur
  else
    undefined

Offline.getOption = (key) ->
  val = grab(Offline.options, key) ? grab(defaultOptions, key)

  if typeof val is 'function'
    val()
  else
    val

# These events are available in modern browsers, but they mean different things.
# In FF and IE they mean the user has explicitly entered "Offline Mode"
# In Chrome they mean that the internet connection was lost or restored
window.addEventListener? 'online', ->
  # The event fires slightly before the browser is ready to make a request
  setTimeout Offline.confirmUp, 100
, false

window.addEventListener? 'offline', ->
  Offline.confirmDown()
, false

Offline.state = 'up'

Offline.markUp = ->
  Offline.trigger 'confirmed-up'

  return if Offline.state is 'up'

  Offline.state = 'up'
  Offline.trigger 'up'

Offline.markDown = ->
  Offline.trigger 'confirmed-down'

  return if Offline.state is 'down'

  Offline.state = 'down'
  Offline.trigger 'down'

handlers = {}

Offline.on = (event, handler, ctx) ->
  events = event.split(' ')

  if events.length > 1
    Offline.on(e, handler, ctx) for e in events
  else
    handlers[event] ?= []
    handlers[event].push [ctx, handler]

Offline.off = (event, handler) ->
  return unless handlers[event]?

  if not handler
    handlers[event] = []
  else
    i = 0
    while i < handlers[event].length
      [ctx, _handler] = handlers[event][i]
      if _handler is handler
        handlers[event].splice i, 1
      else
        i++

Offline.trigger = (event) ->
  if handlers[event]?
    for [ctx, handler] in handlers[event]
      handler.call(ctx)

checkXHR = (xhr, onUp, onDown) ->
  checkStatus = ->
    if xhr.status and xhr.status < 12000<commit_msg>fixes issue that occurs when a previously registered event handler calls .off/.on<commit_after># We get a clue that the browser might be offline from suspicious requests or
# the HTML5 offline api.  If we suspect it's offline, we make a request for any random path
# which (probably) doesn't exist.  If we get a response, we're still online, if not,
# we trigger an event and update our status.

extendNative = (to, from) ->
  for key of from::
    try
      val = from::[key]

      if not to[key]? and typeof val isnt 'function'
        to[key] = val
    catch e

Offline = {}

Offline.options = if window.Offline then window.Offline.options or {} else {}
defaultOptions =
  checks:
    xhr:
      url: ->
        # This can be any endpoint, even one that will 404.
        "/favicon.ico?_=#{ (new Date()).getTime() }"
      timeout: 5000
      type: 'HEAD'
    image:
      url: ->
        # This can be any image, this is the better option if your image is on a different domain, otherwise just use XHR
        "/favicon.ico?_=#{ (new Date()).getTime() }"

    active: 'xhr'

  checkOnLoad: false

  interceptRequests: true

  reconnect: true

  deDupBody: false

grab = (obj, key) ->
  cur = obj
  parts = key.split('.')
  for part, i in parts
    cur = cur[part]
    break if typeof cur isnt 'object'

  if i is parts.length - 1
    cur
  else
    undefined

Offline.getOption = (key) ->
  val = grab(Offline.options, key) ? grab(defaultOptions, key)

  if typeof val is 'function'
    val()
  else
    val

# These events are available in modern browsers, but they mean different things.
# In FF and IE they mean the user has explicitly entered "Offline Mode"
# In Chrome they mean that the internet connection was lost or restored
window.addEventListener? 'online', ->
  # The event fires slightly before the browser is ready to make a request
  setTimeout Offline.confirmUp, 100
, false

window.addEventListener? 'offline', ->
  Offline.confirmDown()
, false

Offline.state = 'up'

Offline.markUp = ->
  Offline.trigger 'confirmed-up'

  return if Offline.state is 'up'

  Offline.state = 'up'
  Offline.trigger 'up'

Offline.markDown = ->
  Offline.trigger 'confirmed-down'

  return if Offline.state is 'down'

  Offline.state = 'down'
  Offline.trigger 'down'

handlers = {}

Offline.on = (event, handler, ctx) ->
  events = event.split(' ')

  if events.length > 1
    Offline.on(e, handler, ctx) for e in events
  else
    handlers[event] ?= []
    handlers[event].push [ctx, handler]

Offline.off = (event, handler) ->
  return unless handlers[event]?

  if not handler
    handlers[event] = []
  else
    i = 0
    while i < handlers[event].length
      [ctx, _handler] = handlers[event][i]
      if _handler is handler
        handlers[event].splice i, 1
      else
        i++

Offline.trigger = (event) ->
  if handlers[event]?
    # temporary cache the handlers since its possible that the called functions will modify the handlers array by calling off/on 
    eventHandlers = handlers[event][..]
    for [ctx, handler] in eventHandlers
      handler.call(ctx)

checkXHR = (xhr, onUp, onDown) ->
  checkStatus = ->
    if xhr.status and xhr.status < 12000
{'fraction_non_alphanumeric': 0.10664049721949624, 'fraction_numerical': 0.006542361792607131, 'mean_word_length': 2.894904458598726, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '1317875', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1906, 'n_words': 713}