<commit_before>"use strict";

const WebSocket = require("ws");
const Constants = require("../../Constants");

const heartbeat = new WeakMap();

class BaseSocket extends WebSocket {
  constructor(url) {
    super(url);
    this.readyState = Constants.ReadyState.CONNECTING;
    this.on("open", () => this.readyState = Constants.ReadyState.OPEN);

    const close = () => {
      this.unsetHeartbeat();
      this.readyState = Constants.ReadyState.CLOSED;
    };
    this.on("close", close);
    this.on("error", close);
  }
  get connected() {
    return this.readyState == Constants.ReadyState.OPEN;
  }
  get connecting() {
    return this.readyState == Constants.ReadyState.CONNECTING;
  }
  send(op, data) {
    let m = {op: op, d: data};

    try {
      super.send(JSON.stringify(m));
    } catch (e) { console.error(e.stack); }
  }
  setHeartbeat(opcode, msec) {
    if (!this.connected)
      return;

    heartbeat.set(this, setInterval(() => {
      if (!this.connected)
        return this.close();
      this.send(opcode, new Date().getTime());
    }, msec));
  }
  unsetHeartbeat() {
    var handle = heartbeat.get(this);
    if (handle !== undefined) clearInterval(handle);
    heartbeat.delete(this);
  }
  close() {
    super.close();
    this.unsetHeartbeat();

    // release websocket resources without waiting for close frame from Discord
    if (this._closeTimer && this._closeTimer._onTimeout)
      this._closeTimer._onTimeout();
    clearTimeout(this._closeTimer);
  }
}

module.exports = BaseSocket;<commit_msg>Revert websocket closing without waiting for close frame<commit_after>"use strict";

const WebSocket = require("ws");
const Constants = require("../../Constants");

const heartbeat = new WeakMap();

class BaseSocket extends WebSocket {
  constructor(url) {
    super(url);
    this.readyState = Constants.ReadyState.CONNECTING;
    this.on("open", () => this.readyState = Constants.ReadyState.OPEN);

    const close = () => {
      this.unsetHeartbeat();
      this.readyState = Constants.ReadyState.CLOSED;
    };
    this.on("close", close);
    this.on("error", close);
  }
  get connected() {
    return this.readyState == Constants.ReadyState.OPEN;
  }
  get connecting() {
    return this.readyState == Constants.ReadyState.CONNECTING;
  }
  send(op, data) {
    let m = {op: op, d: data};

    try {
      super.send(JSON.stringify(m));
    } catch (e) { console.error(e.stack); }
  }
  setHeartbeat(opcode, msec) {
    if (!this.connected)
      return;

    heartbeat.set(this, setInterval(() => {
      if (!this.connected)
        return this.close();
      this.send(opcode, new Date().getTime());
    }, msec));
  }
  unsetHeartbeat() {
    var handle = heartbeat.get(this);
    if (handle !== undefined) clearInterval(handle);
    heartbeat.delete(this);
  }
  close() {
    super.close();
    this.unsetHeartbeat();

    // wait close frame for 5 seconds instead of default 30
    if (this._closeTimer && this._closeTimer._onTimeout) {
      var callback = this._closeTimer._onTimeout;
      setTimeout(() => callback(), 5000);
    }
    clearTimeout(this._closeTimer);
  }
}

module.exports = BaseSocket;
{'fraction_non_alphanumeric': 0.15177439797211661, 'fraction_numerical': 0.002217997465145754, 'mean_word_length': 3.1376146788990824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '1760925', 'n_tokens_mistral': 1004, 'n_tokens_neox': 986, 'n_words': 209}