<commit_before>const Connection = require('./connection');

class ConnectionPool {
  constructor() {
    this.hosts = [];
    this.nextHostID = 1; // not an index for array

    this.addHost = this.addHost.bind(this);
    this.getDisplayList = this.getDisplayList.bind(this);
    this.onConnectRequest = this.onConnectRequest.bind(this);
  }

  newConnection(ws) {
    return new Connection(ws, {
      addHost: this.addHost,
      getDisplayList: this.getDisplayList,
      onConnectRequest: this.onConnectRequest,
    })
  }

  removeHost(con) {
    this.hosts.splice(this.hosts.indexOf(con), 1);
  }

  // ========================== Callbacks ==========================

  addHost(con) {
    this.hosts.push(con);
    return this.nextHostID++;
  }
<commit_msg>Make removal of host a bit safer<commit_after>const Connection = require('./connection');

class ConnectionPool {
  constructor() {
    this.hosts = [];
    this.nextHostID = 1; // not an index for array

    this.addHost = this.addHost.bind(this);
    this.getDisplayList = this.getDisplayList.bind(this);
    this.onConnectRequest = this.onConnectRequest.bind(this);
  }

  newConnection(ws) {
    const con = new Connection(ws, {
      addHost: this.addHost,
      getDisplayList: this.getDisplayList,
      onConnectRequest: this.onConnectRequest,
      onClose: () => this.removeHost(con),
    });
  }

  removeHost(con) {
    const index = this.hosts.indexOf(con);
    if(index >= 0) {
      this.hosts.splice(index, 1);
    }
  }

  // ========================== Callbacks ==========================

  addHost(con) {
    this.hosts.push(con);
    return this.nextHostID++;
  }

{'fraction_non_alphanumeric': 0.1887135922330097, 'fraction_numerical': 0.003033980582524272, 'mean_word_length': 3.3280839895013123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6686835', 'n_tokens_mistral': 535, 'n_tokens_neox': 503, 'n_words': 90}