eventemitter2.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*!
  2. * EventEmitter2
  3. * https://github.com/hij1nx/EventEmitter2
  4. *
  5. * Copyright (c) 2013 hij1nx
  6. * Licensed under the MIT license.
  7. */
  8. ;!function(undefined) {
  9. var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
  10. return Object.prototype.toString.call(obj) === "[object Array]";
  11. };
  12. var defaultMaxListeners = 10;
  13. function init() {
  14. this._events = {};
  15. if (this._conf) {
  16. configure.call(this, this._conf);
  17. }
  18. }
  19. function configure(conf) {
  20. if (conf) {
  21. this._conf = conf;
  22. conf.delimiter && (this.delimiter = conf.delimiter);
  23. conf.maxListeners && (this._events.maxListeners = conf.maxListeners);
  24. conf.wildcard && (this.wildcard = conf.wildcard);
  25. conf.newListener && (this.newListener = conf.newListener);
  26. if (this.wildcard) {
  27. this.listenerTree = {};
  28. }
  29. }
  30. }
  31. function EventEmitter(conf) {
  32. this._events = {};
  33. this.newListener = false;
  34. configure.call(this, conf);
  35. }
  36. //
  37. // Attention, function return type now is array, always !
  38. // It has zero elements if no any matches found and one or more
  39. // elements (leafs) if there are matches
  40. //
  41. function searchListenerTree(handlers, type, tree, i) {
  42. if (!tree) {
  43. return [];
  44. }
  45. var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached,
  46. typeLength = type.length, currentType = type[i], nextType = type[i+1];
  47. if (i === typeLength && tree._listeners) {
  48. //
  49. // If at the end of the event(s) list and the tree has listeners
  50. // invoke those listeners.
  51. //
  52. if (typeof tree._listeners === 'function') {
  53. handlers && handlers.push(tree._listeners);
  54. return [tree];
  55. } else {
  56. for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
  57. handlers && handlers.push(tree._listeners[leaf]);
  58. }
  59. return [tree];
  60. }
  61. }
  62. if ((currentType === '*' || currentType === '**') || tree[currentType]) {
  63. //
  64. // If the event emitted is '*' at this part
  65. // or there is a concrete match at this patch
  66. //
  67. if (currentType === '*') {
  68. for (branch in tree) {
  69. if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
  70. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1));
  71. }
  72. }
  73. return listeners;
  74. } else if(currentType === '**') {
  75. endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*'));
  76. if(endReached && tree._listeners) {
  77. // The next element has a _listeners, add it to the handlers.
  78. listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength));
  79. }
  80. for (branch in tree) {
  81. if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
  82. if(branch === '*' || branch === '**') {
  83. if(tree[branch]._listeners && !endReached) {
  84. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength));
  85. }
  86. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
  87. } else if(branch === nextType) {
  88. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2));
  89. } else {
  90. // No match on this one, shift into the tree but not in the type array.
  91. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
  92. }
  93. }
  94. }
  95. return listeners;
  96. }
  97. listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1));
  98. }
  99. xTree = tree['*'];
  100. if (xTree) {
  101. //
  102. // If the listener tree will allow any match for this part,
  103. // then recursively explore all branches of the tree
  104. //
  105. searchListenerTree(handlers, type, xTree, i+1);
  106. }
  107. xxTree = tree['**'];
  108. if(xxTree) {
  109. if(i < typeLength) {
  110. if(xxTree._listeners) {
  111. // If we have a listener on a '**', it will catch all, so add its handler.
  112. searchListenerTree(handlers, type, xxTree, typeLength);
  113. }
  114. // Build arrays of matching next branches and others.
  115. for(branch in xxTree) {
  116. if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
  117. if(branch === nextType) {
  118. // We know the next element will match, so jump twice.
  119. searchListenerTree(handlers, type, xxTree[branch], i+2);
  120. } else if(branch === currentType) {
  121. // Current node matches, move into the tree.
  122. searchListenerTree(handlers, type, xxTree[branch], i+1);
  123. } else {
  124. isolatedBranch = {};
  125. isolatedBranch[branch] = xxTree[branch];
  126. searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1);
  127. }
  128. }
  129. }
  130. } else if(xxTree._listeners) {
  131. // We have reached the end and still on a '**'
  132. searchListenerTree(handlers, type, xxTree, typeLength);
  133. } else if(xxTree['*'] && xxTree['*']._listeners) {
  134. searchListenerTree(handlers, type, xxTree['*'], typeLength);
  135. }
  136. }
  137. return listeners;
  138. }
  139. function growListenerTree(type, listener) {
  140. type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  141. //
  142. // Looks for two consecutive '**', if so, don't add the event at all.
  143. //
  144. for(var i = 0, len = type.length; i+1 < len; i++) {
  145. if(type[i] === '**' && type[i+1] === '**') {
  146. return;
  147. }
  148. }
  149. var tree = this.listenerTree;
  150. var name = type.shift();
  151. while (name) {
  152. if (!tree[name]) {
  153. tree[name] = {};
  154. }
  155. tree = tree[name];
  156. if (type.length === 0) {
  157. if (!tree._listeners) {
  158. tree._listeners = listener;
  159. }
  160. else if(typeof tree._listeners === 'function') {
  161. tree._listeners = [tree._listeners, listener];
  162. }
  163. else if (isArray(tree._listeners)) {
  164. tree._listeners.push(listener);
  165. if (!tree._listeners.warned) {
  166. var m = defaultMaxListeners;
  167. if (typeof this._events.maxListeners !== 'undefined') {
  168. m = this._events.maxListeners;
  169. }
  170. if (m > 0 && tree._listeners.length > m) {
  171. tree._listeners.warned = true;
  172. console.error('(node) warning: possible EventEmitter memory ' +
  173. 'leak detected. %d listeners added. ' +
  174. 'Use emitter.setMaxListeners() to increase limit.',
  175. tree._listeners.length);
  176. console.trace();
  177. }
  178. }
  179. }
  180. return true;
  181. }
  182. name = type.shift();
  183. }
  184. return true;
  185. }
  186. // By default EventEmitters will print a warning if more than
  187. // 10 listeners are added to it. This is a useful default which
  188. // helps finding memory leaks.
  189. //
  190. // Obviously not all Emitters should be limited to 10. This function allows
  191. // that to be increased. Set to zero for unlimited.
  192. EventEmitter.prototype.delimiter = '.';
  193. EventEmitter.prototype.setMaxListeners = function(n) {
  194. this._events || init.call(this);
  195. this._events.maxListeners = n;
  196. if (!this._conf) this._conf = {};
  197. this._conf.maxListeners = n;
  198. };
  199. EventEmitter.prototype.event = '';
  200. EventEmitter.prototype.once = function(event, fn) {
  201. this.many(event, 1, fn);
  202. return this;
  203. };
  204. EventEmitter.prototype.many = function(event, ttl, fn) {
  205. var self = this;
  206. if (typeof fn !== 'function') {
  207. throw new Error('many only accepts instances of Function');
  208. }
  209. function listener() {
  210. if (--ttl === 0) {
  211. self.off(event, listener);
  212. }
  213. fn.apply(this, arguments);
  214. }
  215. listener._origin = fn;
  216. this.on(event, listener);
  217. return self;
  218. };
  219. EventEmitter.prototype.emit = function() {
  220. this._events || init.call(this);
  221. var type = arguments[0];
  222. if (type === 'newListener' && !this.newListener) {
  223. if (!this._events.newListener) { return false; }
  224. }
  225. // Loop through the *_all* functions and invoke them.
  226. if (this._all) {
  227. var l = arguments.length;
  228. var args = new Array(l - 1);
  229. for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
  230. for (i = 0, l = this._all.length; i < l; i++) {
  231. this.event = type;
  232. this._all[i].apply(this, args);
  233. }
  234. }
  235. // If there is no 'error' event listener then throw.
  236. if (type === 'error') {
  237. if (!this._all &&
  238. !this._events.error &&
  239. !(this.wildcard && this.listenerTree.error)) {
  240. if (arguments[1] instanceof Error) {
  241. throw arguments[1]; // Unhandled 'error' event
  242. } else {
  243. throw new Error("Uncaught, unspecified 'error' event.");
  244. }
  245. return false;
  246. }
  247. }
  248. var handler;
  249. if(this.wildcard) {
  250. handler = [];
  251. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  252. searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
  253. }
  254. else {
  255. handler = this._events[type];
  256. }
  257. if (typeof handler === 'function') {
  258. this.event = type;
  259. if (arguments.length === 1) {
  260. handler.call(this);
  261. }
  262. else if (arguments.length > 1)
  263. switch (arguments.length) {
  264. case 2:
  265. handler.call(this, arguments[1]);
  266. break;
  267. case 3:
  268. handler.call(this, arguments[1], arguments[2]);
  269. break;
  270. // slower
  271. default:
  272. var l = arguments.length;
  273. var args = new Array(l - 1);
  274. for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
  275. handler.apply(this, args);
  276. }
  277. return true;
  278. }
  279. else if (handler) {
  280. var l = arguments.length;
  281. var args = new Array(l - 1);
  282. for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
  283. var listeners = handler.slice();
  284. for (var i = 0, l = listeners.length; i < l; i++) {
  285. this.event = type;
  286. listeners[i].apply(this, args);
  287. }
  288. return (listeners.length > 0) || !!this._all;
  289. }
  290. else {
  291. return !!this._all;
  292. }
  293. };
  294. EventEmitter.prototype.on = function(type, listener) {
  295. if (typeof type === 'function') {
  296. this.onAny(type);
  297. return this;
  298. }
  299. if (typeof listener !== 'function') {
  300. throw new Error('on only accepts instances of Function');
  301. }
  302. this._events || init.call(this);
  303. // To avoid recursion in the case that type == "newListeners"! Before
  304. // adding it to the listeners, first emit "newListeners".
  305. this.emit('newListener', type, listener);
  306. if(this.wildcard) {
  307. growListenerTree.call(this, type, listener);
  308. return this;
  309. }
  310. if (!this._events[type]) {
  311. // Optimize the case of one listener. Don't need the extra array object.
  312. this._events[type] = listener;
  313. }
  314. else if(typeof this._events[type] === 'function') {
  315. // Adding the second element, need to change to array.
  316. this._events[type] = [this._events[type], listener];
  317. }
  318. else if (isArray(this._events[type])) {
  319. // If we've already got an array, just append.
  320. this._events[type].push(listener);
  321. // Check for listener leak
  322. if (!this._events[type].warned) {
  323. var m = defaultMaxListeners;
  324. if (typeof this._events.maxListeners !== 'undefined') {
  325. m = this._events.maxListeners;
  326. }
  327. if (m > 0 && this._events[type].length > m) {
  328. this._events[type].warned = true;
  329. console.error('(node) warning: possible EventEmitter memory ' +
  330. 'leak detected. %d listeners added. ' +
  331. 'Use emitter.setMaxListeners() to increase limit.',
  332. this._events[type].length);
  333. console.trace();
  334. }
  335. }
  336. }
  337. return this;
  338. };
  339. EventEmitter.prototype.onAny = function(fn) {
  340. if (typeof fn !== 'function') {
  341. throw new Error('onAny only accepts instances of Function');
  342. }
  343. if(!this._all) {
  344. this._all = [];
  345. }
  346. // Add the function to the event listener collection.
  347. this._all.push(fn);
  348. return this;
  349. };
  350. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  351. EventEmitter.prototype.off = function(type, listener) {
  352. if (typeof listener !== 'function') {
  353. throw new Error('removeListener only takes instances of Function');
  354. }
  355. var handlers,leafs=[];
  356. if(this.wildcard) {
  357. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  358. leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
  359. }
  360. else {
  361. // does not use listeners(), so no side effect of creating _events[type]
  362. if (!this._events[type]) return this;
  363. handlers = this._events[type];
  364. leafs.push({_listeners:handlers});
  365. }
  366. for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
  367. var leaf = leafs[iLeaf];
  368. handlers = leaf._listeners;
  369. if (isArray(handlers)) {
  370. var position = -1;
  371. for (var i = 0, length = handlers.length; i < length; i++) {
  372. if (handlers[i] === listener ||
  373. (handlers[i].listener && handlers[i].listener === listener) ||
  374. (handlers[i]._origin && handlers[i]._origin === listener)) {
  375. position = i;
  376. break;
  377. }
  378. }
  379. if (position < 0) {
  380. continue;
  381. }
  382. if(this.wildcard) {
  383. leaf._listeners.splice(position, 1);
  384. }
  385. else {
  386. this._events[type].splice(position, 1);
  387. }
  388. if (handlers.length === 0) {
  389. if(this.wildcard) {
  390. delete leaf._listeners;
  391. }
  392. else {
  393. delete this._events[type];
  394. }
  395. }
  396. return this;
  397. }
  398. else if (handlers === listener ||
  399. (handlers.listener && handlers.listener === listener) ||
  400. (handlers._origin && handlers._origin === listener)) {
  401. if(this.wildcard) {
  402. delete leaf._listeners;
  403. }
  404. else {
  405. delete this._events[type];
  406. }
  407. }
  408. }
  409. return this;
  410. };
  411. EventEmitter.prototype.offAny = function(fn) {
  412. var i = 0, l = 0, fns;
  413. if (fn && this._all && this._all.length > 0) {
  414. fns = this._all;
  415. for(i = 0, l = fns.length; i < l; i++) {
  416. if(fn === fns[i]) {
  417. fns.splice(i, 1);
  418. return this;
  419. }
  420. }
  421. } else {
  422. this._all = [];
  423. }
  424. return this;
  425. };
  426. EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
  427. EventEmitter.prototype.removeAllListeners = function(type) {
  428. if (arguments.length === 0) {
  429. !this._events || init.call(this);
  430. return this;
  431. }
  432. if(this.wildcard) {
  433. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  434. var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
  435. for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
  436. var leaf = leafs[iLeaf];
  437. leaf._listeners = null;
  438. }
  439. }
  440. else {
  441. if (!this._events[type]) return this;
  442. this._events[type] = null;
  443. }
  444. return this;
  445. };
  446. EventEmitter.prototype.listeners = function(type) {
  447. if(this.wildcard) {
  448. var handlers = [];
  449. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  450. searchListenerTree.call(this, handlers, ns, this.listenerTree, 0);
  451. return handlers;
  452. }
  453. this._events || init.call(this);
  454. if (!this._events[type]) this._events[type] = [];
  455. if (!isArray(this._events[type])) {
  456. this._events[type] = [this._events[type]];
  457. }
  458. return this._events[type];
  459. };
  460. EventEmitter.prototype.listenersAny = function() {
  461. if(this._all) {
  462. return this._all;
  463. }
  464. else {
  465. return [];
  466. }
  467. };
  468. if (typeof define === 'function' && define.amd) {
  469. // AMD. Register as an anonymous module.
  470. define(function() {
  471. return EventEmitter;
  472. });
  473. } else if (typeof exports === 'object') {
  474. // CommonJS
  475. exports.EventEmitter2 = EventEmitter;
  476. }
  477. else {
  478. // Browser global.
  479. window.EventEmitter2 = EventEmitter;
  480. }
  481. }();