<commit_before>const io = require('socket.io-client')
const Emitter = require('component-emitter')
const parseUrl = require('./parseUrl')

const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING'
const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING'
const ASSEMBLY_COMPLETED = 'ASSEMBLY_COMPLETED'

const statusOrder = [
  ASSEMBLY_UPLOADING,
  ASSEMBLY_EXECUTING,
  ASSEMBLY_COMPLETED
]

/**
 * Check that an assembly status is equal to or larger than some desired status.
 * It checks for things that are larger so that a comparison like this works,
 * when the old assembly status is UPLOADING but the new is FINISHED:
 *
 * !isStatus(oldStatus, ASSEMBLY_EXECUTING) && isStatus(newState, ASSEMBLY_EXECUTING)
 *
 * …so that we can emit the 'executing' event even if the execution step was so
 * fast that we missed it.
 */
function isStatus (status, test) {
  return statusOrder.indexOf(status) >= statusOrder.indexOf(test)
}

class TransloaditAssembly extends Emitter {
  constructor (assembly) {
    super()

    // The current assembly status.
    this.status = assembly
    // The socket.io connection.
    this.socket = null
    // The interval timer for full status updates.
    this.pollInterval = null
    // Whether this assembly has been closed (finished or errored)
    this.closed = false
  }

  connect () {
    this._connectSocket()
    this._beginPolling()
  }

  _onFinished () {
    this.emit('finished')
    this.close()
  }

  _connectSocket () {
    const parsed = parseUrl(this.status.websocket_url)
    const socket = io.connect(parsed.origin, {
      path: parsed.pathname
    })

    socket.on('connect', () => {
      socket.emit('assembly_connect', {
        id: this.status.assembly_id
      })

      this.emit('connect')
    })
    socket.on('error', () => {
      socket.disconnect()
      this.socket = null<commit_msg>transloadit: Lazy load socket.io-client. (#1019)<commit_after>const io = requireSocketIo
const Emitter = require('component-emitter')
const parseUrl = require('./parseUrl')

// Lazy load socket.io to avoid a console error
// in IE 10 when the Transloadit plugin is not used.
// (The console.error call comes from `buffer`. I
// think we actually don't use that part of socket.io
// at all…)
let socketIo
function requireSocketIo () {
  if (!socketIo) {
    socketIo = require('socket.io-client')
  }
  return socketIo
}

const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING'
const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING'
const ASSEMBLY_COMPLETED = 'ASSEMBLY_COMPLETED'

const statusOrder = [
  ASSEMBLY_UPLOADING,
  ASSEMBLY_EXECUTING,
  ASSEMBLY_COMPLETED
]

/**
 * Check that an assembly status is equal to or larger than some desired status.
 * It checks for things that are larger so that a comparison like this works,
 * when the old assembly status is UPLOADING but the new is FINISHED:
 *
 * !isStatus(oldStatus, ASSEMBLY_EXECUTING) && isStatus(newState, ASSEMBLY_EXECUTING)
 *
 * …so that we can emit the 'executing' event even if the execution step was so
 * fast that we missed it.
 */
function isStatus (status, test) {
  return statusOrder.indexOf(status) >= statusOrder.indexOf(test)
}

class TransloaditAssembly extends Emitter {
  constructor (assembly) {
    super()

    // The current assembly status.
    this.status = assembly
    // The socket.io connection.
    this.socket = null
    // The interval timer for full status updates.
    this.pollInterval = null
    // Whether this assembly has been closed (finished or errored)
    this.closed = false
  }

  connect () {
    this._connectSocket()
    this._beginPolling()
  }

  _onFinished () {
    this.emit('finished')
    this.close()
  }

  _connectSocket () {
    const parsed = parseUrl(this.status.websocket_url)
    const socket = io().connect(parsed.origin, {
      path: parsed.pathname
    })

    socket.on('connect', () => {
      socket.emit('assembly_connect', {
        id: this.status.assembly_id
      })

      this.emit('connect')
    })
    socket.on('error', () => {
      socket.disconnect()
      this.socket = null
{'fraction_non_alphanumeric': 0.10430054374691053, 'fraction_numerical': 0.0014829461196243204, 'mean_word_length': 3.3846153846153846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '965627', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1269, 'n_words': 361}