<commit_before>/* eslint-disable no-underscore-dangle */

import assign from "object-assign";
import React from "react";

const ID = Symbol("ReactResolver.ID");
const CHILDREN = Symbol("ReactResolver.CHILDREN");
const HAS_RESOLVED = Symbol("ReactResolver.HAS_RESOLVED");
const IS_CLIENT = Symbol("ReactResolver.IS_CLIENT");

export default class Resolver extends React.Component {
  static childContextTypes = {
    resolver: React.PropTypes.instanceOf(Resolver),
  }

  static contextTypes = {
    resolver: React.PropTypes.instanceOf(Resolver),
  }

  static defaultProps = {
    data: {},
    props: {},
    resolve: {},
  }

  static propTypes = {
    component: React.PropTypes.func.isRequired,
    data: React.PropTypes.object.isRequired,
    props: React.PropTypes.object,
    resolve: React.PropTypes.object,
  }

  static render = function(render, node) {
    const initialData = window.__REACT_RESOLVER_PAYLOAD__;

    // Server-rendered output, but missing payload
    if (!initialData && node.innerHTML) {
      return Resolver.resolve(render).then(({ Resolved }) => {
        React.render(<Resolved />, node);
      });
    }

    React.render(<Resolver data={initialData} render={render} />, node);

    delete window.__REACT_RESOLVER_PAYLOAD__;
  }

  static resolve = function(render, initialData = {}) {
    const queue = [];

    React.renderToStaticMarkup(
      <Resolver
        data={initialData}
        onResolve={(promise) => queue.push(promise)}
        render={render}
      />
    );

    return Promise.all(queue).then((results) => {
      const data = assign({}, initialData);

      results.forEach(({ id, resolved }) => data[id] = resolved);

      if (Object.keys(initialData).length < Object.keys(data).length) {
        return Resolver.resolve(render, data);
      }

      class Resolved extends React.Component {
        displayName = "Resolved"

        render() {
          return (
            <Resolver data={data} render={render} />
          );
        }
      }

      return { data, Resolved };
    });
  }

  displayName = "Resolver"

  constructor(props, context) {
    super(props, context);

    // Internal tracking variables
    this[ID] = this.generateId();
    this[CHILDREN] = [];
    this[HAS_RESOLVED] = false;
    this[IS_CLIENT] = false;

    this.state = {
      pending: {},
      resolved: this.cached() || {},
    };
  }

  cached(resolver = this) {
    const id = resolver[ID];

    if (this.props.data.hasOwnProperty(id)) {
      return assign({}, this.props.data[id]);
    } else if (this.context.resolver) {
      return this.context.resolver.cached(resolver);
    }
  }

  clearData(resolver = this) {
    const id = resolver[ID];

    if (this.props.data.hasOwnProperty(id)) {
      delete this.props.data[id];
    } else if (this.context.resolver) {
      return this.context.resolver.clearData(resolver);
    }
  }

  componentDidMount() {
    this[IS_CLIENT] = true;
  }

  componentWillMount() {
    const nextState = this.computeState(this.props, this.state);

    if (this.isPending(nextState)) {
      this.resolve(nextState);
      this[HAS_RESOLVED] = false;
    } else {
      this[HAS_RESOLVED] = true;
    }

    this.setState(nextState);
  }

  componentWillReceiveProps(nextProps) {
    const cleanState = {
      pending: {},
      resolved: {},
    };

    const { pending, resolved } = this.computeState(nextProps, cleanState);

    // Next state will resolve async props again, but update existing sync props
    const nextState = {<commit_msg>Move componentWillMount logic to constructor<commit_after>/* eslint-disable no-underscore-dangle */

import assign from "object-assign";
import React from "react";

const ID = Symbol("ReactResolver.ID");
const CHILDREN = Symbol("ReactResolver.CHILDREN");
const HAS_RESOLVED = Symbol("ReactResolver.HAS_RESOLVED");
const IS_CLIENT = Symbol("ReactResolver.IS_CLIENT");

export default class Resolver extends React.Component {
  static childContextTypes = {
    resolver: React.PropTypes.instanceOf(Resolver),
  }

  static contextTypes = {
    resolver: React.PropTypes.instanceOf(Resolver),
  }

  static defaultProps = {
    data: {},
    props: {},
    resolve: {},
  }

  static propTypes = {
    component: React.PropTypes.func.isRequired,
    data: React.PropTypes.object.isRequired,
    props: React.PropTypes.object,
    resolve: React.PropTypes.object,
  }

  static render = function(render, node) {
    const initialData = window.__REACT_RESOLVER_PAYLOAD__;

    // Server-rendered output, but missing payload
    if (!initialData && node.innerHTML) {
      return Resolver.resolve(render).then(({ Resolved }) => {
        React.render(<Resolved />, node);
      });
    }

    React.render(<Resolver data={initialData} render={render} />, node);

    delete window.__REACT_RESOLVER_PAYLOAD__;
  }

  static resolve = function(render, initialData = {}) {
    const queue = [];

    React.renderToStaticMarkup(
      <Resolver
        data={initialData}
        onResolve={(promise) => queue.push(promise)}
        render={render}
      />
    );

    return Promise.all(queue).then((results) => {
      const data = assign({}, initialData);

      results.forEach(({ id, resolved }) => data[id] = resolved);

      if (Object.keys(initialData).length < Object.keys(data).length) {
        return Resolver.resolve(render, data);
      }

      class Resolved extends React.Component {
        displayName = "Resolved"

        render() {
          return (
            <Resolver data={data} render={render} />
          );
        }
      }

      return { data, Resolved };
    });
  }

  displayName = "Resolver"

  constructor(props, context) {
    super(props, context);

    // Internal tracking variables
    this[ID] = this.generateId();
    this[CHILDREN] = [];
    this[HAS_RESOLVED] = false;
    this[IS_CLIENT] = false;

    this.state = this.computeState(this.props, {
      pending: {},
      resolved: this.cached() || {},
    });

    if (this.isPending(this.state)) {
      this.resolve(this.state);
      this[HAS_RESOLVED] = false;
    } else {
      this[HAS_RESOLVED] = true;
    }
  }

  cached(resolver = this) {
    const id = resolver[ID];

    if (this.props.data.hasOwnProperty(id)) {
      return assign({}, this.props.data[id]);
    } else if (this.context.resolver) {
      return this.context.resolver.cached(resolver);
    }
  }

  clearData(resolver = this) {
    const id = resolver[ID];

    if (this.props.data.hasOwnProperty(id)) {
      delete this.props.data[id];
    } else if (this.context.resolver) {
      return this.context.resolver.clearData(resolver);
    }
  }

  componentDidMount() {
    this[IS_CLIENT] = true;

  }

  componentWillReceiveProps(nextProps) {
    const cleanState = {
      pending: {},
      resolved: {},
    };

    const { pending, resolved } = this.computeState(nextProps, cleanState);

    // Next state will resolve async props again, but update existing sync props
    const nextState = {
{'fraction_non_alphanumeric': 0.1350192884697814, 'fraction_numerical': 0.0, 'mean_word_length': 2.875968992248062, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '5647938', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2144, 'n_words': 421}