Show:
+(function (factory) {
  if (typeof exports === 'undefined') {
    factory(webduino || {});
  } else {
    module.exports = factory;
  }
}(function (scope) {
  'use strict';

  // source:
  // https://raw.githubusercontent.com/Gozala/events/master/events.js
  var proto;

  /**
   * An event emitter in browser.
   *
   * @namespace webduino
   * @class EventEmitter
   * @constructor
   */
  function EventEmitter() {
    this._events = this._events || {};
    this._maxListeners = this._maxListeners || undefined;
  }

  proto = EventEmitter.prototype;
  proto._events = undefined;
  proto._maxListeners = undefined;

  // By default EventEmitters will print a warning if more than 10 listeners are
  // added to it. This is a useful default which helps finding memory leaks.

  /**
   * Default maximum number of listeners (10).
   *
   * @property defaultMaxListeners
   * @type {Number}
   * @static
   */
  EventEmitter.defaultMaxListeners = 10;

  // Obviously not all Emitters should be limited to 10. This function allows
  // that to be increased. Set to zero for unlimited.

  /**
   * Set maximum number of listeners that is allow to bind on an emitter.
   *
   * @method setMaxListeners
   * @param {Number} n Number of listeners.
   */
  proto.setMaxListeners = function (n) {
    if (!isNumber(n) || n < 0 || isNaN(n))
      throw TypeError('n must be a positive number');
    this._maxListeners = n;
    return this;
  };

  /**
   * Emit an event of certain type.
   *
   * @method emit
   * @param {String} type Event type.
   * @param {Object} [object,...] Event object(s).
   */
  proto.emit = function (type) {
    var er, handler, len, args, i, listeners;

    if (!this._events)
      this._events = {};

    // If there is no 'error' event listener then throw.
    // EDIT: Do not throw unhandled 'error' in the browser. (mz)
    // if (type === 'error') {
    //   if (!this._events.error ||
    //     (isObject(this._events.error) && !this._events.error.length)) {
    //     er = arguments[1];
    //     if (er instanceof Error) {
    //       throw er; // Unhandled 'error' event
    //     }
    //     throw TypeError('Uncaught, unspecified "error" event.');
    //   }
    // }

    handler = this._events[type];

    if (isUndefined(handler))
      return false;

    if (isFunction(handler)) {
      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:
        args = Array.prototype.slice.call(arguments, 1);
        handler.apply(this, args);
      }
    } else if (isObject(handler)) {
      args = Array.prototype.slice.call(arguments, 1);
      listeners = handler.slice();
      len = listeners.length;
      for (i = 0; i < len; i++)
        listeners[i].apply(this, args);
    }

    return true;
  };

  /**
   * Add a listener for a certain type of event.
   *
   * @method addListener
   * @param {String} type Event type.
   * @param {Function} listener Event listener.
   */
  proto.addListener = function (type, listener) {
    var m;

    if (!isFunction(listener))
      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,
        isFunction(listener.listener) ?
        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 (isObject(this._events[type]))
    // 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 (isObject(this._events[type]) && !this._events[type].warned) {
      if (!isUndefined(this._maxListeners)) {
        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 listeners added. ' +
          'Use emitter.setMaxListeners() to increase limit.',
          this._events[type].length);
        if (typeof console.trace === 'function') {
          // not supported in IE 10
          console.trace();
        }
      }
    }

    return this;
  };

  /**
   * Alias for EventEmitter.addListener(type, listener)
   *
   * @method on
   * @param {String} type Event type.
   * @param {Function} listener Event listener.
   */
  proto.on = proto.addListener;

  /**
   * Add a one-time listener for a certain type of event.
   *
   * @method once
   * @param {String} type Event type.
   * @param {Function} listener Event listener.
   */
  proto.once = function (type, listener) {
    if (!isFunction(listener))
      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;
  };

  /**
   * Remove a listener for certain type of event.
   *
   * @method removeListener
   * @param {String} type Event type.
   * @param {Function} listener Event listener.
   */
  // emits a 'removeListener' event iff the listener was removed
  proto.removeListener = function (type, listener) {
    var list, position, length, i;

    if (!isFunction(listener))
      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 ||
      (isFunction(list.listener) && list.listener === listener)) {
      delete this._events[type];
      if (this._events.removeListener)
        this.emit('removeListener', type, listener);

    } else if (isObject(list)) {
      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;
  };

  /**
   * Remove all listeners of certain type.
   *
   * @method removeAllListeners
   * @param {String} type Event type.
   */
  proto.removeAllListeners = function (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 (isFunction(listeners)) {
      this.removeListener(type, listeners);
    } else if (listeners) {
      // LIFO order
      while (listeners.length)
        this.removeListener(type, listeners[listeners.length - 1]);
    }
    delete this._events[type];

    return this;
  };

  /**
   * Return the listener list bound to certain type of event.
   *
   * @method listeners
   * @param {String} type Evnet type.
   */
  proto.listeners = function (type) {
    var ret;
    if (!this._events || !this._events[type])
      ret = [];
    else if (isFunction(this._events[type]))
      ret = [this._events[type]];
    else
      ret = this._events[type].slice();
    return ret;
  };

  /**
   * Count the number of listeners of an emitter.
   *
   * @method  listenerCount
   * @param  {webduino.EventEmitter} emitter The EventEmitter instance to count.
   * @param  {String} type Event type.
   * @static
   */
  EventEmitter.listenerCount = function (emitter, type) {
    var ret;
    if (!emitter._events || !emitter._events[type])
      ret = 0;
    else if (isFunction(emitter._events[type]))
      ret = 1;
    else
      ret = emitter._events[type].length;
    return ret;
  };

  function isFunction(arg) {
    return typeof arg === 'function';
  }

  function isNumber(arg) {
    return typeof arg === 'number';
  }

  function isObject(arg) {
    return typeof arg === 'object' && arg !== null;
  }

  function isUndefined(arg) {
    return arg === void 0;
  }

  scope.EventEmitter = EventEmitter;
}));