<commit_before>#= require cable/connection_monitor

class Cable.Connection
  constructor: (@consumer) ->
    new Cable.ConnectionMonitor @consumer
    @open()

  send: (data) ->
    if @isOpen()
      @websocket.send(JSON.stringify(data))
      true
    else
      false

  open: ->
    @websocket = new WebSocket(@consumer.url)
    @websocket.onmessage = @onMessage
    @websocket.onopen    = @onOpen
    @websocket.onclose   = @onClose
    @websocket.onerror   = @onError
    @websocket

  close: ->
    @websocket.close() unless @isClosed()

  reopen: ->
    @close()
    @open()

  isOpen: ->
    @websocket.readyState is WebSocket.OPEN

  isClosed: ->
    @websocket.readyState in [ WebSocket.CLOSED, WebSocket.CLOSING ]

  onMessage: (message) =>
    data = JSON.parse message.data
    @consumer.subscribers.notify(data.identifier, "received", data.message)

  onOpen: =>
    @consumer.subscribers.reload()

  onClose: =>
    @disconnect()

  onError: =>
    @disconnect()
    @websocket.onclose = -> # no-op
    @websocket.onerror = -> # no-op
    try @close()

  disconnect: ->
    @consumer.subscribers.notifyAll("disconnected")
<commit_msg>Wait for connection to close before reopening it<commit_after>#= require cable/connection_monitor

class Cable.Connection
  constructor: (@consumer) ->
    new Cable.ConnectionMonitor @consumer
    @open()

  send: (data) ->
    if @isOpen()
      @websocket.send(JSON.stringify(data))
      true
    else
      false

  open: =>
    @websocket = new WebSocket(@consumer.url)
    @websocket.onmessage = @onMessage
    @websocket.onopen    = @onOpen
    @websocket.onclose   = @onClose
    @websocket.onerror   = @onError

  close: ->
    @websocket.close() unless @isClosed()

  reopen: ->
    if @isClosed()
      @open()
    else
      @websocket.onclose = @open
      @websocket.onerror = @open
      @websocket.close()

  isOpen: ->
    @websocket.readyState is WebSocket.OPEN

  isClosed: ->
    @websocket.readyState in [ WebSocket.CLOSED, WebSocket.CLOSING ]

  onMessage: (message) =>
    data = JSON.parse message.data
    @consumer.subscribers.notify(data.identifier, "received", data.message)

  onOpen: =>
    @consumer.subscribers.reload()

  onClose: =>
    @disconnect()

  onError: =>
    @disconnect()
    @websocket.onclose = -> # no-op
    @websocket.onerror = -> # no-op
    try @close()

  disconnect: ->
    @consumer.subscribers.notifyAll("disconnected")

{'fraction_non_alphanumeric': 0.13632619439868204, 'fraction_numerical': 0.0, 'mean_word_length': 2.8926282051282053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '5622095', 'n_tokens_mistral': 863, 'n_tokens_neox': 794, 'n_words': 153}