<commit_before>var AppDispatcher      = require('../dispatcher/AppDispatcher'),
    EventEmitter       = require('events').EventEmitter,
    assign             = require('react/lib/Object.assign'),
    Constants          = require('../Constants'),
    socket             = require('socket'),

    CHANGE_EVENT       = 'change',
    API                = {
        SEARCH_USER: 'plugins.ns-awards.searchUser'
    },
    _result            = [],
    _resultSelectIndex = 0,
    _selected          = [];

var SearchUsersStore = assign({}, EventEmitter.prototype, {
    addChangeListener: function (listener) {
        this.on(CHANGE_EVENT, listener);
    },

    emitChange: function () {
        this.emit(CHANGE_EVENT);
    },

    getResult: function () {
        return _result;
    },

    getResultSelectIndex: function () {
        return _resultSelectIndex;
    },

    getSelected: function () {
        return _selected;
    },

    removeChangeListener: function (listener) {
        this.removeListener(CHANGE_EVENT, listener);
    }
});

AppDispatcher.register(function (action) {
    switch (action.actionType) {
        case Constants.EVENT_SEARCH_USER:
            socket.emit(API.SEARCH_USER, {
                username: action.request
            }, function (error, searchResult) {
                //Search Result will have signature: {matchCount: 1, pagination: PaginationMeta, pageCount: 1, timing: "0.01", users: Array[N]}
                _result = searchResult.users;
                // Adjust latest selection
                if (_resultSelectIndex >= _result.length) {
                    _resultSelectIndex = _result.length - 1;
                }
                SearchUsersStore.emitChange();
            });
            break;
        case Constants.EVENT_CLEAR_SEARCH_RESULT:
            clear();
            break;
        case Constants.EVENT_OFFSET_USER_FROM_SEARCH_ON:
            _resultSelectIndex += action.offset;
            if (_resultSelectIndex < 0) {
                if (_result.length > 0) {
                    _resultSelectIndex = _result.length - 1;
                } else {
                    _resultSelectIndex = 0;
                }
            } else if (_resultSelectIndex >= _result.length) {
                _resultSelectIndex = 0;
            }
            SearchUsersStore.emitChange();
            break;
        case Constants.EVENT_PICK_USER_FROM_SEARCH:
            pickUserAt(_resultSelectIndex);
            break;
        case Constants.EVENT_PICK_USER_FROM_SEARCH_AT:
            pickUserAt(action.index);
            break;
        case Constants.EVENT_UNPICK_USER_FROM_SEARCH:
            _selected.splice(action.index, 1);
            SearchUsersStore.emitChange();
            break;
        case Constants.EVENT_AWARD_USERS:
            clear();
            break;
        default:
            return true;
    }
});

function clear() {
    _result.length = 0;
    _resultSelectIndex = 0;
    _selected.length = 0;
    SearchUsersStore.emitChange();
}

function pickUserAt(index) {
    // Validation
    if (index < 0 || index >= _result.length) {
        console.error('Index for picking User is invalid, something went wrong in the system...');
        return;
    }

    _selected.push(_result[index]);
    _result.length = 0;
    _resultSelectIndex = 0;
    SearchUsersStore.emitChange();
}

module.exports = SearchUsersStore;<commit_msg>separate search store<commit_after>var Actions       = require('../actions/Actions'),
    AppDispatcher = require('../dispatcher/AppDispatcher'),
    EventEmitter  = require('events').EventEmitter,
    assign        = require('react/lib/Object.assign'),
    Constants     = require('../Constants'),
    socket        = require('socket'),

    CHANGE_EVENT  = 'change',
    API           = {
        SEARCH_USER: 'plugins.ns-awards.searchUser'
    },
    _result       = [],
    _selectIndex  = 0;

var SearchUsersStore = assign({}, EventEmitter.prototype, {
    addChangeListener: function (listener) {
        this.on(CHANGE_EVENT, listener);
    },

    emitChange: function () {
        this.emit(CHANGE_EVENT);
    },

    getResult: function () {
        return _result;
    },

    getSelectIndex: function () {
        return _selectIndex;
    },

    removeChangeListener: function (listener) {
        this.removeListener(CHANGE_EVENT, listener);
    }
});

AppDispatcher.register(function (action) {
    switch (action.actionType) {
        case Constants.EVENT_SEARCH_USER:
            socket.emit(API.SEARCH_USER, {
                username: action.request
            }, function (error, searchResult) {
                //Search Result will have signature: {matchCount: 1, pagination: PaginationMeta, pageCount: 1, timing: "0.01", users: Array[N]}
                _result = searchResult.users;
                // Adjust latest selection
                if (_selectIndex >= _result.length) {
                    _selectIndex = _result.length - 1;
                }
                SearchUsersStore.emitChange();
            });
            break;
        case Constants.EVENT_CLEAR_SEARCH_RESULT:
            clear();
            break;
        case Constants.EVENT_OFFSET_USER_FROM_SEARCH_ON:
            _selectIndex += action.offset;
            if (_selectIndex < 0) {
                if (_result.length > 0) {
                    _selectIndex = _result.length - 1;
                } else {
                    _selectIndex = 0;
                }
            } else if (_selectIndex >= _result.length) {
                _selectIndex = 0;
            }
            SearchUsersStore.emitChange();
            break;
        case Constants.EVENT_PICK_USER_FROM_SEARCH:
            pickUserAt(_selectIndex);
            break;
        case Constants.EVENT_PICK_USER_FROM_SEARCH_AT:
            pickUserAt(action.index);
            break;
        default:
            return true;
    }
});

function clear() {
    _result.length = 0;
    _selectIndex = 0;
    SearchUsersStore.emitChange();
}

function pickUserAt(index) {
    // Validation
    if (index < 0 || index >= _result.length) {
        console.error('Index for picking User is invalid, something went wrong in the system...');
        return;
    }

    Actions.selectUser(_result[index]);
}

module.exports = SearchUsersStore;
{'fraction_non_alphanumeric': 0.10336081166772353, 'fraction_numerical': 0.00538998097653773, 'mean_word_length': 1.8612244897959183, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6422614', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1675, 'n_words': 333}