{"size": 1912, "ext": "js", "max_stars_repo_path": "kite-go/curation/cmds/codeexample-author/static/js/lib/events.js", "max_stars_repo_name": "kiteco/kiteco-public", "max_stars_repo_stars_event_min_datetime": "2022-01-10T11:01:50.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T03:21:08.000Z", "max_issues_repo_path": "kite-go/curation/cmds/codeexample-author/static/js/lib/events.js", "max_issues_repo_name": "kiteco/kiteco-public", "max_issues_repo_issues_event_min_datetime": "2022-01-13T14:28:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-13T14:28:47.000Z", "max_forks_repo_path": "kite-go/curation/cmds/codeexample-author/static/js/lib/events.js", "max_forks_repo_name": "kiteco/kiteco-public", "max_forks_repo_forks_event_min_datetime": "2022-01-07T03:58:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T07:38:20.000Z"}
{"max_stars_count": 17, "max_issues_count": 1, "max_forks_count": 7, "avg_line_length": 31.3442622951, "max_line_length": 98, "alphanum_fraction": 0.6558577406}
// Global event system

(function() {

function Dispatcher() {

  this.listeners = {};

  // registers a callback function to call when an event named `eventName` is triggered.
  // returns an id which can be passed to the `deregister` function
  this.register = function(eventName, callback) {
    if (typeof this.listeners[eventName] === 'undefined') {
      this.listeners[eventName] = [];
    }
    var callbackIndex;
    // attempt to find a null in the this.listeners[eventName] array
    for (callbackIndex = 0;  callbackIndex < this.listeners[eventName].length;  callbackIndex++) {
      if (this.listeners[eventName][callbackIndex] === null) {
        // populate a null spot with callback & return this callbackIndex
        this.listeners[eventName][callbackIndex] = callback;
        return {
          eventName: eventName,
          callbackIndex: callbackIndex
        }
      }
    }
    // if we didn't find an empty spot in this.listeners[eventName], just
    // append the callback
    this.listeners[eventName].push(callback);
    return {
      eventName: eventName,
      callbackIndex: this.listeners[eventName].length - 1
    };
  };

  // pass the object returned by register into deregister to remove the callback
  this.deregister = function(callbackId) {
    this.listeners[callbackId.eventName][callbackId.callbackIndex] = null;
  };

  this.trigger = function(eventName, obj) {
    if (typeof this.listeners[eventName] != 'undefined') {
      this.listeners[eventName].forEach(function(callback) {
        if (callback != null) {
          callback(obj);
        }
      });
    }
  };
}

if (typeof module !== 'undefined' && module.exports) { // node (for tests)
  module.exports.Dispatcher = Dispatcher;
} else if (typeof window !== 'undefined') { // browser
  window.Dispatcher = Dispatcher;
} else {
  console.log("I have no idea what environment we are running in.");
}

})();
