<commit_before>// TODO: hoist statics and all this HOC stuff

import React from 'react';
import PropTypes from 'prop-types';

import { getDisplayName } from './util/getDisplayName';

import { omit } from './util/omit';

export function withYMaps(Component, waitForApi = false) {
  class WithYMaps extends React.Component {
    constructor() {
      super();

      this._isMounted = true;
      this._unsubscribe = null;
    }

    componentDidMount() {
      if (this.context.ymaps.api === null) {
        this._unsubscribe = this.context.ymaps.subscribe(() => {
          if (this._isMounted === true) {
            this._unsubscribe();
            this.forceUpdate();
          }
        });

        this.context.ymaps
          .load()
          .then(api => {
            if (this._isMounted === true) {
              this.context.ymaps.update();
              this.props.onLoad(api);
            }
          })
          .catch(err => {
            if (this._isMounted === true) {
              this.props.onError(err);
            }
          });
      }
    }

    componentWillUnmount() {
      if (typeof this._unsubscribe === 'function') {
        this._unsubscribe();
      }
      this._isMounted = false;
    }

    render() {
      // eslint-disable-next-line no-unused-vars
      const { onLoad, onError } = this.props;
      const { api } = this.context.ymaps;

      const shouldRender = waitForApi === false || api !== null;

      return (
        shouldRender &&
        React.createElement(
          Component,
          Object.assign({ ymaps: api }, omit(this.props, ['onLoad', 'onError']))
        )
      );
    }
  }

  WithYMaps.displayName = `withYMaps(${getDisplayName(Component)})`;

  WithYMaps.propTypes = {
    onLoad: PropTypes.func,
    onError: PropTypes.func,
  };

  WithYMaps.defaultProps = {
    onLoad: Function.prototype,
    onError: Function.prototype,
  };

  WithYMaps.contextTypes = {
    ymaps: PropTypes.object,<commit_msg>Make withYMaps HOC responsible for fetching modules<commit_after>// TODO: hoist statics and all this HOC stuff

import React from 'react';
import PropTypes from 'prop-types';

import { getDisplayName as name } from './util/getDisplayName';

import { omit } from './util/omit';

export function withYMaps(Component, waitForApi = false, modules = []) {
  class WithYMaps extends React.Component {
    constructor() {
      super();
      this._isMounted = false;
    }

    componentWillMount() {
      if (!this.context.ymaps) {
        throw new Error(
          "Couldn't find YMaps in the context. " +
            `Make sure that <${name(Component)} /> is inside <YMapsProvider />`
        );
      }
    }

    componentDidMount() {
      this._isMounted = true;

      this.context.ymaps
        .load()
        .then(api => {
          return Promise.all(
            modules
              .concat(this.props.modules)
              .map(moduleName => api.loadModule(moduleName))
          ).then(() => {
            if (this._isMounted === true) {
              this.forceUpdate();
              this.props.onLoad(api);
            }
          });
        })
        .catch(err => {
          if (this._isMounted === true) {
            this.props.onError(err);
          }
        });
    }

    componentWillUnmount() {
      this._isMounted = false;
    }

    render() {
      const { api } = this.context.ymaps;

      const shouldRender = waitForApi === false || api !== null;

      return (
        shouldRender &&
        React.createElement(
          Component,
          Object.assign(
            { ymaps: api },
            omit(this.props, ['onLoad', 'onError', 'modules'])
          )
        )
      );
    }
  }

  WithYMaps.displayName = `withYMaps(${name(Component)})`;

  WithYMaps.propTypes = {
    onLoad: PropTypes.func,
    onError: PropTypes.func,
    modules: PropTypes.arrayOf(PropTypes.string),
  };

  WithYMaps.defaultProps = {
    onLoad: Function.prototype,
    onError: Function.prototype,
    modules: [],
  };

  WithYMaps.contextTypes = {
    ymaps: PropTypes.object,
{'fraction_non_alphanumeric': 0.13225569434239529, 'fraction_numerical': 0.0, 'mean_word_length': 1.9213161659513591, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, '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': '6464049', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1198, 'n_words': 243}