<commit_before>import Promise from 'bluebird';
import { List } from 'immutable';

const reconnectableStatus = 4000;
const timeoutStart = 300;
const timeoutMax = 2 * 60 * 1000;
export const readyStates = {
  CONNECTING: 0,
  OPEN: 1,
  CLOSING: 2,
  CLOSED: 3,
  RECONNECT_ABORTED: 4,
};

const globalVar = typeof global !== 'undefined' // eslint-disable-line
  ? global
  : (typeof window !== 'undefined' ? window : {});

function createWebSocket(url, protocols, customWsClient) {
  const urlOk = /wss?:\/\//.exec(url);
  if (!urlOk) {
    throw new Error('Invalid url provided');
  }
  const Socket = customWsClient || globalVar.WebSocket || globalVar.MozWebSocket;
  return new Socket(url, protocols || undefined);
}

export default class WsClient {

  constructor(url, protocols, options = {}) {
    this.init(url, protocols, options);
    this.reconnectAttempts = 0;
    this.manualClose = false;
    this.sendQueue = List();
    this.onOpenCallbacks = List();
    this.onCloseCallbacks = List();
    this.onErrorCallbacks = List();
    this.onMessageCallbacks = List();
    this.socket = undefined;
  }

  // ==============================================================================================
  //  PUBLIC

  connect = (url, protocols, options = {}) => {
    this.init(url, protocols, options);
    if (!this.isOpen()) {
      this.socket = createWebSocket(this.url, this.protocols, this.customWsClient);
      this.socket.onmessage = this.onMessageHandler;
      this.socket.onopen = this.onOpenHandler;
      this.socket.onerror = this.onErrorHandler;
      this.socket.onclose = this.onCloseHandler;
    }
    return this;
  }

  isOpen() {
    return this.socket && this.socket.readyState === readyStates.OPEN;
  }

  init(url, protocols, options = {}) {<commit_msg>connecting state check on (re?)connect<commit_after>import Promise from 'bluebird';
import { List } from 'immutable';

const reconnectableStatus = 4000;
const timeoutStart = 300;
const timeoutMax = 2 * 60 * 1000;
export const readyStates = {
  CONNECTING: 0,
  OPEN: 1,
  CLOSING: 2,
  CLOSED: 3,
  RECONNECT_ABORTED: 4,
};

const globalVar = typeof global !== 'undefined' // eslint-disable-line
  ? global
  : (typeof window !== 'undefined' ? window : {});

function createWebSocket(url, protocols, customWsClient) {
  const urlOk = /wss?:\/\//.exec(url);
  if (!urlOk) {
    throw new Error('Invalid url provided');
  }
  const Socket = customWsClient || globalVar.WebSocket || globalVar.MozWebSocket;
  return new Socket(url, protocols || undefined);
}

export default class WsClient {

  constructor(url, protocols, options = {}) {
    this.init(url, protocols, options);
    this.reconnectAttempts = 0;
    this.manualClose = false;
    this.sendQueue = List();
    this.onOpenCallbacks = List();
    this.onCloseCallbacks = List();
    this.onErrorCallbacks = List();
    this.onMessageCallbacks = List();
    this.socket = undefined;
  }

  // ==============================================================================================
  //  PUBLIC

  connect = (url, protocols, options = {}) => {
    this.init(url, protocols, options);
    if (!this.isOpen() && this.socket.readyState !== readyStates.CONNECTING) {
      this.socket = createWebSocket(this.url, this.protocols, this.customWsClient);
      this.socket.onmessage = this.onMessageHandler;
      this.socket.onopen = this.onOpenHandler;
      this.socket.onerror = this.onErrorHandler;
      this.socket.onclose = this.onCloseHandler;
    }
    return this;
  }

  isOpen() {
    return this.socket && this.socket.readyState === readyStates.OPEN;
  }

  init(url, protocols, options = {}) {
{'fraction_non_alphanumeric': 0.1821917808219178, 'fraction_numerical': 0.010958904109589041, 'mean_word_length': 3.75390625, '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': '5064580', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1103, 'n_words': 256}