<commit_before>import React from 'react';
import Temperature from './Temperature'
import Spinner from './Spinner';

import './css/temperatures.css';

class LiveTemperatures extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      error: null,
      measurements: [],
      socket: null
    }
  }
  
  componentWillReceiveProps(nextProps) {
    if (this.props.showAveragedSensors !== nextProps.showAveragedSensors) {
      if (this.state.socket) {
        this.state.socket.close();
      }      
      this.setState({
        socket: this.createSocket(nextProps.showAveragedSensors)
      });
    }
  }
  
  componentDidMount() {
    this.setState({
      socket: this.createSocket(this.props.showAveragedSensors)
    });
  }
  
  createSocket(showAverage) {
    const socket = new WebSocket(this.getUrl(showAverage));
    socket.onopen = () => this.onSocketOpen();
    socket.onmessage = (message) => this.onSocketData(message);
    socket.onclose = () => this.onSocketClose();
    return socket;
  }

  render() {
    return <div>
      <div className="temperatures-heading">
        <p>Current Temperature</p>
      </div>
      <div className="temperatures-area">
        { this.renderMainTemperatureArea() }
      </div>
    </div>
  }

  renderMainTemperatureArea() {
    let element = null;
    if (this.state.loading === true)
      element = <Spinner/>;
    else if (this.state.error)
      element = this.renderError(this.state.error);
    else
      element = this.renderTemperatures(this.state.measurements);
    return element;<commit_msg>close socket when unmounting<commit_after>import React from 'react';
import Temperature from './Temperature'
import Spinner from './Spinner';

import './css/temperatures.css';

class LiveTemperatures extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      error: null,
      measurements: [],
      socket: null
    }
  }
  
  componentWillReceiveProps(nextProps) {
    if (this.props.showAveragedSensors !== nextProps.showAveragedSensors) {
      if (this.state.socket) {
        this.state.socket.close();
      }      
      this.setState({
        socket: this.createSocket(nextProps.showAveragedSensors)
      });
    }
  }
  
  componentDidMount() {
    this.setState({
      socket: this.createSocket(this.props.showAveragedSensors)
    });
  }
  
  componentWillUnmount() {
    if (this.state.socket) {
      this.state.socket.close();
    }
  }
  
  createSocket(showAverage) {
    const socket = new WebSocket(this.getUrl(showAverage));
    socket.onopen = () => this.onSocketOpen();
    socket.onmessage = (message) => this.onSocketData(message);
    socket.onclose = () => this.onSocketClose();
    return socket;
  }

  render() {
    return <div>
      <div className="temperatures-heading">
        <p>Current Temperature</p>
      </div>
      <div className="temperatures-area">
        { this.renderMainTemperatureArea() }
      </div>
    </div>
  }

  renderMainTemperatureArea() {
    let element = null;
    if (this.state.loading === true)
      element = <Spinner/>;
    else if (this.state.error)
      element = this.renderError(this.state.error);
    else
      element = this.renderTemperatures(this.state.measurements);
    return element;
{'fraction_non_alphanumeric': 0.13193403298350825, 'fraction_numerical': 0.0, 'mean_word_length': 2.7823129251700682, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '7522483', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1029, 'n_words': 182}