text: // Copyright Joyent, Inc. and other Node contributors.
  //
  // Permission is hereby granted, free of charge, to any person obtaining a
  // copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to permit
  // persons to whom the Software is furnished to do so, subject to the
  // following conditions:
  //
  // The above copyright notice and this permission notice shall be included
  // in all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  // USE OR OTHER DEALINGS IN THE SOFTWARE.
  function EventEmitter() {}
  
  EventEmitter.prototype.emit = function emit(type) {
      var er, handler, len, args, i, listeners;
  
      if (!this._events)
          this._events = {};
  
      // If there is no 'error' event listener then throw.
      if (type === 'error' && !this._events.error) {
          er = arguments[1];
  
          if (er instanceof Error) {
              throw er; // Unhandled 'error' event
          } else {
              throw Error('Uncaught, unspecified "error" event.');
          }
      }
  
      handler = this._events[type];
  
      if (typeof handler === 'undefined')
          return false;
  
      if (typeof handler === 'function') {
          switch (arguments.length) {
              // fast cases
              case 1:
                  handler.call(this);
                  break;
              case 2:
                  handler.call(this, arguments[1]);
                  break;
              case 3:
                  handler.call(this, arguments[1], arguments[2]);
                  break;
              // slower
              default:
                  len = arguments.length;
                  args = new Array(len - 1);
                  for (i = 1; i < len; i++)
                      args[i - 1] = arguments[i];
                  handler.apply(this, args);
          }
      } else if (typeof handler === 'object') {
          len = arguments.length;
          args = new Array(len - 1);
          for (i = 1; i < len; i++)
              args[i - 1] = arguments[i];
  
          listeners = handler.slice();
          len = listeners.length;
          for (i = 0; i < len; i++)
              listeners[i].apply(this, args);
      }
  
      return true;
  };
  
  EventEmitter.prototype.addListener = function addListener(type, listener) {
      var m;
  
      if (typeof listener != 'function')
          throw TypeError('listener must be a function');
  
      if (!this._events)
          this._events = {};
  
      // To avoid recursion in the case that type === "newListener"! Before
      // adding it to the listeners, first emit "newListener".
      if (this._events.newListener)
          this.emit('newListener', type,
                  typeof listener.listener === 'function' ?
                  listener.listener : listener);
  
      if (!this._events[type])
      // Optimize the case of one listener. Don't need the extra array object.
          this._events[type] = listener;
      else if (typeof this._events[type] === 'object')
      // If we've already got an array, just append.
          this._events[type].push(listener);
      else
      // Adding the second element, need to change to array.
          this._events[type] = [this._events[type], listener];
  
      // Check for listener leak
      if (typeof this._events[type] === 'object' && !this._events[type].warned) {
          if (typeof this._maxListeners != 'undefined') {
              m = this._maxListeners;
          } else {
              m = EventEmitter.defaultMaxListeners;
          }
  
          if (m && m > 0 && this._events[type].length > m) {
              this._events[type].warned = true;
              console.error('(node) warning: possible EventEmitter memory ' +
                      'leak detected. %d %s listeners added. ' +
                      'Use emitter.setMaxListeners() to increase limit.',
                  this._events[type].length, type);
              console.trace();
          }
      }
  
      return this;
  };
  
  EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  
  EventEmitter.prototype.once = function once(type, listener) {
      if (typeof listener != 'function')
          throw TypeError('listener must be a function');
  
      var fired = false;
  
      function g() {
          this.removeListener(type, g);
  
          if (!fired) {
              fired = true;
              listener.apply(this, arguments);
          }
      }
  
      g.listener = listener;
      this.on(type, g);
  
      return this;
  };
  
  // emits a 'removeListener' event iff the listener was removed
  EventEmitter.prototype.removeListener =
      function removeListener(type, listener) {
          var list, position, length, i;
  
          if (typeof listener != 'function')
              throw TypeError('listener must be a function');
  
          if (!this._events || !this._events[type])
              return this;
  
          list = this._events[type];
          length = list.length;
          position = -1;
  
          if (list === listener ||
              (typeof list.listener === 'function' && list.listener === listener)) {
              delete this._events[type];
              if (this._events.removeListener)
                  this.emit('removeListener', type, listener);
  
          } else if (typeof list === 'object') {
              for (i = length; i-- > 0;) {
                  if (list[i] === listener ||
                      (list[i].listener && list[i].listener === listener)) {
                      position = i;
                      break;
                  }
              }
  
              if (position < 0)
                  return this;
  
              if (list.length === 1) {
                  list.length = 0;
                  delete this._events[type];
              } else {
                  list.splice(position, 1);
              }
  
              if (this._events.removeListener)
                  this.emit('removeListener', type, listener);
          }
  
          return this;
      };
  
  EventEmitter.prototype.removeAllListeners =
      function removeAllListeners(type) {
          var key, listeners;
  
          if (!this._events)
              return this;
  
          // not listening for removeListener, no need to emit
          if (!this._events.removeListener) {
              if (arguments.length === 0)
                  this._events = {};
              else if (this._events[type])
                  delete this._events[type];
              return this;
          }
  
          // emit removeListener for all listeners on all events
          if (arguments.length === 0) {
              for (key in this._events) {
                  if (key === 'removeListener') continue;
                  this.removeAllListeners(key);
              }
              this.removeAllListeners('removeListener');
              this._events = {};
              return this;
          }
  
          listeners = this._events[type];
  
          if (typeof listeners === 'function') {
              this.removeListener(type, listeners);
          } else if (Array.isArray(listeners)) {
              // LIFO order
              while (listeners.length)
                  this.removeListener(type, listeners[listeners.length - 1]);
          }
          delete this._events[type];
  
          return this;
      };
  
  EventEmitter.prototype.listeners = function listeners(type) {
      var ret;
      if (!this._events || !this._events[type])
          ret = [];
      else if (typeof this._events[type] === 'function')
          ret = [this._events[type]];
      else
          ret = this._events[type].slice();
      return ret;
  };
  
  EventEmitter.listenerCount = function(emitter, type) {
      var ret;
      if (!emitter._events || !emitter._events[type])
          ret = 0;
      else if (typeof emitter._events[type] === 'function')
          ret = 1;
      else
          ret = emitter._events[type].length;
      return ret;
  };
  
  module.exports = EventEmitter;
  