<commit_before>'use strict'

const ChromeRemoter = require('chrome-remote-interface')
const DevToolsNetwork = require('./devToolsNetwork')
const DevToolsInspector = require('./devToolsInspector')
const DevToolsHelper = require('./devToolsHelper')

/**
 * internals
 * To wrap private methods and properties
 *   when bound to a instantiated object of this class
 */
const internals = {
}

/**
 * Wrap the Chrome DevTools Remote Protocol.
 *
 * @see {@link https://chromedevtools.github.io/devtools-protocol/}
 * @class
 */
class RemoteDebugChrome extends DevToolsHelper {
  /**
  * @constructor
  */
  constructor () {
    super()

    this._chromeDevTools = null
    this._networkDevTools = null
    this._inspectorDevTools = null
  }

  connect (remoterOptions) {
    const self = this
    /**
     * initialize the chrome remote interface
     * Then this modules chrome devTools Domain handlers
    */
    return ChromeRemoter(remoterOptions)
      .then(function onInit (Chrome) {
        self._chromeDevTools = Chrome

        return internals.initialize.call(self)
      })
      .then(function initPassed (initVal) {
        return true
      })
      .catch(({ stack }) => {
        self.emitAsync('error', new Error(`ChromeRemoter catch: ${stack}`))
        self._chromeDevTools = null
        return false
      })
  }

  /**
   * @public
   * @method destroy - clean up
  */
  destroy () {
    this._chromeDevTools.close(function closed () {
      this.emitAsync('closed')
    }.bind(this))

    if (this._networkDevTools !== null) {
      this._networkDevTools.destroy()
    }

    if (this._inspectorDevTools !== null) {
      this._inspectorDevTools.destroy()
    }

    this._chromeDevTools = null
    this._networkDevTools = null
    this._inspectorDevTools = null
  }
}

/**
 * @private
 * @method initialize
*/
internals.initialize = async function () {
  const self = this
  this._chromeDevTools.on('disconnect', function () {
    self.emitAsync(`disconnected`)
  })

  this._chromeDevTools.on('ready', function () {
    self.emitAsync(`ready`)
  })

  this._chromeDevTools.on('connect', function () {
    self.emitAsync('connect')
  })

  this._chromeDevTools.on('error', function (err) {
    self.emitAsync('error', err)<commit_msg>on destroy() return a promise as well as legacy event emit<commit_after>'use strict'

const ChromeRemoter = require('chrome-remote-interface')
const DevToolsNetwork = require('./devToolsNetwork')
const DevToolsInspector = require('./devToolsInspector')
const DevToolsHelper = require('./devToolsHelper')

/**
 * internals
 * To wrap private methods and properties
 *   when bound to a instantiated object of this class
 */
const internals = {
}

/**
 * Wrap the Chrome DevTools Remote Protocol.
 *
 * @see {@link https://chromedevtools.github.io/devtools-protocol/}
 * @class
 */
class RemoteDebugChrome extends DevToolsHelper {
  /**
  * @constructor
  */
  constructor () {
    super()

    this._chromeDevTools = null
    this._networkDevTools = null
    this._inspectorDevTools = null
  }

  connect (remoterOptions) {
    const self = this
    /**
     * initialize the chrome remote interface
     * Then this modules chrome devTools Domain handlers
    */
    return ChromeRemoter(remoterOptions)
      .then(function onInit (Chrome) {
        self._chromeDevTools = Chrome

        return internals.initialize.call(self)
      })
      .then(function initPassed (initVal) {
        self.emitAsync('connected') // event based notification
        return true // promise based resolution
      })
      .catch(({ stack }) => {
        self.emitAsync('error', new Error(`ChromeRemoter catch: ${stack}`))
        self._chromeDevTools = null
        return false // promise based resolution
      })
  }

  /**
   * @public
   * @method destroy - clean up
  */
  destroy () {
    const self = this
    return new Promise((resolve, reject) => {
      if (self._networkDevTools !== null) {
        self._networkDevTools.destroy()
      }

      if (self._inspectorDevTools !== null) {
        self._inspectorDevTools.destroy()
      }

      self._chromeDevTools.close(function closed () {
        self.emitAsync('closed')  // event based notification
        resolve('destroyed')  // promise based resolution
      })

      self._chromeDevTools = null
      self._networkDevTools = null
      self._inspectorDevTools = null
    })
  }
}

/**
 * @private
 * @method initialize
*/
internals.initialize = function () {
  const self = this
  this._chromeDevTools.on('disconnect', function () {
    self.emitAsync(`disconnected`)
  })

  this._chromeDevTools.on('ready', function () {
    self.emitAsync(`ready`)
  })

  this._chromeDevTools.on('connect', function () {
    self.emitAsync('connect')
  })

  this._chromeDevTools.on('error', function (err) {
    self.emitAsync('error', err)
{'fraction_non_alphanumeric': 0.12432878975629905, 'fraction_numerical': 0.0, 'mean_word_length': 2.953469387755102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '1844557', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1370, 'n_words': 319}