<commit_before>/**
 * Base class of an event emitter.
 * @constructor
 */
function EventEmitter() {
    this.listeners_ = {};
    this.onceListeners_ = {};
}


/**
 * Returns array of registered callbacks for the given event.
 * @param {string} event
 * @private
 */
EventEmitter.prototype.getListeners_ = function(event, once) {
    var listeners = once ? this.onceListeners_ : this.listeners_;

    if (!(event in listeners)) {
        listeners[event] = [];
    }

    return listeners[event];
};


/**
 * Registers callback to event.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.on = function(event, callback) {
    this.getListeners_(event).push(callback);
};


/**
 * Registers callback to event. Callback should only be fired once.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.once = function(event, callback) {
    this.getListeners_(event, true /* once */).push(callback);
};


/**
 * Remove callback for event.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.off = function(event, callback) {
    var callbacks, idx;

    callbacks = this.getListeners_(event);
    idx = callbacks.indexOf(callback)
    if (idx != -1) {
        callbacks.splice(idx, 1);
    }

    callbacks = this.getListeners_(event, true /* once */);
    idx = callbacks.indexOf(callback)
    if (idx != -1) {
        callbacks.splice(idx, 1);
    }
};


/**
 * Emit event.
 * Calls all registered callbacks with the provided arguments
 * @param {string} event
 * @param {...} args Arguments to pass on to the callback.
 */
EventEmitter.prototype.emit = function(event) {
    var callbacks, callback;

    callbacks = this.getListeners_(event);
    for (var i = 0; i < callbacks.length; i++) {
        callback = callbacks[i];
        callback.apply(null, Array.prototype.slice.call(arguments, 1));
    }

    callbacks = this.getListeners_(event, true /* once */);
    while (callbacks.length) {
        callback = callbacks.pop();
        callback.apply(null, Array.prototype.slice.call(arguments, 1));
    }
};


/** Aliases for methods. */
EventEmitter.prototype.listen = EventEmitter.prototype.on;
EventEmitter.prototype.listenOnce = EventEmitter.prototype.once;
EventEmitter.prototype.unlisten = EventEmitter.prototype.off;
EventEmitter.prototype.trigger = EventEmitter.prototype.emit;<commit_msg>Simplify logic for .once listeners.<commit_after>/**
 * Base class of an event emitter.
 * @constructor
 */
function EventEmitter() {
    this.listeners_ = {};
}


/**
 * Returns array of registered callbacks for the given event.
 * @param {string} event
 * @private
 */
EventEmitter.prototype.getListeners_ = function(event) {
    if (!(event in this.listeners_)) {
        this.listeners_[event] = [];
    }

    return this.listeners_[event];
};


/**
 * Registers callback to event.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.on = function(event, callback) {
    this.getListeners_(event).push(callback);
};


/**
 * Registers callback to event. Callback should only be fired once.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.once = function(event, callback) {
    // Wrap callback in a function that removes this listener when fired.
    var wrapped = function() {
        this.off(event, wrapped);
        return callback.apply(null, arguments);
    }.bind(this);
    wrapped.func = callback;

    this.getListeners_(event).push(wrapped);
};


/**
 * Remove callback for event.
 * @param {string} event
 * @param {function} callback
 */
EventEmitter.prototype.off = function(event, callback) {
    var callbacks = this.getListeners_(event);

    for (var i = 0; i < callbacks.length; i++) {
        var cb = callbacks[i];
        if (cb == callback || cb.func == callback) {
            callbacks.splice(i, 1);
            break;
        }
    }
};


/**
 * Emit event.
 * Calls all registered callbacks with the provided arguments
 * @param {string} event
 * @param {...} args Arguments to pass on to the callback.
 */
EventEmitter.prototype.emit = function(event) {
    var callbacks = this.getListeners_(event);
    for (var i = 0; i < callbacks.length; i++) {
        var cb = callbacks[i];
        cb.apply(null, Array.prototype.slice.call(arguments, 1));
    }
};


/** Aliases for methods. */
EventEmitter.prototype.listen = EventEmitter.prototype.on;
EventEmitter.prototype.listenOnce = EventEmitter.prototype.once;
EventEmitter.prototype.unlisten = EventEmitter.prototype.off;
EventEmitter.prototype.trigger = EventEmitter.prototype.emit;
{'fraction_non_alphanumeric': 0.13719974031594892, 'fraction_numerical': 0.0023804371348193033, 'mean_word_length': 3.3075489282385835, 'pattern_counts': {'":': 0, '<': 6, '<?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': '3906271', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1376, 'n_words': 353}