arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/node_modules/ultron/index.js
server/node_modules/ultron/index.jsRaw
  1'use strict';
  2
  3var has = Object.prototype.hasOwnProperty;
  4
  5/**
  6 * An auto incrementing id which we can use to create "unique" Ultron instances
  7 * so we can track the event emitters that are added through the Ultron
  8 * interface.
  9 *
 10 * @type {Number}
 11 * @private
 12 */
 13var id = 0;
 14
 15/**
 16 * Ultron is high-intelligence robot. It gathers intelligence so it can start improving
 17 * upon his rudimentary design. It will learn from your EventEmitting patterns
 18 * and exterminate them.
 19 *
 20 * @constructor
 21 * @param {EventEmitter} ee EventEmitter instance we need to wrap.
 22 * @api public
 23 */
 24function Ultron(ee) {
 25  if (!(this instanceof Ultron)) return new Ultron(ee);
 26
 27  this.id = id++;
 28  this.ee = ee;
 29}
 30
 31/**
 32 * Register a new EventListener for the given event.
 33 *
 34 * @param {String} event Name of the event.
 35 * @param {Functon} fn Callback function.
 36 * @param {Mixed} context The context of the function.
 37 * @returns {Ultron}
 38 * @api public
 39 */
 40Ultron.prototype.on = function on(event, fn, context) {
 41  fn.__ultron = this.id;
 42  this.ee.on(event, fn, context);
 43
 44  return this;
 45};
 46/**
 47 * Add an EventListener that's only called once.
 48 *
 49 * @param {String} event Name of the event.
 50 * @param {Function} fn Callback function.
 51 * @param {Mixed} context The context of the function.
 52 * @returns {Ultron}
 53 * @api public
 54 */
 55Ultron.prototype.once = function once(event, fn, context) {
 56  fn.__ultron = this.id;
 57  this.ee.once(event, fn, context);
 58
 59  return this;
 60};
 61
 62/**
 63 * Remove the listeners we assigned for the given event.
 64 *
 65 * @returns {Ultron}
 66 * @api public
 67 */
 68Ultron.prototype.remove = function remove() {
 69  var args = arguments
 70    , event;
 71
 72  //
 73  // When no event names are provided we assume that we need to clear all the
 74  // events that were assigned through us.
 75  //
 76  if (args.length === 1 && 'string' === typeof args[0]) {
 77    args = args[0].split(/[, ]+/);
 78  } else if (!args.length) {
 79    args = [];
 80
 81    for (event in this.ee._events) {
 82      if (has.call(this.ee._events, event)) args.push(event);
 83    }
 84  }
 85
 86  for (var i = 0; i < args.length; i++) {
 87    var listeners = this.ee.listeners(args[i]);
 88
 89    for (var j = 0; j < listeners.length; j++) {
 90      event = listeners[j];
 91
 92      //
 93      // Once listeners have a `listener` property that stores the real listener
 94      // in the EventEmitter that ships with Node.js.
 95      //
 96      if (event.listener) {
 97        if (event.listener.__ultron !== this.id) continue;
 98        delete event.listener.__ultron;
 99      } else {
100        if (event.__ultron !== this.id) continue;
101        delete event.__ultron;
102      }
103
104      this.ee.removeListener(args[i], event);
105    }
106  }
107
108  return this;
109};
110
111/**
112 * Destroy the Ultron instance, remove all listeners and release all references.
113 *
114 * @returns {Boolean}
115 * @api public
116 */
117Ultron.prototype.destroy = function destroy() {
118  if (!this.ee) return false;
119
120  this.remove();
121  this.ee = null;
122
123  return true;
124};
125
126//
127// Expose the module.
128//
129module.exports = Ultron;