events.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. function EventEmitter() {
  22. this._events = this._events || {};
  23. this._maxListeners = this._maxListeners || undefined;
  24. }
  25. module.exports = EventEmitter;
  26. // Backwards-compat with node 0.10.x
  27. EventEmitter.EventEmitter = EventEmitter;
  28. EventEmitter.prototype._events = undefined;
  29. EventEmitter.prototype._maxListeners = undefined;
  30. // By default EventEmitters will print a warning if more than 10 listeners are
  31. // added to it. This is a useful default which helps finding memory leaks.
  32. EventEmitter.defaultMaxListeners = 10;
  33. // Obviously not all Emitters should be limited to 10. This function allows
  34. // that to be increased. Set to zero for unlimited.
  35. EventEmitter.prototype.setMaxListeners = function(n) {
  36. if (!isNumber(n) || n < 0 || isNaN(n))
  37. throw TypeError('n must be a positive number');
  38. this._maxListeners = n;
  39. return this;
  40. };
  41. EventEmitter.prototype.emit = function(type) {
  42. var er, handler, len, args, i, listeners;
  43. if (!this._events)
  44. this._events = {};
  45. // If there is no 'error' event listener then throw.
  46. if (type === 'error') {
  47. if (!this._events.error ||
  48. (isObject(this._events.error) && !this._events.error.length)) {
  49. er = arguments[1];
  50. if (er instanceof Error) {
  51. throw er; // Unhandled 'error' event
  52. } else {
  53. // At least give some kind of context to the user
  54. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  55. err.context = er;
  56. throw err;
  57. }
  58. }
  59. }
  60. handler = this._events[type];
  61. if (isUndefined(handler))
  62. return false;
  63. if (isFunction(handler)) {
  64. switch (arguments.length) {
  65. // fast cases
  66. case 1:
  67. handler.call(this);
  68. break;
  69. case 2:
  70. handler.call(this, arguments[1]);
  71. break;
  72. case 3:
  73. handler.call(this, arguments[1], arguments[2]);
  74. break;
  75. // slower
  76. default:
  77. args = Array.prototype.slice.call(arguments, 1);
  78. handler.apply(this, args);
  79. }
  80. } else if (isObject(handler)) {
  81. args = Array.prototype.slice.call(arguments, 1);
  82. listeners = handler.slice();
  83. len = listeners.length;
  84. for (i = 0; i < len; i++)
  85. listeners[i].apply(this, args);
  86. }
  87. return true;
  88. };
  89. EventEmitter.prototype.addListener = function(type, listener) {
  90. var m;
  91. if (!isFunction(listener))
  92. throw TypeError('listener must be a function');
  93. if (!this._events)
  94. this._events = {};
  95. // To avoid recursion in the case that type === "newListener"! Before
  96. // adding it to the listeners, first emit "newListener".
  97. if (this._events.newListener)
  98. this.emit('newListener', type,
  99. isFunction(listener.listener) ?
  100. listener.listener : listener);
  101. if (!this._events[type])
  102. // Optimize the case of one listener. Don't need the extra array object.
  103. this._events[type] = listener;
  104. else if (isObject(this._events[type]))
  105. // If we've already got an array, just append.
  106. this._events[type].push(listener);
  107. else
  108. // Adding the second element, need to change to array.
  109. this._events[type] = [this._events[type], listener];
  110. // Check for listener leak
  111. if (isObject(this._events[type]) && !this._events[type].warned) {
  112. if (!isUndefined(this._maxListeners)) {
  113. m = this._maxListeners;
  114. } else {
  115. m = EventEmitter.defaultMaxListeners;
  116. }
  117. if (m && m > 0 && this._events[type].length > m) {
  118. this._events[type].warned = true;
  119. console.error('(node) warning: possible EventEmitter memory ' +
  120. 'leak detected. %d listeners added. ' +
  121. 'Use emitter.setMaxListeners() to increase limit.',
  122. this._events[type].length);
  123. if (typeof console.trace === 'function') {
  124. // not supported in IE 10
  125. console.trace();
  126. }
  127. }
  128. }
  129. return this;
  130. };
  131. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  132. EventEmitter.prototype.once = function(type, listener) {
  133. if (!isFunction(listener))
  134. throw TypeError('listener must be a function');
  135. var fired = false;
  136. function g() {
  137. this.removeListener(type, g);
  138. if (!fired) {
  139. fired = true;
  140. listener.apply(this, arguments);
  141. }
  142. }
  143. g.listener = listener;
  144. this.on(type, g);
  145. return this;
  146. };
  147. // emits a 'removeListener' event iff the listener was removed
  148. EventEmitter.prototype.removeListener = function(type, listener) {
  149. var list, position, length, i;
  150. if (!isFunction(listener))
  151. throw TypeError('listener must be a function');
  152. if (!this._events || !this._events[type])
  153. return this;
  154. list = this._events[type];
  155. length = list.length;
  156. position = -1;
  157. if (list === listener ||
  158. (isFunction(list.listener) && list.listener === listener)) {
  159. delete this._events[type];
  160. if (this._events.removeListener)
  161. this.emit('removeListener', type, listener);
  162. } else if (isObject(list)) {
  163. for (i = length; i-- > 0;) {
  164. if (list[i] === listener ||
  165. (list[i].listener && list[i].listener === listener)) {
  166. position = i;
  167. break;
  168. }
  169. }
  170. if (position < 0)
  171. return this;
  172. if (list.length === 1) {
  173. list.length = 0;
  174. delete this._events[type];
  175. } else {
  176. list.splice(position, 1);
  177. }
  178. if (this._events.removeListener)
  179. this.emit('removeListener', type, listener);
  180. }
  181. return this;
  182. };
  183. EventEmitter.prototype.removeAllListeners = function(type) {
  184. var key, listeners;
  185. if (!this._events)
  186. return this;
  187. // not listening for removeListener, no need to emit
  188. if (!this._events.removeListener) {
  189. if (arguments.length === 0)
  190. this._events = {};
  191. else if (this._events[type])
  192. delete this._events[type];
  193. return this;
  194. }
  195. // emit removeListener for all listeners on all events
  196. if (arguments.length === 0) {
  197. for (key in this._events) {
  198. if (key === 'removeListener') continue;
  199. this.removeAllListeners(key);
  200. }
  201. this.removeAllListeners('removeListener');
  202. this._events = {};
  203. return this;
  204. }
  205. listeners = this._events[type];
  206. if (isFunction(listeners)) {
  207. this.removeListener(type, listeners);
  208. } else if (listeners) {
  209. // LIFO order
  210. while (listeners.length)
  211. this.removeListener(type, listeners[listeners.length - 1]);
  212. }
  213. delete this._events[type];
  214. return this;
  215. };
  216. EventEmitter.prototype.listeners = function(type) {
  217. var ret;
  218. if (!this._events || !this._events[type])
  219. ret = [];
  220. else if (isFunction(this._events[type]))
  221. ret = [this._events[type]];
  222. else
  223. ret = this._events[type].slice();
  224. return ret;
  225. };
  226. EventEmitter.prototype.listenerCount = function(type) {
  227. if (this._events) {
  228. var evlistener = this._events[type];
  229. if (isFunction(evlistener))
  230. return 1;
  231. else if (evlistener)
  232. return evlistener.length;
  233. }
  234. return 0;
  235. };
  236. EventEmitter.listenerCount = function(emitter, type) {
  237. return emitter.listenerCount(type);
  238. };
  239. function isFunction(arg) {
  240. return typeof arg === 'function';
  241. }
  242. function isNumber(arg) {
  243. return typeof arg === 'number';
  244. }
  245. function isObject(arg) {
  246. return typeof arg === 'object' && arg !== null;
  247. }
  248. function isUndefined(arg) {
  249. return arg === void 0;
  250. }