<commit_before>class Kontrol extends KDEventEmitter

  bound: Bongo.bound

  [NOTREADY, READY, CLOSED] = [0,1,3]

  constructor: (options)->
    super
    @readyState = NOTREADY
    @addr = "ws://127.0.0.1:4000/_moh_/pub" #kontrol addr
    @connect()

  connect:->
    @ws = new WebSocket @addr
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  onOpen:->
    log "I'm connected to #{@addr}. Yayyy!"
    @readyState = READY
    @emit 'ready'
    @subscribe()

  onClose: (evt) ->
    log "Disconnected from #{@addr}, trying to reconnect"
    @readyState = CLOSED

  onMessage: (evt) ->
    @blobToString evt.data, (msg) =>
      try
        args = JSON.parse msg
      catch e
        log "json parse error: ", e, msg

      log {args}

      switch args.action
        when "AddKite"
          KD.utils.wait 1000, => @emit "KiteConnected", args

          kiteController = KD.getSingleton("kiteController")
          correlationName = "local-#{KD.nick()}"
          key = kiteController.getKiteKey args.kitename, correlationName
          kite = new NewKite {addr: args.addr, kiteName: args.kitename, token: args.token}
          log "ADDING KITE #{args.kitename} with key #{key}"
          kiteController.kiteInstances[key] = kite
        when "RemoveKite"
          KD.utils.wait 1000, => @emit "KiteDisconnected", args
          kiteController = KD.getSingleton("kiteController")
          correlationName = "local-#{KD.nick()}"
          key = kiteController.getKiteKey args.kitename, correlationName
          log "REMOVING KITE #{args.kitename} with key #{key}"
          delete kiteController.kiteInstances[key]

  onError: (evt) ->
    log "kontrol: error #{evt.data}"

  ready: (callback)->
    return KD.utils.defer callback  if @readyState
    @once 'ready', callback

  subscribe: ->
    log "subscribing"
    @send
      name: "subscribe"
      args:
        key: "kite.start.#{KD.nick()}"

  send: (data) ->
    log "sending", {data}
    @ready =>
      try @ws.send JSON.stringify data
      catch e then log e

  blobToString: (blob, callback) ->
    reader = new FileReader()
    reader.readAsText(blob)
    reader.onloadend = ->
      callback reader.result


<commit_msg>kite/kontrol.coffee: refactor addKite and removeKite into own functions<commit_after>class Kontrol extends KDEventEmitter

  bound: Bongo.bound

  [NOTREADY, READY, CLOSED] = [0,1,3]

  constructor: (options)->
    super
    @readyState = NOTREADY
    @addr = "ws://127.0.0.1:4000/_moh_/pub" #kontrol addr
    @connect()

  connect:->
    @ws = new WebSocket @addr
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  onOpen:->
    log "I'm connected to #{@addr}. Yayyy!"
    @readyState = READY
    @emit 'ready'
    @subscribe()

  onClose: (evt) ->
    log "Disconnected from #{@addr}, trying to reconnect"
    @readyState = CLOSED

  onMessage: (evt) ->
    @blobToString evt.data, (msg) =>
      try
        args = JSON.parse msg
      catch e
        log "json parse error: ", e, msg

      log {args}

      switch args.action
        when "AddKite"
          @addKite args
          KD.utils.wait 1000, => @emit "KiteConnected", args
        when "RemoveKite"
          @removeKite args
          KD.utils.wait 1000, => @emit "KiteDisconnected", args

  addKite: (kite) ->
    kc = KD.getSingleton("kiteController")
    correlationName = "local-#{KD.nick()}"
    key = kc.getKiteKey kite.kitename, correlationName
    kiteInstance = new NewKite {addr: kite.addr, kiteName: kite.kitename, token: kite.token}

    log "ADDING KITE #{kite.kitename} with key #{key}"
    kc.kiteInstances[key] = kiteInstance

  removeKite: (kite) ->
    kc = KD.getSingleton("kiteController")
    correlationName = "local-#{KD.nick()}"
    key = kc.getKiteKey kite.kitename, correlationName

    log "REMOVING KITE #{kite.kitename} with key #{key}"
    delete kc.kiteInstances[key]



  onError: (evt) ->
    log "kontrol: error #{evt.data}"

  ready: (callback)->
    return KD.utils.defer callback  if @readyState
    @once 'ready', callback

  subscribe: ->
    log "subscribing"
    @send
      name: "subscribe"
      args:
        key: "kite.start.#{KD.nick()}"

  send: (data) ->
    log "sending", {data}
    @ready =>
      try @ws.send JSON.stringify data
      catch e then log e

  blobToString: (blob, callback) ->
    reader = new FileReader()
    reader.readAsText(blob)
    reader.onloadend = ->
      callback reader.result



{'fraction_non_alphanumeric': 0.13308807617398832, 'fraction_numerical': 0.009088941787491886, 'mean_word_length': 2.5581216320246343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2226943', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1557, 'n_words': 391}