<commit_before>import {debounce} from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';

import {optionType} from '../propTypes';
import {getDisplayName} from '../utils';

const DEFAULT_DELAY_MS = 200;

/**
 * HoC that encapsulates common behavior and functionality for doing
 * asynchronous searches, including:
 *
 *  - Debouncing user input
 *  - Optional query caching
 *  - Search prompt and empty results behaviors
 */
const asyncContainer = (Typeahead) => {
  class WrappedTypeahead extends React.Component {
    _cache = {};
    _query = this.props.defaultInputValue || '';

    componentDidMount() {
      this._handleSearchDebounced = debounce(
        this._handleSearch,
        this.props.delay
      );
    }

    componentDidUpdate(prevProps, prevState) {
      if (prevProps.isLoading && this.props.useCache) {
        this._cache[this._query] = this.props.options;
      }
    }

    componentWillUnmount() {
      this._cache = {};
      this._query = '';
      this._handleSearchDebounced.cancel();
    }

    render() {
      const {options, useCache, ...props} = this.props;
      const cachedQuery = this._cache[this._query];

      // Disable custom selections during a search unless `allowNew` is a
      // function.
      const allowNew = typeof props.allowNew === 'function' ?<commit_msg>Update the cache only when isLoading has changed.<commit_after>import {debounce} from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';

import {optionType} from '../propTypes';
import {getDisplayName} from '../utils';

const DEFAULT_DELAY_MS = 200;

/**
 * HoC that encapsulates common behavior and functionality for doing
 * asynchronous searches, including:
 *
 *  - Debouncing user input
 *  - Optional query caching
 *  - Search prompt and empty results behaviors
 */
const asyncContainer = (Typeahead) => {
  class WrappedTypeahead extends React.Component {
    _cache = {};
    _query = this.props.defaultInputValue || '';

    componentDidMount() {
      this._handleSearchDebounced = debounce(
        this._handleSearch,
        this.props.delay
      );
    }

    componentDidUpdate(prevProps, prevState) {
      if (!this.props.isLoading && prevProps.isLoading && this.props.useCache) {
        this._cache[this._query] = this.props.options;
      }
    }

    componentWillUnmount() {
      this._cache = {};
      this._query = '';
      this._handleSearchDebounced.cancel();
    }

    render() {
      const {options, useCache, ...props} = this.props;
      const cachedQuery = this._cache[this._query];

      // Disable custom selections during a search unless `allowNew` is a
      // function.
      const allowNew = typeof props.allowNew === 'function' ?
{'fraction_non_alphanumeric': 0.11691633175009133, 'fraction_numerical': 0.0021921812203142127, 'mean_word_length': 2.997080291970803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6416896', 'n_tokens_mistral': 792, 'n_tokens_neox': 751, 'n_words': 204}