<commit_before># NewKite is a class for communicating with a remote Kite.
# It is analogous to RemoteKite class in go/koding/newkite/kite/remote.go file.
class NewKite extends KDObject

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

  constructor: (kite, authentication, options={})->
    super options

    @kite = kite
    @authentication = authentication

    @readyState = NOTREADY
    @autoReconnect = true

    @initBackoff options  if @autoReconnect

    @handlers =
      log   : (options)->
        log options.withArgs...
        options.responseCallback({withArgs: [{error: null, result: null}]})
      alert : (options)->
        alert options.withArgs...
        options.responseCallback({withArgs: [{error: null, result: null}]})

    @proto = new Bongo.dnodeProtocol.Session null, @handlers
    @proto.on 'request', (req)=>
      log "proto request", {req}
      @ready =>
        @ws.send JSON.stringify req
    @proto.on 'fail', (err)=>
      log "proto fail", {err}
    @proto.on 'error', (err)=>
      log "proto error", {err}
    @proto.on 'remoteError', (err)=>
      log "proto remoteEerror", {err}

  connect: ->
    addr = @kite.publicIP + ":" + @kite.port
    log "Trying to connect to #{addr}"
    @ws = new WebSocket "ws://#{addr}/dnode"
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  disconnect: (reconnect=true)->
    @autoReconnect = !!reconnect  if reconnect?
    @ws.close()

  # Call a method on the connected Kite.
  tell: (method, args, cb) ->
    if not Array.isArray(args)
      args = [args]

    options =
      authentication   : @authentication
      withArgs         : args
      responseCallback : cb

    # Normally the request is made with the following statement:
    #   @proto.request method, [options, cb]
    # However, we are making a single request/response call and
    # there is no point to hold the callback function because it
    # will never be called again. That's why we are deleting to
    # free the memory with the code below.
    scrubber = new Bongo.dnodeProtocol.Scrubber @proto.localStore
    scrubber.scrub [options], =><commit_msg>kite/newkite.coffee: fix optional arguments to method<commit_after># NewKite is a class for communicating with a remote Kite.
# It is analogous to RemoteKite class in go/koding/newkite/kite/remote.go file.
class NewKite extends KDObject

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

  constructor: (kite, authentication, options={})->
    super options

    @kite = kite
    @authentication = authentication

    @readyState = NOTREADY
    @autoReconnect = true

    @initBackoff options  if @autoReconnect

    @handlers =
      log   : (options)->
        log options.withArgs...
        options.responseCallback({withArgs: [{error: null, result: null}]})
      alert : (options)->
        alert options.withArgs...
        options.responseCallback({withArgs: [{error: null, result: null}]})

    @proto = new Bongo.dnodeProtocol.Session null, @handlers
    @proto.on 'request', (req)=>
      log "proto request", {req}
      @ready =>
        @ws.send JSON.stringify req
    @proto.on 'fail', (err)=>
      log "proto fail", {err}
    @proto.on 'error', (err)=>
      log "proto error", {err}
    @proto.on 'remoteError', (err)=>
      log "proto remoteEerror", {err}

  connect: ->
    addr = @kite.publicIP + ":" + @kite.port
    log "Trying to connect to #{addr}"
    @ws = new WebSocket "ws://#{addr}/dnode"
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  disconnect: (reconnect=true)->
    @autoReconnect = !!reconnect  if reconnect?
    @ws.close()

  # Call a method on the connected Kite.
  tell: (method, args, cb) ->
    # some methods doesn't need any argument(s), therefore you can omit args
    # completely. If you omit args, then you'll going to pass cb as the second
    # argument to the function, therefore we'll assign it back to cb and make
    # args empty.
    [args, cb] = [[], args] unless cb

    if not Array.isArray(args)
      args = [args]

    options =
      authentication   : @authentication
      withArgs         : args
      responseCallback : cb

    # Normally the request is made with the following statement:
    #   @proto.request method, [options, cb]
    # However, we are making a single request/response call and
    # there is no point to hold the callback function because it
    # will never be called again. That's why we are deleting to
    # free the memory with the code below.
    scrubber = new Bongo.dnodeProtocol.Scrubber @proto.localStore
    scrubber.scrub [options], =>
{'fraction_non_alphanumeric': 0.12058387983922149, 'fraction_numerical': 0.0012693039983075946, 'mean_word_length': 2.8129032258064517, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 27, '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': '1879105', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1454, 'n_words': 480}