You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.9 KiB

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