events.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * events.js - event emitter for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. var slice = Array.prototype.slice;
  7. /**
  8. * EventEmitter
  9. */
  10. function EventEmitter() {
  11. if (!this._events) this._events = {};
  12. }
  13. EventEmitter.prototype.setMaxListeners = function(n) {
  14. this._maxListeners = n;
  15. };
  16. EventEmitter.prototype.addListener = function(type, listener) {
  17. if (!this._events[type]) {
  18. this._events[type] = listener;
  19. } else if (typeof this._events[type] === 'function') {
  20. this._events[type] = [this._events[type], listener];
  21. } else {
  22. this._events[type].push(listener);
  23. }
  24. this._emit('newListener', [type, listener]);
  25. };
  26. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  27. EventEmitter.prototype.removeListener = function(type, listener) {
  28. var handler = this._events[type];
  29. if (!handler) return;
  30. if (typeof handler === 'function' || handler.length === 1) {
  31. delete this._events[type];
  32. this._emit('removeListener', [type, listener]);
  33. return;
  34. }
  35. for (var i = 0; i < handler.length; i++) {
  36. if (handler[i] === listener || handler[i].listener === listener) {
  37. handler.splice(i, 1);
  38. this._emit('removeListener', [type, listener]);
  39. return;
  40. }
  41. }
  42. };
  43. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  44. EventEmitter.prototype.removeAllListeners = function(type) {
  45. if (type) {
  46. delete this._events[type];
  47. } else {
  48. this._events = {};
  49. }
  50. };
  51. EventEmitter.prototype.once = function(type, listener) {
  52. function on() {
  53. this.removeListener(type, on);
  54. return listener.apply(this, arguments);
  55. }
  56. on.listener = listener;
  57. return this.on(type, on);
  58. };
  59. EventEmitter.prototype.listeners = function(type) {
  60. return typeof this._events[type] === 'function'
  61. ? [this._events[type]]
  62. : this._events[type] || [];
  63. };
  64. EventEmitter.prototype._emit = function(type, args) {
  65. var handler = this._events[type]
  66. , ret;
  67. // if (type !== 'event') {
  68. // this._emit('event', [type.replace(/^element /, '')].concat(args));
  69. // }
  70. if (!handler) {
  71. if (type === 'error') {
  72. throw new args[0];
  73. }
  74. return;
  75. }
  76. if (typeof handler === 'function') {
  77. return handler.apply(this, args);
  78. }
  79. for (var i = 0; i < handler.length; i++) {
  80. if (handler[i].apply(this, args) === false) {
  81. ret = false;
  82. }
  83. }
  84. return ret !== false;
  85. };
  86. EventEmitter.prototype.emit = function(type) {
  87. var args = slice.call(arguments, 1)
  88. , params = slice.call(arguments)
  89. , el = this;
  90. this._emit('event', params);
  91. if (this.type === 'screen') {
  92. return this._emit(type, args);
  93. }
  94. if (this._emit(type, args) === false) {
  95. return false;
  96. }
  97. type = 'element ' + type;
  98. args.unshift(this);
  99. // `element` prefix
  100. // params = [type].concat(args);
  101. // no `element` prefix
  102. // params.splice(1, 0, this);
  103. do {
  104. // el._emit('event', params);
  105. if (!el._events[type]) continue;
  106. if (el._emit(type, args) === false) {
  107. return false;
  108. }
  109. } while (el = el.parent);
  110. return true;
  111. };
  112. // For hooking into the main EventEmitter if we want to.
  113. // Might be better to do things this way being that it
  114. // will always be compatible with node, not to mention
  115. // it gives us domain support as well.
  116. // Node.prototype._emit = Node.prototype.emit;
  117. // Node.prototype.emit = function(type) {
  118. // var args, el;
  119. //
  120. // if (this.type === 'screen') {
  121. // return this._emit.apply(this, arguments);
  122. // }
  123. //
  124. // this._emit.apply(this, arguments);
  125. // if (this._bubbleStopped) return false;
  126. //
  127. // args = slice.call(arguments, 1);
  128. // el = this;
  129. //
  130. // args.unshift('element ' + type, this);
  131. // this._bubbleStopped = false;
  132. // //args.push(stopBubble);
  133. //
  134. // do {
  135. // if (!el._events || !el._events[type]) continue;
  136. // el._emit.apply(el, args);
  137. // if (this._bubbleStopped) return false;
  138. // } while (el = el.parent);
  139. //
  140. // return true;
  141. // };
  142. //
  143. // Node.prototype._addListener = Node.prototype.addListener;
  144. // Node.prototype.on =
  145. // Node.prototype.addListener = function(type, listener) {
  146. // function on() {
  147. // if (listener.apply(this, arguments) === false) {
  148. // this._bubbleStopped = true;
  149. // }
  150. // }
  151. // on.listener = listener;
  152. // return this._addListener(type, on);
  153. // };
  154. /**
  155. * Expose
  156. */
  157. exports = EventEmitter;
  158. exports.EventEmitter = EventEmitter;
  159. module.exports = exports;