<commit_before>import async from 'async';
import Logdown from 'logdown';
import {AvailableTransports} from './transports/index';
import {CLIENT_EVENTS, CLIENT_STATES} from './Constants';
import ConnectingMessageBuffer from './ConnectingMessageBuffer';

export const CONNECTION_CONFIG_DEFAULTS = {
  logger: new Logdown({prefix: 'SignalR Connection'}),
  KeepAliveTimeout: 0
};

export default class Connection {
  constructor(client, config) {
    config = Object.assign({}, CONNECTION_CONFIG_DEFAULTS, config);
    this._client = client;
    this._url = config.Url;
    this._logger = config.logger;
    this._connectionToken = config.ConnectionToken;
    this._connectionId = config.ConnectionId;
    this._keepAliveData = {
      monitor: false,
      activated: !!config.KeepAliveTimeout,
      timeout: config.KeepAliveTimeout * 1000,
      timeoutWarning: (config.KeepAliveTimeout * 1000) * (2 / 3),
      transportNotified: false
    };
    this._disconnectTimeout = config.DisconnectTimeout * 1000;
    this._connectionTimeout = config.ConnectionTimeout;
    this._tryWebSockets = config.TryWebSockets;
    this._protocolVersion = config.ProtocolVersion;
    this._transportConnectTimeout = config.TransportConnectTimeout;
    this._longPollDelay = config.LongPollDelay;
    this._pollTimeout = config.ConnectionTimeout * 1000 + 10000;
    this._reconnectWindow = (config.KeepAliveTimeout + config.DisconnectTimeout) * 1000;
    this._connectingMessageBuffer = new ConnectingMessageBuffer(client, client.emit.bind(client, CLIENT_EVENTS.onReceived));
    this._beatInterval = (this._keepAliveData.timeout - this._keepAliveData.timeoutWarning) / 3;
    this._lastActiveAt = new Date().getTime();

  }

  _supportsKeepAlive() {
    return this._keepAliveData.activated && this._transport.supportsKeepAlive;
  }

  _checkIsAlive(){
    let keepAliveData = this._keepAliveData,
      timeElapsed;
    if(this._client.state === CLIENT.STATES.connected){
      timeElapsed = new Date().getTime() - this._transport._lastMessageAt;
      if(timeElapsed >= keepAliveData.timeout){
        this.log('Keep alive timed out. Notifying the transport that the connection has been lost. Will attempt to reconnect');
        this._transport._reconnect();
      } else if(timeElapsed >= keepAliveData.timeoutWarning){
        this.log('Keep alive missed but not timed out. May have slow or unstable/dead connection.');<commit_msg>Fixed typo in new KeepAlive implementation. Addition towards issue #8<commit_after>import async from 'async';
import Logdown from 'logdown';
import {AvailableTransports} from './transports/index';
import {CLIENT_EVENTS, CLIENT_STATES} from './Constants';
import ConnectingMessageBuffer from './ConnectingMessageBuffer';

export const CONNECTION_CONFIG_DEFAULTS = {
  logger: new Logdown({prefix: 'SignalR Connection'}),
  KeepAliveTimeout: 0
};

export default class Connection {
  constructor(client, config) {
    config = Object.assign({}, CONNECTION_CONFIG_DEFAULTS, config);
    this._client = client;
    this._url = config.Url;
    this._logger = config.logger;
    this._connectionToken = config.ConnectionToken;
    this._connectionId = config.ConnectionId;
    this._keepAliveData = {
      monitor: false,
      activated: !!config.KeepAliveTimeout,
      timeout: config.KeepAliveTimeout * 1000,
      timeoutWarning: (config.KeepAliveTimeout * 1000) * (2 / 3),
      transportNotified: false
    };
    this._disconnectTimeout = config.DisconnectTimeout * 1000;
    this._connectionTimeout = config.ConnectionTimeout;
    this._tryWebSockets = config.TryWebSockets;
    this._protocolVersion = config.ProtocolVersion;
    this._transportConnectTimeout = config.TransportConnectTimeout;
    this._longPollDelay = config.LongPollDelay;
    this._pollTimeout = config.ConnectionTimeout * 1000 + 10000;
    this._reconnectWindow = (config.KeepAliveTimeout + config.DisconnectTimeout) * 1000;
    this._connectingMessageBuffer = new ConnectingMessageBuffer(client, client.emit.bind(client, CLIENT_EVENTS.onReceived));
    this._beatInterval = (this._keepAliveData.timeout - this._keepAliveData.timeoutWarning) / 3;
    this._lastActiveAt = new Date().getTime();

  }

  _supportsKeepAlive() {
    return this._keepAliveData.activated && this._transport.supportsKeepAlive;
  }

  beat(){

  }

  startHeartbeat(){

  }

  _checkIsAlive(){
    let keepAliveData = this._keepAliveData,
      timeElapsed;
    if(this._client.state === CLIENT_STATES.connected){
      timeElapsed = new Date().getTime() - this._transport._lastMessageAt;
      if(timeElapsed >= keepAliveData.timeout){
        this.log('Keep alive timed out. Notifying the transport that the connection has been lost. Will attempt to reconnect');
        this._transport._reconnect();
      } else if(timeElapsed >= keepAliveData.timeoutWarning){
        this.log('Keep alive missed but not timed out. May have slow or unstable/dead connection.');
{'fraction_non_alphanumeric': 0.10271474878444084, 'fraction_numerical': 0.011952998379254457, 'mean_word_length': 4.252127659574468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1630982', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1378, 'n_words': 310}