<commit_before>function _attach(tabId) {
  var protocolVersion = '1.1';

  return new Promise((resolve, reject) => {
    chrome.debugger.attach({
      tabId: tabId
    }, protocolVersion, () => {
      if (chrome.runtime.lastError) {
        reject(chrome.runtime.lastError.message);
        return;
      }

      resolve();
    });
  });
}

function _sendCommand(tabId, command, data = {}) {
  return new Promise((resolve, reject) => {
    chrome.debugger.sendCommand({
      tabId: tabId
    }, command, data, (response) => {
      console.log('command', command, tabId, data);
      if (response.error) {
        reject(response.error);
        return;
      }

      resolve();
    });
  });
}

class TabDebugger {
  constructor(tabId) {
    this._tabId = tabId;
  }

  connect() {
    return _attach(this._tabId);
  }

  sendCommand(command, data) {
    return _sendCommand(this._tabId, command, data);
  }
}

export default TabDebugger;<commit_msg>Reconnect tab debugger when it gets detached.<commit_after>function _attach(tabId) {
  var protocolVersion = '1.1';

  return new Promise((resolve, reject) => {
    chrome.debugger.attach({
      tabId: tabId
    }, protocolVersion, () => {
      if (chrome.runtime.lastError) {
        reject(chrome.runtime.lastError.message);
        return;
      }

      resolve();
    });
  });
}

function _sendCommand(tabId, command, data = {}) {
  return new Promise((resolve, reject) => {
    chrome.debugger.sendCommand({
      tabId: tabId
    }, command, data, (response) => {
      console.log('command', command, tabId, data);
      if (response.error) {
        reject(response.error);
        return;
      }

      resolve();
    });
  });
}

class TabDebugger {
  constructor(tabId) {
    var tabDebugger = this;
    this._tabId = tabId;
    this._attached = true;

    chrome.debugger.onDetach.addListener((source, reason) => {
      if(source.tabId === tabDebugger._tabId) {
        tabDebugger._attached = false;
      }
    });
  }

  connect() {
    var tabDebugger = this;

    return _attach(this._tabId).then(() => {
      tabDebugger._attached = true;
    });
  }

  sendCommand(command, data) {
    var tabDebugger = this;

    if(!this._attached) {
      //TODO all settings (width/height/userAgent/etc.) are cleared when we reconnect - fix that
      return this.connect().then(() => {
        return _sendCommand(tabDebugger._tabId, command, data);
      });
    }

    return _sendCommand(this._tabId, command, data);
  }
}

export default TabDebugger;
{'fraction_non_alphanumeric': 0.14376237623762375, 'fraction_numerical': 0.0015841584158415843, 'mean_word_length': 2.6189111747851004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '4641613', 'n_tokens_mistral': 801, 'n_tokens_neox': 792, 'n_words': 158}