eventemitter2.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. this._maxListeners = conf.maxListeners !== undefined ? conf.maxListeners : defaultMaxListeners;
  24. conf.wildcard && (this.wildcard = conf.wildcard);
  25. conf.newListener && (this._newListener = conf.newListener);
  26. conf.removeListener && (this._removeListener = conf.removeListener);
  27. conf.verboseMemoryLeak && (this.verboseMemoryLeak = conf.verboseMemoryLeak);
  28. if (this.wildcard) {
  29. this.listenerTree = {};
  30. }
  31. } else {
  32. this._maxListeners = defaultMaxListeners;
  33. }
  34. }
  35. function logPossibleMemoryLeak(count, eventName) {
  36. var errorMsg = '(node) warning: possible EventEmitter memory ' +
  37. 'leak detected. ' + count + ' listeners added. ' +
  38. 'Use emitter.setMaxListeners() to increase limit.';
  39. if(this.verboseMemoryLeak){
  40. errorMsg += ' Event name: ' + eventName + '.';
  41. }
  42. if(typeof process !== 'undefined' && process.emitWarning){
  43. var e = new Error(errorMsg);
  44. e.name = 'MaxListenersExceededWarning';
  45. e.emitter = this;
  46. e.count = count;
  47. process.emitWarning(e);
  48. } else {
  49. console.error(errorMsg);
  50. if (console.trace){
  51. console.trace();
  52. }
  53. }
  54. }
  55. function EventEmitter(conf) {
  56. this._events = {};
  57. this._newListener = false;
  58. this._removeListener = false;
  59. this.verboseMemoryLeak = false;
  60. configure.call(this, conf);
  61. }
  62. EventEmitter.EventEmitter2 = EventEmitter; // backwards compatibility for exporting EventEmitter property
  63. //
  64. // Attention, function return type now is array, always !
  65. // It has zero elements if no any matches found and one or more
  66. // elements (leafs) if there are matches
  67. //
  68. function searchListenerTree(handlers, type, tree, i) {
  69. if (!tree) {
  70. return [];
  71. }
  72. var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached,
  73. typeLength = type.length, currentType = type[i], nextType = type[i+1];
  74. if (i === typeLength && tree._listeners) {
  75. //
  76. // If at the end of the event(s) list and the tree has listeners
  77. // invoke those listeners.
  78. //
  79. if (typeof tree._listeners === 'function') {
  80. handlers && handlers.push(tree._listeners);
  81. return [tree];
  82. } else {
  83. for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
  84. handlers && handlers.push(tree._listeners[leaf]);
  85. }
  86. return [tree];
  87. }
  88. }
  89. if ((currentType === '*' || currentType === '**') || tree[currentType]) {
  90. //
  91. // If the event emitted is '*' at this part
  92. // or there is a concrete match at this patch
  93. //
  94. if (currentType === '*') {
  95. for (branch in tree) {
  96. if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
  97. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1));
  98. }
  99. }
  100. return listeners;
  101. } else if(currentType === '**') {
  102. endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*'));
  103. if(endReached && tree._listeners) {
  104. // The next element has a _listeners, add it to the handlers.
  105. listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength));
  106. }
  107. for (branch in tree) {
  108. if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
  109. if(branch === '*' || branch === '**') {
  110. if(tree[branch]._listeners && !endReached) {
  111. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength));
  112. }
  113. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
  114. } else if(branch === nextType) {
  115. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2));
  116. } else {
  117. // No match on this one, shift into the tree but not in the type array.
  118. listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
  119. }
  120. }
  121. }
  122. return listeners;
  123. }
  124. listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1));
  125. }
  126. xTree = tree['*'];
  127. if (xTree) {
  128. //
  129. // If the listener tree will allow any match for this part,
  130. // then recursively explore all branches of the tree
  131. //
  132. searchListenerTree(handlers, type, xTree, i+1);
  133. }
  134. xxTree = tree['**'];
  135. if(xxTree) {
  136. if(i < typeLength) {
  137. if(xxTree._listeners) {
  138. // If we have a listener on a '**', it will catch all, so add its handler.
  139. searchListenerTree(handlers, type, xxTree, typeLength);
  140. }
  141. // Build arrays of matching next branches and others.
  142. for(branch in xxTree) {
  143. if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
  144. if(branch === nextType) {
  145. // We know the next element will match, so jump twice.
  146. searchListenerTree(handlers, type, xxTree[branch], i+2);
  147. } else if(branch === currentType) {
  148. // Current node matches, move into the tree.
  149. searchListenerTree(handlers, type, xxTree[branch], i+1);
  150. } else {
  151. isolatedBranch = {};
  152. isolatedBranch[branch] = xxTree[branch];
  153. searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1);
  154. }
  155. }
  156. }
  157. } else if(xxTree._listeners) {
  158. // We have reached the end and still on a '**'
  159. searchListenerTree(handlers, type, xxTree, typeLength);
  160. } else if(xxTree['*'] && xxTree['*']._listeners) {
  161. searchListenerTree(handlers, type, xxTree['*'], typeLength);
  162. }
  163. }
  164. return listeners;
  165. }
  166. function growListenerTree(type, listener) {
  167. type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  168. //
  169. // Looks for two consecutive '**', if so, don't add the event at all.
  170. //
  171. for(var i = 0, len = type.length; i+1 < len; i++) {
  172. if(type[i] === '**' && type[i+1] === '**') {
  173. return;
  174. }
  175. }
  176. var tree = this.listenerTree;
  177. var name = type.shift();
  178. while (name !== undefined) {
  179. if (!tree[name]) {
  180. tree[name] = {};
  181. }
  182. tree = tree[name];
  183. if (type.length === 0) {
  184. if (!tree._listeners) {
  185. tree._listeners = listener;
  186. }
  187. else {
  188. if (typeof tree._listeners === 'function') {
  189. tree._listeners = [tree._listeners];
  190. }
  191. tree._listeners.push(listener);
  192. if (
  193. !tree._listeners.warned &&
  194. this._maxListeners > 0 &&
  195. tree._listeners.length > this._maxListeners
  196. ) {
  197. tree._listeners.warned = true;
  198. logPossibleMemoryLeak.call(this, tree._listeners.length, name);
  199. }
  200. }
  201. return true;
  202. }
  203. name = type.shift();
  204. }
  205. return true;
  206. }
  207. // By default EventEmitters will print a warning if more than
  208. // 10 listeners are added to it. This is a useful default which
  209. // helps finding memory leaks.
  210. //
  211. // Obviously not all Emitters should be limited to 10. This function allows
  212. // that to be increased. Set to zero for unlimited.
  213. EventEmitter.prototype.delimiter = '.';
  214. EventEmitter.prototype.setMaxListeners = function(n) {
  215. if (n !== undefined) {
  216. this._maxListeners = n;
  217. if (!this._conf) this._conf = {};
  218. this._conf.maxListeners = n;
  219. }
  220. };
  221. EventEmitter.prototype.event = '';
  222. EventEmitter.prototype.once = function(event, fn) {
  223. return this._once(event, fn, false);
  224. };
  225. EventEmitter.prototype.prependOnceListener = function(event, fn) {
  226. return this._once(event, fn, true);
  227. };
  228. EventEmitter.prototype._once = function(event, fn, prepend) {
  229. this._many(event, 1, fn, prepend);
  230. return this;
  231. };
  232. EventEmitter.prototype.many = function(event, ttl, fn) {
  233. return this._many(event, ttl, fn, false);
  234. }
  235. EventEmitter.prototype.prependMany = function(event, ttl, fn) {
  236. return this._many(event, ttl, fn, true);
  237. }
  238. EventEmitter.prototype._many = function(event, ttl, fn, prepend) {
  239. var self = this;
  240. if (typeof fn !== 'function') {
  241. throw new Error('many only accepts instances of Function');
  242. }
  243. function listener() {
  244. if (--ttl === 0) {
  245. self.off(event, listener);
  246. }
  247. return fn.apply(this, arguments);
  248. }
  249. listener._origin = fn;
  250. this._on(event, listener, prepend);
  251. return self;
  252. };
  253. EventEmitter.prototype.emit = function() {
  254. this._events || init.call(this);
  255. var type = arguments[0];
  256. if (type === 'newListener' && !this._newListener) {
  257. if (!this._events.newListener) {
  258. return false;
  259. }
  260. }
  261. var al = arguments.length;
  262. var args,l,i,j;
  263. var handler;
  264. if (this._all && this._all.length) {
  265. handler = this._all.slice();
  266. if (al > 3) {
  267. args = new Array(al);
  268. for (j = 0; j < al; j++) args[j] = arguments[j];
  269. }
  270. for (i = 0, l = handler.length; i < l; i++) {
  271. this.event = type;
  272. switch (al) {
  273. case 1:
  274. handler[i].call(this, type);
  275. break;
  276. case 2:
  277. handler[i].call(this, type, arguments[1]);
  278. break;
  279. case 3:
  280. handler[i].call(this, type, arguments[1], arguments[2]);
  281. break;
  282. default:
  283. handler[i].apply(this, args);
  284. }
  285. }
  286. }
  287. if (this.wildcard) {
  288. handler = [];
  289. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  290. searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
  291. } else {
  292. handler = this._events[type];
  293. if (typeof handler === 'function') {
  294. this.event = type;
  295. switch (al) {
  296. case 1:
  297. handler.call(this);
  298. break;
  299. case 2:
  300. handler.call(this, arguments[1]);
  301. break;
  302. case 3:
  303. handler.call(this, arguments[1], arguments[2]);
  304. break;
  305. default:
  306. args = new Array(al - 1);
  307. for (j = 1; j < al; j++) args[j - 1] = arguments[j];
  308. handler.apply(this, args);
  309. }
  310. return true;
  311. } else if (handler) {
  312. // need to make copy of handlers because list can change in the middle
  313. // of emit call
  314. handler = handler.slice();
  315. }
  316. }
  317. if (handler && handler.length) {
  318. if (al > 3) {
  319. args = new Array(al - 1);
  320. for (j = 1; j < al; j++) args[j - 1] = arguments[j];
  321. }
  322. for (i = 0, l = handler.length; i < l; i++) {
  323. this.event = type;
  324. switch (al) {
  325. case 1:
  326. handler[i].call(this);
  327. break;
  328. case 2:
  329. handler[i].call(this, arguments[1]);
  330. break;
  331. case 3:
  332. handler[i].call(this, arguments[1], arguments[2]);
  333. break;
  334. default:
  335. handler[i].apply(this, args);
  336. }
  337. }
  338. return true;
  339. } else if (!this._all && type === 'error') {
  340. if (arguments[1] instanceof Error) {
  341. throw arguments[1]; // Unhandled 'error' event
  342. } else {
  343. throw new Error("Uncaught, unspecified 'error' event.");
  344. }
  345. return false;
  346. }
  347. return !!this._all;
  348. };
  349. EventEmitter.prototype.emitAsync = function() {
  350. this._events || init.call(this);
  351. var type = arguments[0];
  352. if (type === 'newListener' && !this._newListener) {
  353. if (!this._events.newListener) { return Promise.resolve([false]); }
  354. }
  355. var promises= [];
  356. var al = arguments.length;
  357. var args,l,i,j;
  358. var handler;
  359. if (this._all) {
  360. if (al > 3) {
  361. args = new Array(al);
  362. for (j = 1; j < al; j++) args[j] = arguments[j];
  363. }
  364. for (i = 0, l = this._all.length; i < l; i++) {
  365. this.event = type;
  366. switch (al) {
  367. case 1:
  368. promises.push(this._all[i].call(this, type));
  369. break;
  370. case 2:
  371. promises.push(this._all[i].call(this, type, arguments[1]));
  372. break;
  373. case 3:
  374. promises.push(this._all[i].call(this, type, arguments[1], arguments[2]));
  375. break;
  376. default:
  377. promises.push(this._all[i].apply(this, args));
  378. }
  379. }
  380. }
  381. if (this.wildcard) {
  382. handler = [];
  383. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  384. searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
  385. } else {
  386. handler = this._events[type];
  387. }
  388. if (typeof handler === 'function') {
  389. this.event = type;
  390. switch (al) {
  391. case 1:
  392. promises.push(handler.call(this));
  393. break;
  394. case 2:
  395. promises.push(handler.call(this, arguments[1]));
  396. break;
  397. case 3:
  398. promises.push(handler.call(this, arguments[1], arguments[2]));
  399. break;
  400. default:
  401. args = new Array(al - 1);
  402. for (j = 1; j < al; j++) args[j - 1] = arguments[j];
  403. promises.push(handler.apply(this, args));
  404. }
  405. } else if (handler && handler.length) {
  406. handler = handler.slice();
  407. if (al > 3) {
  408. args = new Array(al - 1);
  409. for (j = 1; j < al; j++) args[j - 1] = arguments[j];
  410. }
  411. for (i = 0, l = handler.length; i < l; i++) {
  412. this.event = type;
  413. switch (al) {
  414. case 1:
  415. promises.push(handler[i].call(this));
  416. break;
  417. case 2:
  418. promises.push(handler[i].call(this, arguments[1]));
  419. break;
  420. case 3:
  421. promises.push(handler[i].call(this, arguments[1], arguments[2]));
  422. break;
  423. default:
  424. promises.push(handler[i].apply(this, args));
  425. }
  426. }
  427. } else if (!this._all && type === 'error') {
  428. if (arguments[1] instanceof Error) {
  429. return Promise.reject(arguments[1]); // Unhandled 'error' event
  430. } else {
  431. return Promise.reject("Uncaught, unspecified 'error' event.");
  432. }
  433. }
  434. return Promise.all(promises);
  435. };
  436. EventEmitter.prototype.on = function(type, listener) {
  437. return this._on(type, listener, false);
  438. };
  439. EventEmitter.prototype.prependListener = function(type, listener) {
  440. return this._on(type, listener, true);
  441. };
  442. EventEmitter.prototype.onAny = function(fn) {
  443. return this._onAny(fn, false);
  444. };
  445. EventEmitter.prototype.prependAny = function(fn) {
  446. return this._onAny(fn, true);
  447. };
  448. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  449. EventEmitter.prototype._onAny = function(fn, prepend){
  450. if (typeof fn !== 'function') {
  451. throw new Error('onAny only accepts instances of Function');
  452. }
  453. if (!this._all) {
  454. this._all = [];
  455. }
  456. // Add the function to the event listener collection.
  457. if(prepend){
  458. this._all.unshift(fn);
  459. }else{
  460. this._all.push(fn);
  461. }
  462. return this;
  463. }
  464. EventEmitter.prototype._on = function(type, listener, prepend) {
  465. if (typeof type === 'function') {
  466. this._onAny(type, listener);
  467. return this;
  468. }
  469. if (typeof listener !== 'function') {
  470. throw new Error('on only accepts instances of Function');
  471. }
  472. this._events || init.call(this);
  473. // To avoid recursion in the case that type == "newListeners"! Before
  474. // adding it to the listeners, first emit "newListeners".
  475. if (this._newListener)
  476. this.emit('newListener', type, listener);
  477. if (this.wildcard) {
  478. growListenerTree.call(this, type, listener);
  479. return this;
  480. }
  481. if (!this._events[type]) {
  482. // Optimize the case of one listener. Don't need the extra array object.
  483. this._events[type] = listener;
  484. }
  485. else {
  486. if (typeof this._events[type] === 'function') {
  487. // Change to array.
  488. this._events[type] = [this._events[type]];
  489. }
  490. // If we've already got an array, just add
  491. if(prepend){
  492. this._events[type].unshift(listener);
  493. }else{
  494. this._events[type].push(listener);
  495. }
  496. // Check for listener leak
  497. if (
  498. !this._events[type].warned &&
  499. this._maxListeners > 0 &&
  500. this._events[type].length > this._maxListeners
  501. ) {
  502. this._events[type].warned = true;
  503. logPossibleMemoryLeak.call(this, this._events[type].length, type);
  504. }
  505. }
  506. return this;
  507. }
  508. EventEmitter.prototype.off = function(type, listener) {
  509. if (typeof listener !== 'function') {
  510. throw new Error('removeListener only takes instances of Function');
  511. }
  512. var handlers,leafs=[];
  513. if(this.wildcard) {
  514. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  515. leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
  516. }
  517. else {
  518. // does not use listeners(), so no side effect of creating _events[type]
  519. if (!this._events[type]) return this;
  520. handlers = this._events[type];
  521. leafs.push({_listeners:handlers});
  522. }
  523. for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
  524. var leaf = leafs[iLeaf];
  525. handlers = leaf._listeners;
  526. if (isArray(handlers)) {
  527. var position = -1;
  528. for (var i = 0, length = handlers.length; i < length; i++) {
  529. if (handlers[i] === listener ||
  530. (handlers[i].listener && handlers[i].listener === listener) ||
  531. (handlers[i]._origin && handlers[i]._origin === listener)) {
  532. position = i;
  533. break;
  534. }
  535. }
  536. if (position < 0) {
  537. continue;
  538. }
  539. if(this.wildcard) {
  540. leaf._listeners.splice(position, 1);
  541. }
  542. else {
  543. this._events[type].splice(position, 1);
  544. }
  545. if (handlers.length === 0) {
  546. if(this.wildcard) {
  547. delete leaf._listeners;
  548. }
  549. else {
  550. delete this._events[type];
  551. }
  552. }
  553. if (this._removeListener)
  554. this.emit("removeListener", type, listener);
  555. return this;
  556. }
  557. else if (handlers === listener ||
  558. (handlers.listener && handlers.listener === listener) ||
  559. (handlers._origin && handlers._origin === listener)) {
  560. if(this.wildcard) {
  561. delete leaf._listeners;
  562. }
  563. else {
  564. delete this._events[type];
  565. }
  566. if (this._removeListener)
  567. this.emit("removeListener", type, listener);
  568. }
  569. }
  570. function recursivelyGarbageCollect(root) {
  571. if (root === undefined) {
  572. return;
  573. }
  574. var keys = Object.keys(root);
  575. for (var i in keys) {
  576. var key = keys[i];
  577. var obj = root[key];
  578. if ((obj instanceof Function) || (typeof obj !== "object") || (obj === null))
  579. continue;
  580. if (Object.keys(obj).length > 0) {
  581. recursivelyGarbageCollect(root[key]);
  582. }
  583. if (Object.keys(obj).length === 0) {
  584. delete root[key];
  585. }
  586. }
  587. }
  588. recursivelyGarbageCollect(this.listenerTree);
  589. return this;
  590. };
  591. EventEmitter.prototype.offAny = function(fn) {
  592. var i = 0, l = 0, fns;
  593. if (fn && this._all && this._all.length > 0) {
  594. fns = this._all;
  595. for(i = 0, l = fns.length; i < l; i++) {
  596. if(fn === fns[i]) {
  597. fns.splice(i, 1);
  598. if (this._removeListener)
  599. this.emit("removeListenerAny", fn);
  600. return this;
  601. }
  602. }
  603. } else {
  604. fns = this._all;
  605. if (this._removeListener) {
  606. for(i = 0, l = fns.length; i < l; i++)
  607. this.emit("removeListenerAny", fns[i]);
  608. }
  609. this._all = [];
  610. }
  611. return this;
  612. };
  613. EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
  614. EventEmitter.prototype.removeAllListeners = function(type) {
  615. if (type === undefined) {
  616. !this._events || init.call(this);
  617. return this;
  618. }
  619. if (this.wildcard) {
  620. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  621. var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
  622. for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
  623. var leaf = leafs[iLeaf];
  624. leaf._listeners = null;
  625. }
  626. }
  627. else if (this._events) {
  628. this._events[type] = null;
  629. }
  630. return this;
  631. };
  632. EventEmitter.prototype.listeners = function(type) {
  633. if (this.wildcard) {
  634. var handlers = [];
  635. var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
  636. searchListenerTree.call(this, handlers, ns, this.listenerTree, 0);
  637. return handlers;
  638. }
  639. this._events || init.call(this);
  640. if (!this._events[type]) this._events[type] = [];
  641. if (!isArray(this._events[type])) {
  642. this._events[type] = [this._events[type]];
  643. }
  644. return this._events[type];
  645. };
  646. EventEmitter.prototype.eventNames = function(){
  647. return Object.keys(this._events);
  648. }
  649. EventEmitter.prototype.listenerCount = function(type) {
  650. return this.listeners(type).length;
  651. };
  652. EventEmitter.prototype.listenersAny = function() {
  653. if(this._all) {
  654. return this._all;
  655. }
  656. else {
  657. return [];
  658. }
  659. };
  660. if (typeof define === 'function' && define.amd) {
  661. // AMD. Register as an anonymous module.
  662. define(function() {
  663. return EventEmitter;
  664. });
  665. } else if (typeof exports === 'object') {
  666. // CommonJS
  667. module.exports = EventEmitter;
  668. }
  669. else {
  670. // Browser global.
  671. window.EventEmitter2 = EventEmitter;
  672. }
  673. }();