<commit_before>ipc  = require 'ipc'
utf8      = require 'utf8'
{EventEmitter} = require 'events'
SingleSocket = require 'single-socket'

module.exports =
class Terminal extends EventEmitter
  constructor: (url) ->
    @url = url
    @connect()

  connect: () ->
    @waitForSocket = new Promise (resolve, reject) =>
      @socket = new SingleSocket @url,
        onopen: () =>
          console.log('open')
          @emit 'open'
          resolve()
        onmessage: (msg) =>
          console.log('message', msg)
          @emit 'message', utf8.decode(window.atob(msg))
        onclose: () =>
          console.log('close')
          @emit 'close'
        onerror: (e) ->
          console.log('error', e)
          @emit 'error', e
          reject(e)

  reset: () ->
    @socket.reset()

  send: (data) ->
    console.log('sending data over socket', data)
    @waitForSocket.then =>
      @socket.send(data)
<commit_msg>use new api interface for single socket in terminal<commit_after>ipc  = require 'ipc'
utf8      = require 'utf8'
{EventEmitter} = require 'events'
SingleSocket = require 'single-socket'

module.exports =
class Terminal extends EventEmitter
  constructor: (url) ->
    @url = url
    @connect()

  connect: () ->
    @waitForSocket = new Promise (resolve, reject) =>
      @socket = new SingleSocket @url

      @socket.on 'open', =>
        console.log('open')
        @emit 'open'
        resolve()

      @socket.on 'message', (msg) =>
        console.log('message', msg)
        @emit 'message', utf8.decode(window.atob(msg))

      @socket.on 'close', () =>
        console.log('close')
        @emit 'close'

      @socket.on 'error', (e) =>
        console.log('error', e)
        @emit 'error', e
        reject(e)

  reset: () ->
    @socket.reset()

  send: (data) ->
    console.log('sending data over socket', data)
    @waitForSocket.then =>
      @socket.send(data)

{'fraction_non_alphanumeric': 0.15388655462184875, 'fraction_numerical': 0.0031512605042016808, 'mean_word_length': 2.3130434782608695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6784288', 'n_tokens_mistral': 601, 'n_tokens_neox': 592, 'n_words': 146}