<commit_before>import { Events } from '../constants';
import SmartObject from '../utils/SmartObject';

import requery from './actions/requery';

/**
 * @param observableCollection
 * @param event
 * @param doc
 * @param affectedFields
 */
export default function (observableCollection, event, doc, affectedFields) {
    switch (event) {
        case Events.INSERT:
            handleInsert(observableCollection, doc);
            break;
        case Events.UPDATE:
            handleUpdate(observableCollection, doc, affectedFields);
            break;
        case Events.REMOVE:
            handleRemove(observableCollection, doc);
            break;
        default:
            throw new Meteor.Error(`Invalid event specified: ${event}`)
    }
}

/**
 * @param observableCollection
 * @param doc
 */
const handleInsert = function (observableCollection, doc) {
    if (observableCollection.isEligible(doc)) {
        requery(observableCollection, doc);
    }
};

/**
 * @param observableCollection
 * @param doc
 * @param affectedFields
 */
const handleUpdate = function (observableCollection, doc, affectedFields) {
    const smartObject = new SmartObject(
        doc,
        observableCollection.selector.fields,
        observableCollection.options.sort,
        affectedFields
    );

    if (observableCollection.contains(doc._id)) {
        if (observableCollection.isEligibleByDB(doc._id)) {
            if (smartObject.fieldsExistInSortOptions(affectedFields)) {
                requery(observableCollection, doc, Events.UPDATE);
            } else {
                if (smartObject.fieldsExistInFieldsOptions(affectedFields)) {
                    observableCollection.change(doc._id, doc);
                }
            }
        } else {
            requery(observableCollection);
        }
    } else {
        if (observableCollection.isEligibleByDB(doc._id)) {
            requery(observableCollection);
        }
    }
};

/**
 * @param observableCollection
 * @param doc
 */
const handleRemove = function (observableCollection, doc) {
    if (observableCollection.contains(doc._id)) {
        requery(observableCollection, doc);
    } else {
        if (observableCollection.options.skip) {
            requery(observableCollection, doc)
        }
    }
};<commit_msg>added back the functionality of addById in limit-sort processor<commit_after>import { Events } from '../constants';
import SmartObject from '../utils/SmartObject';

import requery from './actions/requery';

/**
 * @param observableCollection
 * @param event
 * @param doc
 * @param affectedFields
 */
export default function (observableCollection, event, doc, affectedFields) {
    switch (event) {
        case Events.INSERT:
            handleInsert(observableCollection, doc);
            break;
        case Events.UPDATE:
            handleUpdate(observableCollection, doc, affectedFields);
            break;
        case Events.REMOVE:
            handleRemove(observableCollection, doc);
            break;
        default:
            throw new Meteor.Error(`Invalid event specified: ${event}`)
    }
}

/**
 * @param observableCollection
 * @param doc
 */
const handleInsert = function (observableCollection, doc) {
    if (observableCollection.isEligible(doc)) {
        requery(observableCollection, doc);
    }
};

/**
 * @param observableCollection
 * @param doc
 * @param affectedFields
 */
const handleUpdate = function (observableCollection, doc, affectedFields) {
    const smartObject = new SmartObject(
        doc,
        observableCollection.selector.fields,
        observableCollection.options.sort,
        affectedFields
    );

    if (observableCollection.contains(doc._id)) {
        if (observableCollection.isEligibleByDB(doc._id)) {
            if (smartObject.fieldsExistInSortOptions(affectedFields)) {
                requery(observableCollection, doc, Events.UPDATE);
            } else {
                if (smartObject.fieldsExistInFieldsOptions(affectedFields)) {
                    observableCollection.change(doc._id, doc);
                }
            }
        } else {
            requery(observableCollection);
        }
    } else {
        if (observableCollection.isEligibleByDB(doc._id)) {
            if (smartObject.fieldsExistInSortOptions(affectedFields)) {
                requery(observableCollection, doc, Events.UPDATE);
            } else {
                observableCollection.addById(doc._id);
            }
        }
    }
};

/**
 * @param observableCollection
 * @param doc
 */
const handleRemove = function (observableCollection, doc) {
    if (observableCollection.contains(doc._id)) {
        requery(observableCollection, doc);
    } else {
        if (observableCollection.options.skip) {
            requery(observableCollection, doc)
        }
    }
};
{'fraction_non_alphanumeric': 0.10261736601578729, 'fraction_numerical': 0.0, 'mean_word_length': 2.4740259740259742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5081748', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1240, 'n_words': 260}