<commit_before>var _ = require('./utils.js');


module.exports = function() {
  return {
    listeners: [],

    // Register a handler to be called every time an event happens
    //XXX Support registering multiple events at once
    on: function(action, cb) {
      var res = action.split(':'); ///XXX not needed -> use event:key directly as event name
      var l = {
        action: res[0],
        key: res[1],
        cb: cb
      };
      this.listeners.push(l);
      this.listenerAdded();
      return cb;
    },

    // Remove a handler from all events it was registered for
    off: function(handler) {
      for (var i = this.listeners.length - 1; i >= 0; i--) {
        var l = this.listeners[i];
        if(l.cb === handler) {
          this.listeners.splice(i, 1);
        }
      }
      this.listenerRemoved();
      return this;
    },

    // Register a handler to be called as soon as an event happens
    once: function(action, cb) {
      var self = this;
      var handler = function() {
        self.off(handler);
        cb();
      };
      self.on(action, handler);
      return handler;
    },

    // Call all handlers that listen to this event
    emit: function(action, key) {
      var self = this;
      var listeners = _.clone(self.listeners);
      // _.defer(function() {
        for(var i in listeners) {
          var l = listeners[i];
          if(l.action == action) {
            if(key) {
              if(l.key == key) {
                l.cb();
              }
            } else {
              if(!l.key) {
                l.cb();
              }
            }
          }
        }
      // });
      return self;
    },

    discardEventHandlers: function() {
      this.listeners = [];<commit_msg>Allow passing event data to listeners<commit_after>var _ = require('./utils.js');


module.exports = function() {
  return {
    listeners: [],

    // Register a handler to be called every time an event happens
    //XXX Support registering multiple events at once
    on: function(action, cb) {
      var res = action.split(':'); ///XXX not needed -> use event:key directly as event name
      var l = {
        action: res[0],
        key: res[1],
        cb: cb
      };
      this.listeners.push(l);
      this.listenerAdded();
      return cb;
    },

    // Remove a handler from all events it was registered for
    off: function(handler) {
      for (var i = this.listeners.length - 1; i >= 0; i--) {
        var l = this.listeners[i];
        if(l.cb === handler) {
          this.listeners.splice(i, 1);
        }
      }
      this.listenerRemoved();
      return this;
    },

    // Register a handler to be called as soon as an event happens
    once: function(action, cb) {
      var self = this;
      var handler = function() {
        self.off(handler);
        cb();
      };
      self.on(action, handler);
      return handler;
    },

    // Call all handlers that listen to this event
    emit: function(action, key, data) {
      var self = this;
      data = _.merge({
        action: action,
        object: self,
        property: key
      }, data);
      var listeners = _.clone(self.listeners);
      for(var i in listeners) {
        var l = listeners[i];
        if(l.action == action) {
          if(key) {
            if(l.key == key) {
              l.cb(data);
            }
          } else {
            if(!l.key) {
              l.cb(data);
            }
          }
        }
      }
      return self;
    },

    discardEventHandlers: function() {
      this.listeners = [];
{'fraction_non_alphanumeric': 0.12801796743402583, 'fraction_numerical': 0.002807411566535654, 'mean_word_length': 1.5651547876169907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6459168', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1029, 'n_words': 308}