<commit_before>### define
libs/request : request
###

class SimpleArrayBufferSocket
  
  fallbackMode : false
  
  constructor : (options) ->

    _.extend(@, options)
    @sender = @defaultSender
    @sender.open(@)
  

  switchToFallback : ->

    unless @fallbackMode
      @sender.close()
      @fallbackMode = true
      @sender = @fallbackSender
      @sender.open(@)


  send : (data) ->

    deferred = $.Deferred()

    resolver = (result) ->
      deferred.resolve(result)

    @sender.send(data)
      .done(resolver)
      .fail( =>
        unless @fallbackMode
          @switchToFallback()
          @send(data).done(resolver)
        else
          deferred.reject()
      )

    deferred.promise()


class SimpleArrayBufferSocket.WebSocket

  OPEN_TIMEOUT : 5000
  MESSAGE_TIMEOUT : 20000

  pendingRequests : []

  constructor : (@url) ->
    _window = window ? self
    @WebSocketImpl = if _window.MozWebSocket then _window.MozWebSocket else _window.WebSocket


  open : ({ @responseBufferType, @requestBufferType }) ->
    
    @initialize()


  initialize : ->

    unless @socket and @openDeferred

      socket = @socket = new @WebSocketImpl(@url)
      openDeferred = @openDeferred = $.Deferred()

      socket.binaryType = 'arraybuffer'
      
      socket.onopen = -> openDeferred.resolve()
      
      socket.onerror = (err) ->
        console.error("socket error", err)
     
      socket.addEventListener(
        "close" 
        (code, reason) => 

          @socket = null
          
          request.reject("socket closed") for request in @pendingRequests
          @pendingRequests.length = 0
          
          console?.error("socket closed", "#{code}: #{reason}")

        false
      )

      socket.addEventListener(
        "message"
        (event) =>
          
          buffer = event.data
          handle = new Float32Array(buffer, 0, 1)[0]
          
          for request in @pendingRequests when request.handle == handle
            
            _.removeElement(@pendingRequests, request)

            if buffer.byteLength > 4
              request.resolve(new @responseBufferType(buffer, 4))
            else
              request.resolve([])
            
            break
        false
      )
      
      setTimeout(
        => 
          if not @socket or @socket.readyState != @WebSocketImpl.OPEN
            openDeferred.reject("timeout")
        @OPEN_TIMEOUT
      )
    
    @openDeferred.promise()


  close : ->
    if @socket
      @socket.close()
      @socket = null
      @openDeferred = null


  send : (data) ->

    @initialize().pipe =>
    
      { transmitBuffer, socketHandle } = @createPackage(data)

      deferred = $.Deferred()
      deferred.handle = socketHandle
      
      @pendingRequests.push(deferred)
      @socket.send(transmitBuffer.buffer)
    
      setTimeout(
        => 
          _.removeElement(@pendingRequests, deferred)
          deferred.reject("timeout")
        @MESSAGE_TIMEOUT
      )

      deferred.promise()


  createPackage : (data) ->

    transmitBuffer    = new @requestBufferType(1 + data.length)
    transmitBuffer[0] = Math.random()
    transmitBuffer.set(data, 1)
    socketHandle      = transmitBuffer[0]

    { transmitBuffer, socketHandle }


class SimpleArrayBufferSocket.XmlHttpRequest

  constructor : (@url) ->

  open : ({ @responseBufferType, @requestBufferType }) ->

  send : (data) ->
    data = new @requestBufferType(data) if _.isArray(data)
    request(
      data : data.buffer
      url : @url
      responseType : 'arraybuffer'
    ).pipe (buffer) => 
    
      if buffer<commit_msg>OX-120 hot fix to get ajax reloading working<commit_after>### define
libs/request : Request
###

class SimpleArrayBufferSocket
  
  fallbackMode : false
  
  constructor : (options) ->

    _.extend(@, options)
    @sender = @defaultSender
    @sender.open(@)
  

  switchToFallback : ->

    unless @fallbackMode
      @sender.close()
      @fallbackMode = true
      @sender = @fallbackSender
      @sender.open(@)


  send : (data) ->

    deferred = $.Deferred()

    resolver = (result) ->
      deferred.resolve(result)

    @sender.send(data)
      .done(resolver)
      .fail( =>
        unless @fallbackMode
          @switchToFallback()
          @send(data).done(resolver)
        else
          deferred.reject()
      )

    deferred.promise()


class SimpleArrayBufferSocket.WebSocket

  OPEN_TIMEOUT : 5000
  MESSAGE_TIMEOUT : 20000

  pendingRequests : []

  constructor : (@url) ->
    _window = window ? self
    @WebSocketImpl = if _window.MozWebSocket then _window.MozWebSocket else _window.WebSocket


  open : ({ @responseBufferType, @requestBufferType }) ->
    
    @initialize()


  initialize : ->

    unless @socket and @openDeferred

      socket = @socket = new @WebSocketImpl(@url)
      openDeferred = @openDeferred = $.Deferred()

      socket.binaryType = 'arraybuffer'
      
      socket.onopen = -> openDeferred.resolve()
      
      socket.onerror = (err) ->
        console.error("socket error", err)
     
      socket.addEventListener(
        "close" 
        (code, reason) => 

          @socket = null
          
          request.reject("socket closed") for request in @pendingRequests
          @pendingRequests.length = 0
          
          console?.error("socket closed", "#{code}: #{reason}")

        false
      )

      socket.addEventListener(
        "message"
        (event) =>
          
          buffer = event.data
          handle = new Float32Array(buffer, 0, 1)[0]
          
          for request in @pendingRequests when request.handle == handle
            
            _.removeElement(@pendingRequests, request)

            if buffer.byteLength > 4
              request.resolve(new @responseBufferType(buffer, 4))
            else
              request.resolve([])
            
            break
        false
      )
      
      setTimeout(
        => 
          if not @socket or @socket.readyState != @WebSocketImpl.OPEN
            openDeferred.reject("timeout")
        @OPEN_TIMEOUT
      )
    
    @openDeferred.promise()


  close : ->
    if @socket
      @socket.close()
      @socket = null
      @openDeferred = null


  send : (data) ->

    @initialize().pipe =>
    
      { transmitBuffer, socketHandle } = @createPackage(data)

      deferred = $.Deferred()
      deferred.handle = socketHandle
      
      @pendingRequests.push(deferred)
      @socket.send(transmitBuffer.buffer)
    
      setTimeout(
        => 
          _.removeElement(@pendingRequests, deferred)
          deferred.reject("timeout")
        @MESSAGE_TIMEOUT
      )

      deferred.promise()


  createPackage : (data) ->

    transmitBuffer    = new @requestBufferType(1 + data.length)
    transmitBuffer[0] = Math.random()
    transmitBuffer.set(data, 1)
    socketHandle      = transmitBuffer[0]

    { transmitBuffer, socketHandle }


class SimpleArrayBufferSocket.XmlHttpRequest

  constructor : (@url) ->

  open : ({ @responseBufferType, @requestBufferType }) ->

  send : (data) ->
    data = new @requestBufferType(data) if _.isArray(data)
    Request(
      data : data.buffer
      url : @url
      responseType : 'arraybuffer'
    ).pipe (buffer) => 
    
      if buffer
{'fraction_non_alphanumeric': 0.1104691154216536, 'fraction_numerical': 0.006190672719768881, 'mean_word_length': 1.9954676555418212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '5998804', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2040, 'n_words': 422}