<commit_before>import { Component } from 'react';
import { connect } from 'react-redux';
import ReconnectableWebSocket from 'reconnectable-websocket';
import Url from 'url';

import { subscribe as certificates } from '../ducks/certificates';
import {
  connected,
  disconnected,
  received,
} from '../ducks/connection';
import { subscribe as connections } from '../ducks/connections';
import { subscribe as devices } from '../ducks/devices';
import { subscribe as nodes } from '../ducks/nodes';
import { subscribe as requests } from '../ducks/requests';
import { subscribe as rules } from '../ducks/rules';
import { subscribe as savedstates } from '../ducks/savedstates';
import { update as updateServer } from '../ducks/server';

// Placeholder until we have the write func from the websocket
let writeFunc = () => {
  throw new Error('Not initialized yet');
};
export const write = msg => writeFunc(JSON.stringify(msg));

class Websocket extends Component {
  constructor(props) {
    super(props);

    this.subscriptions = {};
  }

  componentDidMount() {
    this.setupSocket(this.props);
  }

  componentWillReceiveProps(props) {
    this.setupSocket(props);
  }

  componentWillUnmount() {
    if (typeof this.socket !== 'undefined') {
      this.socket.close();
    }
  }

  onOpen = () => () => {
    this.props.dispatch(connected());

    const url = Url.parse(this.props.url);
    if (url.protocol === 'wss:') {
      this.props.dispatch(updateServer({ secure: true }));
    }

    this.subscribe({
      certificates,
      connections,
      devices,
      nodes,
      requests,
      rules,
      savedstates,
    });
  }
  onClose = () => () => {
    this.props.dispatch(disconnected());
    this.subscriptions = {};
  }
  onMessage = () => (event) => {
    const { dispatch } = this.props;
    const parsed = JSON.parse(event.data);

    dispatch(received(parsed));
    const subscriptions = this.subscriptions[parsed.type];
    if (subscriptions) {
      subscriptions.forEach(callback => callback(parsed.body));
    }
    switch (parsed.type) {
      case 'server-info': {
       dispatch(updateServer(parsed.body));
       break;
      }
    }
  }

  setupSocket(props) {
    const { url } = props;
    if (this.socket) {
      // this is becuase there is a bug in reconnecting websocket causing it to retry forever
      this.socket.onclose = () => throw ('force close socket');
      // Close the existing connection
      this.socket.close();
    }

    this.socket = new ReconnectableWebSocket(url, ['gui'], {
      reconnectInterval: 3000,
      timeoutInterval: 1000,
    });
    writeFunc = this.socket.send;
    this.socket.onmessage = this.onMessage();
    this.socket.onopen = this.onOpen();
    this.socket.onclose = this.onClose();
  }

  subscribe = (ducks) => {
    const subscriptions = [];
    Object.keys(ducks).forEach((duck) => {
      if (!ducks[duck]) {
        return;
      }

      const channels = ducks[duck](this.props.dispatch);
      Object.keys(channels).forEach((channel) => {
        this.subscriptions[channel] = this.subscriptions[channel] || [];
        this.subscriptions[channel].push(channels[channel]);
        subscriptions.push(channel);
      });
    });

    write({
      type: 'subscribe',
      body: subscriptions,
    });
  }

  render = () => null;
}

const mapToProps = state => ({
  url: state.getIn(['app', 'url']),
});
export default connect(mapToProps)(Websocket);

<commit_msg>Forgot the lint fixes<commit_after>import { Component } from 'react';
import { connect } from 'react-redux';
import ReconnectableWebSocket from 'reconnectable-websocket';
import Url from 'url';

import { subscribe as certificates } from '../ducks/certificates';
import {
  connected,
  disconnected,
  received,
} from '../ducks/connection';
import { subscribe as connections } from '../ducks/connections';
import { subscribe as devices } from '../ducks/devices';
import { subscribe as nodes } from '../ducks/nodes';
import { subscribe as requests } from '../ducks/requests';
import { subscribe as rules } from '../ducks/rules';
import { subscribe as savedstates } from '../ducks/savedstates';
import { update as updateServer } from '../ducks/server';

// Placeholder until we have the write func from the websocket
let writeFunc = () => {
  throw new Error('Not initialized yet');
};
export const write = msg => writeFunc(JSON.stringify(msg));

class Websocket extends Component {
  constructor(props) {
    super(props);

    this.subscriptions = {};
  }

  componentDidMount() {
    this.setupSocket(this.props);
  }

  componentWillReceiveProps(props) {
    this.setupSocket(props);
  }

  componentWillUnmount() {
    if (typeof this.socket !== 'undefined') {
      this.socket.close();
    }
  }

  onOpen = () => () => {
    this.props.dispatch(connected());

    const url = Url.parse(this.props.url);
    if (url.protocol === 'wss:') {
      this.props.dispatch(updateServer({ secure: true }));
    }

    this.subscribe({
      certificates,
      connections,
      devices,
      nodes,
      requests,
      rules,
      savedstates,
    });
  }
  onClose = () => () => {
    this.props.dispatch(disconnected());
    this.subscriptions = {};
  }
  onMessage = () => (event) => {
    const { dispatch } = this.props;
    const parsed = JSON.parse(event.data);

    dispatch(received(parsed));
    const subscriptions = this.subscriptions[parsed.type];
    if (subscriptions) {
      subscriptions.forEach(callback => callback(parsed.body));
    }
    switch (parsed.type) {
      case 'server-info': {
        dispatch(updateServer(parsed.body));
        break;
      }
      default: {
        // Nothing
      }
    }
  }

  setupSocket(props) {
    const { url } = props;
    if (this.socket) {
      // this is becuase there is a bug in reconnecting websocket causing it to retry forever
      this.socket.onclose = () => { throw new Error('force close socket'); };
      // Close the existing connection
      this.socket.close();
    }

    this.socket = new ReconnectableWebSocket(url, ['gui'], {
      reconnectInterval: 3000,
      timeoutInterval: 1000,
    });
    writeFunc = this.socket.send;
    this.socket.onmessage = this.onMessage();
    this.socket.onopen = this.onOpen();
    this.socket.onclose = this.onClose();
  }

  subscribe = (ducks) => {
    const subscriptions = [];
    Object.keys(ducks).forEach((duck) => {
      if (!ducks[duck]) {
        return;
      }

      const channels = ducks[duck](this.props.dispatch);
      Object.keys(channels).forEach((channel) => {
        this.subscriptions[channel] = this.subscriptions[channel] || [];
        this.subscriptions[channel].push(channels[channel]);
        subscriptions.push(channel);
      });
    });

    write({
      type: 'subscribe',
      body: subscriptions,
    });
  }

  render = () => null;
}

const mapToProps = state => ({
  url: state.getIn(['app', 'url']),
});
export default connect(mapToProps)(Websocket);


{'fraction_non_alphanumeric': 0.15051605504587157, 'fraction_numerical': 0.0022935779816513763, 'mean_word_length': 2.9196629213483147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '3432113', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2048, 'n_words': 470}