Emitter.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Generated by CoffeeScript 1.6.3
  2. var Emitter, array;
  3. array = require('./array');
  4. module.exports = Emitter = (function() {
  5. function Emitter() {
  6. this._listeners = {};
  7. this._listenersForAnyEvent = [];
  8. this._disabledEmitters = {};
  9. }
  10. Emitter.prototype.on = function(eventName, listener) {
  11. if (this._listeners[eventName] == null) {
  12. this._listeners[eventName] = [];
  13. }
  14. this._listeners[eventName].push(listener);
  15. return this;
  16. };
  17. Emitter.prototype.once = function(eventName, listener) {
  18. var cb, ran,
  19. _this = this;
  20. ran = false;
  21. cb = function() {
  22. if (ran) {
  23. return;
  24. }
  25. ran = true;
  26. listener();
  27. return setTimeout(function() {
  28. return _this.removeEvent(eventName, cb);
  29. }, 0);
  30. };
  31. this.on(eventName, cb);
  32. return this;
  33. };
  34. Emitter.prototype.onAnyEvent = function(listener) {
  35. this._listenersForAnyEvent.push(listener);
  36. return this;
  37. };
  38. Emitter.prototype.removeEvent = function(eventName, listener) {
  39. if (this._listeners[eventName] == null) {
  40. return this;
  41. }
  42. array.pluckOneItem(this._listeners[eventName], listener);
  43. return this;
  44. };
  45. Emitter.prototype.removeListeners = function(eventName) {
  46. if (this._listeners[eventName] == null) {
  47. return this;
  48. }
  49. this._listeners[eventName].length = 0;
  50. return this;
  51. };
  52. Emitter.prototype.removeAllListeners = function() {
  53. var listeners, name, _ref;
  54. _ref = this._listeners;
  55. for (name in _ref) {
  56. listeners = _ref[name];
  57. listeners.length = 0;
  58. }
  59. return this;
  60. };
  61. Emitter.prototype._emit = function(eventName, data) {
  62. var listener, _i, _j, _len, _len1, _ref, _ref1;
  63. _ref = this._listenersForAnyEvent;
  64. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  65. listener = _ref[_i];
  66. listener.call(this, data, eventName);
  67. }
  68. if (this._listeners[eventName] == null) {
  69. return;
  70. }
  71. _ref1 = this._listeners[eventName];
  72. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  73. listener = _ref1[_j];
  74. listener.call(this, data);
  75. }
  76. };
  77. Emitter.prototype._throttleEmitterMethod = function(fnName, time) {
  78. var lastCallArgs, originalFn, pend, pending, runIt, timer,
  79. _this = this;
  80. if (time == null) {
  81. time = 1000;
  82. }
  83. originalFn = this[fnName];
  84. if (typeof originalFn !== 'function') {
  85. throw Error("this class does not have a method called '" + fnName + "'");
  86. }
  87. lastCallArgs = null;
  88. pending = false;
  89. timer = null;
  90. this[fnName] = function() {
  91. lastCallArgs = arguments;
  92. return pend();
  93. };
  94. pend = function() {
  95. if (pending) {
  96. clearTimeout(timer);
  97. }
  98. timer = setTimeout(runIt, time);
  99. return pending = true;
  100. };
  101. return runIt = function() {
  102. pending = false;
  103. return originalFn.apply(_this, lastCallArgs);
  104. };
  105. };
  106. Emitter.prototype._disableEmitter = function(fnName) {
  107. if (this._disabledEmitters[fnName] != null) {
  108. throw Error("" + fnName + " is already a disabled emitter");
  109. }
  110. this._disabledEmitters[fnName] = this[fnName];
  111. return this[fnName] = function() {};
  112. };
  113. Emitter.prototype._enableEmitter = function(fnName) {
  114. var fn;
  115. fn = this._disabledEmitters[fnName];
  116. if (fn == null) {
  117. throw Error("" + fnName + " is not a disabled emitter");
  118. }
  119. this[fnName] = fn;
  120. return delete this._disabledEmitters[fnName];
  121. };
  122. return Emitter;
  123. })();