<commit_before>import React from "react-native";
import LocalitiesFiltered from "../components/localities-filtered";
import debounce from "../../lib/debounce";
import store from "../../store/store";

const {
    InteractionManager
} = React;

export default class LocalitiesFilterController extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            data: []
        };

        this._fetchMatchingRooms = debounce(this._fetchMatchingRoomsImmediate.bind(this));
    }

    componentDidMount() {
        this._mounted = true;
    }

    componentWillUnmount() {
        this._mounted = false;
    }

    _fetchMatchingRoomsImmediate(filter) {
        setTimeout(() => {
            this._onDataArrived(store.getAllRooms().filter(room => {
                return (
                    room.id.toLowerCase().indexOf(filter) === 0 ||
                    room.displayName.toLowerCase().indexOf(filter) === 0
                );
            }).slice(0, 10));
        }, 500);
    }

    _onDataArrived(data) {
        InteractionManager.runAfterInteractions(() => {
            if (this._mounted) {
                this.setState({ data });
            }
        });
    }

    _onSearchChange(filter) {
        if (filter) {
            InteractionManager.runAfterInteractions(() => {
                if (this._mounted) {
                    this.setState({
                        data: [ "LOADING" ]
                    });
                }
            });

            this._fetchMatchingRooms(filter);
        } else {
            this.setState({
                data: []
            });
        }
    }

    _onError() {
        InteractionManager.runAfterInteractions(() => {
            if (this._mounted) {
                this.setState({
                    data: [ "FAILED" ]
                });
            }
        });
    }

    render() {
        return (
            <LocalitiesFiltered
                {...this.props}
                {...this.state}
                onSearchChange={this._onSearchChange.bind(this)}
            />
        );
    }
}

LocalitiesFilterController.propTypes = {
    filter: React.PropTypes.string
};
<commit_msg>Cache search results to avoid server roundtrip<commit_after>import React from "react-native";
import LocalitiesFiltered from "../components/localities-filtered";
import debounce from "../../lib/debounce";
import store from "../../store/store";

const {
    InteractionManager
} = React;

export default class LocalitiesFilterController extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            data: []
        };

        this._fetchMatchingRooms = debounce(this._fetchMatchingRoomsImmediate.bind(this));

        this._cachedResults = {};
    }

    componentDidMount() {
        this._mounted = true;
    }

    componentWillUnmount() {
        this._mounted = false;
    }

    _fetchMatchingRoomsImmediate(filter) {
        setTimeout(() => {
            const data = store.getAllRooms().filter(room => {
                return (
                    room.id.toLowerCase().indexOf(filter) === 0 ||
                    room.displayName.toLowerCase().indexOf(filter) === 0
                );
            }).slice(0, 10);

            this._cachedResults[filter] = data;

            this._onDataArrived(data);
        }, 500);
    }

    _onDataArrived(data) {
        InteractionManager.runAfterInteractions(() => {
            if (this._mounted) {
                this.setState({ data });
            }
        });
    }

    _onSearchChange(filter) {
        if (filter) {
            InteractionManager.runAfterInteractions(() => {
                if (this._mounted) {
                    if (this._cachedResults[filter]) {
                        this.setState({
                            data: this._cachedResults[filter]
                        });
                    } else {
                        this.setState({
                            data: [ "LOADING" ]
                        });
                    }
                }
            });

            this._fetchMatchingRooms(filter);
        } else {
            this.setState({
                data: []
            });
        }
    }

    _onError() {
        InteractionManager.runAfterInteractions(() => {
            if (this._mounted) {
                this.setState({
                    data: [ "FAILED" ]
                });
            }
        });
    }

    render() {
        return (
            <LocalitiesFiltered
                {...this.props}
                {...this.state}
                onSearchChange={this._onSearchChange.bind(this)}
            />
        );
    }
}

LocalitiesFilterController.propTypes = {
    filter: React.PropTypes.string
};

{'fraction_non_alphanumeric': 0.12226619454280359, 'fraction_numerical': 0.0033326390335346803, 'mean_word_length': 1.3678500986193294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '7053997', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1219, 'n_words': 184}