123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- /*!
- * EventEmitter2
- * https://github.com/hij1nx/EventEmitter2
- *
- * Copyright (c) 2013 hij1nx
- * Licensed under the MIT license.
- */
- ;!function(undefined) {
- var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
- };
- var defaultMaxListeners = 10;
- function init() {
- this._events = {};
- if (this._conf) {
- configure.call(this, this._conf);
- }
- }
- function configure(conf) {
- if (conf) {
- this._conf = conf;
- conf.delimiter && (this.delimiter = conf.delimiter);
- this._maxListeners = conf.maxListeners !== undefined ? conf.maxListeners : defaultMaxListeners;
- conf.wildcard && (this.wildcard = conf.wildcard);
- conf.newListener && (this._newListener = conf.newListener);
- conf.removeListener && (this._removeListener = conf.removeListener);
- conf.verboseMemoryLeak && (this.verboseMemoryLeak = conf.verboseMemoryLeak);
- if (this.wildcard) {
- this.listenerTree = {};
- }
- } else {
- this._maxListeners = defaultMaxListeners;
- }
- }
- function logPossibleMemoryLeak(count, eventName) {
- var errorMsg = '(node) warning: possible EventEmitter memory ' +
- 'leak detected. ' + count + ' listeners added. ' +
- 'Use emitter.setMaxListeners() to increase limit.';
- if(this.verboseMemoryLeak){
- errorMsg += ' Event name: ' + eventName + '.';
- }
- if(typeof process !== 'undefined' && process.emitWarning){
- var e = new Error(errorMsg);
- e.name = 'MaxListenersExceededWarning';
- e.emitter = this;
- e.count = count;
- process.emitWarning(e);
- } else {
- console.error(errorMsg);
- if (console.trace){
- console.trace();
- }
- }
- }
- function EventEmitter(conf) {
- this._events = {};
- this._newListener = false;
- this._removeListener = false;
- this.verboseMemoryLeak = false;
- configure.call(this, conf);
- }
- EventEmitter.EventEmitter2 = EventEmitter; // backwards compatibility for exporting EventEmitter property
- //
- // Attention, function return type now is array, always !
- // It has zero elements if no any matches found and one or more
- // elements (leafs) if there are matches
- //
- function searchListenerTree(handlers, type, tree, i) {
- if (!tree) {
- return [];
- }
- var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached,
- typeLength = type.length, currentType = type[i], nextType = type[i+1];
- if (i === typeLength && tree._listeners) {
- //
- // If at the end of the event(s) list and the tree has listeners
- // invoke those listeners.
- //
- if (typeof tree._listeners === 'function') {
- handlers && handlers.push(tree._listeners);
- return [tree];
- } else {
- for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
- handlers && handlers.push(tree._listeners[leaf]);
- }
- return [tree];
- }
- }
- if ((currentType === '*' || currentType === '**') || tree[currentType]) {
- //
- // If the event emitted is '*' at this part
- // or there is a concrete match at this patch
- //
- if (currentType === '*') {
- for (branch in tree) {
- if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1));
- }
- }
- return listeners;
- } else if(currentType === '**') {
- endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*'));
- if(endReached && tree._listeners) {
- // The next element has a _listeners, add it to the handlers.
- listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength));
- }
- for (branch in tree) {
- if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
- if(branch === '*' || branch === '**') {
- if(tree[branch]._listeners && !endReached) {
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength));
- }
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
- } else if(branch === nextType) {
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2));
- } else {
- // No match on this one, shift into the tree but not in the type array.
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
- }
- }
- }
- return listeners;
- }
- listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1));
- }
- xTree = tree['*'];
- if (xTree) {
- //
- // If the listener tree will allow any match for this part,
- // then recursively explore all branches of the tree
- //
- searchListenerTree(handlers, type, xTree, i+1);
- }
- xxTree = tree['**'];
- if(xxTree) {
- if(i < typeLength) {
- if(xxTree._listeners) {
- // If we have a listener on a '**', it will catch all, so add its handler.
- searchListenerTree(handlers, type, xxTree, typeLength);
- }
- // Build arrays of matching next branches and others.
- for(branch in xxTree) {
- if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
- if(branch === nextType) {
- // We know the next element will match, so jump twice.
- searchListenerTree(handlers, type, xxTree[branch], i+2);
- } else if(branch === currentType) {
- // Current node matches, move into the tree.
- searchListenerTree(handlers, type, xxTree[branch], i+1);
- } else {
- isolatedBranch = {};
- isolatedBranch[branch] = xxTree[branch];
- searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1);
- }
- }
- }
- } else if(xxTree._listeners) {
- // We have reached the end and still on a '**'
- searchListenerTree(handlers, type, xxTree, typeLength);
- } else if(xxTree['*'] && xxTree['*']._listeners) {
- searchListenerTree(handlers, type, xxTree['*'], typeLength);
- }
- }
- return listeners;
- }
- function growListenerTree(type, listener) {
- type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- //
- // Looks for two consecutive '**', if so, don't add the event at all.
- //
- for(var i = 0, len = type.length; i+1 < len; i++) {
- if(type[i] === '**' && type[i+1] === '**') {
- return;
- }
- }
- var tree = this.listenerTree;
- var name = type.shift();
- while (name !== undefined) {
- if (!tree[name]) {
- tree[name] = {};
- }
- tree = tree[name];
- if (type.length === 0) {
- if (!tree._listeners) {
- tree._listeners = listener;
- }
- else {
- if (typeof tree._listeners === 'function') {
- tree._listeners = [tree._listeners];
- }
- tree._listeners.push(listener);
- if (
- !tree._listeners.warned &&
- this._maxListeners > 0 &&
- tree._listeners.length > this._maxListeners
- ) {
- tree._listeners.warned = true;
- logPossibleMemoryLeak.call(this, tree._listeners.length, name);
- }
- }
- return true;
- }
- name = type.shift();
- }
- return true;
- }
- // By default EventEmitters will print a warning if more than
- // 10 listeners are added to it. This is a useful default which
- // helps finding memory leaks.
- //
- // Obviously not all Emitters should be limited to 10. This function allows
- // that to be increased. Set to zero for unlimited.
- EventEmitter.prototype.delimiter = '.';
- EventEmitter.prototype.setMaxListeners = function(n) {
- if (n !== undefined) {
- this._maxListeners = n;
- if (!this._conf) this._conf = {};
- this._conf.maxListeners = n;
- }
- };
- EventEmitter.prototype.event = '';
- EventEmitter.prototype.once = function(event, fn) {
- return this._once(event, fn, false);
- };
- EventEmitter.prototype.prependOnceListener = function(event, fn) {
- return this._once(event, fn, true);
- };
- EventEmitter.prototype._once = function(event, fn, prepend) {
- this._many(event, 1, fn, prepend);
- return this;
- };
- EventEmitter.prototype.many = function(event, ttl, fn) {
- return this._many(event, ttl, fn, false);
- }
- EventEmitter.prototype.prependMany = function(event, ttl, fn) {
- return this._many(event, ttl, fn, true);
- }
- EventEmitter.prototype._many = function(event, ttl, fn, prepend) {
- var self = this;
- if (typeof fn !== 'function') {
- throw new Error('many only accepts instances of Function');
- }
- function listener() {
- if (--ttl === 0) {
- self.off(event, listener);
- }
- return fn.apply(this, arguments);
- }
- listener._origin = fn;
- this._on(event, listener, prepend);
- return self;
- };
- EventEmitter.prototype.emit = function() {
- this._events || init.call(this);
- var type = arguments[0];
- if (type === 'newListener' && !this._newListener) {
- if (!this._events.newListener) {
- return false;
- }
- }
- var al = arguments.length;
- var args,l,i,j;
- var handler;
- if (this._all && this._all.length) {
- handler = this._all.slice();
- if (al > 3) {
- args = new Array(al);
- for (j = 0; j < al; j++) args[j] = arguments[j];
- }
- for (i = 0, l = handler.length; i < l; i++) {
- this.event = type;
- switch (al) {
- case 1:
- handler[i].call(this, type);
- break;
- case 2:
- handler[i].call(this, type, arguments[1]);
- break;
- case 3:
- handler[i].call(this, type, arguments[1], arguments[2]);
- break;
- default:
- handler[i].apply(this, args);
- }
- }
- }
- if (this.wildcard) {
- handler = [];
- var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
- } else {
- handler = this._events[type];
- if (typeof handler === 'function') {
- this.event = type;
- switch (al) {
- case 1:
- handler.call(this);
- break;
- case 2:
- handler.call(this, arguments[1]);
- break;
- case 3:
- handler.call(this, arguments[1], arguments[2]);
- break;
- default:
- args = new Array(al - 1);
- for (j = 1; j < al; j++) args[j - 1] = arguments[j];
- handler.apply(this, args);
- }
- return true;
- } else if (handler) {
- // need to make copy of handlers because list can change in the middle
- // of emit call
- handler = handler.slice();
- }
- }
- if (handler && handler.length) {
- if (al > 3) {
- args = new Array(al - 1);
- for (j = 1; j < al; j++) args[j - 1] = arguments[j];
- }
- for (i = 0, l = handler.length; i < l; i++) {
- this.event = type;
- switch (al) {
- case 1:
- handler[i].call(this);
- break;
- case 2:
- handler[i].call(this, arguments[1]);
- break;
- case 3:
- handler[i].call(this, arguments[1], arguments[2]);
- break;
- default:
- handler[i].apply(this, args);
- }
- }
- return true;
- } else if (!this._all && type === 'error') {
- if (arguments[1] instanceof Error) {
- throw arguments[1]; // Unhandled 'error' event
- } else {
- throw new Error("Uncaught, unspecified 'error' event.");
- }
- return false;
- }
- return !!this._all;
- };
- EventEmitter.prototype.emitAsync = function() {
- this._events || init.call(this);
- var type = arguments[0];
- if (type === 'newListener' && !this._newListener) {
- if (!this._events.newListener) { return Promise.resolve([false]); }
- }
- var promises= [];
- var al = arguments.length;
- var args,l,i,j;
- var handler;
- if (this._all) {
- if (al > 3) {
- args = new Array(al);
- for (j = 1; j < al; j++) args[j] = arguments[j];
- }
- for (i = 0, l = this._all.length; i < l; i++) {
- this.event = type;
- switch (al) {
- case 1:
- promises.push(this._all[i].call(this, type));
- break;
- case 2:
- promises.push(this._all[i].call(this, type, arguments[1]));
- break;
- case 3:
- promises.push(this._all[i].call(this, type, arguments[1], arguments[2]));
- break;
- default:
- promises.push(this._all[i].apply(this, args));
- }
- }
- }
- if (this.wildcard) {
- handler = [];
- var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
- } else {
- handler = this._events[type];
- }
- if (typeof handler === 'function') {
- this.event = type;
- switch (al) {
- case 1:
- promises.push(handler.call(this));
- break;
- case 2:
- promises.push(handler.call(this, arguments[1]));
- break;
- case 3:
- promises.push(handler.call(this, arguments[1], arguments[2]));
- break;
- default:
- args = new Array(al - 1);
- for (j = 1; j < al; j++) args[j - 1] = arguments[j];
- promises.push(handler.apply(this, args));
- }
- } else if (handler && handler.length) {
- handler = handler.slice();
- if (al > 3) {
- args = new Array(al - 1);
- for (j = 1; j < al; j++) args[j - 1] = arguments[j];
- }
- for (i = 0, l = handler.length; i < l; i++) {
- this.event = type;
- switch (al) {
- case 1:
- promises.push(handler[i].call(this));
- break;
- case 2:
- promises.push(handler[i].call(this, arguments[1]));
- break;
- case 3:
- promises.push(handler[i].call(this, arguments[1], arguments[2]));
- break;
- default:
- promises.push(handler[i].apply(this, args));
- }
- }
- } else if (!this._all && type === 'error') {
- if (arguments[1] instanceof Error) {
- return Promise.reject(arguments[1]); // Unhandled 'error' event
- } else {
- return Promise.reject("Uncaught, unspecified 'error' event.");
- }
- }
- return Promise.all(promises);
- };
- EventEmitter.prototype.on = function(type, listener) {
- return this._on(type, listener, false);
- };
- EventEmitter.prototype.prependListener = function(type, listener) {
- return this._on(type, listener, true);
- };
- EventEmitter.prototype.onAny = function(fn) {
- return this._onAny(fn, false);
- };
- EventEmitter.prototype.prependAny = function(fn) {
- return this._onAny(fn, true);
- };
- EventEmitter.prototype.addListener = EventEmitter.prototype.on;
- EventEmitter.prototype._onAny = function(fn, prepend){
- if (typeof fn !== 'function') {
- throw new Error('onAny only accepts instances of Function');
- }
- if (!this._all) {
- this._all = [];
- }
- // Add the function to the event listener collection.
- if(prepend){
- this._all.unshift(fn);
- }else{
- this._all.push(fn);
- }
- return this;
- }
- EventEmitter.prototype._on = function(type, listener, prepend) {
- if (typeof type === 'function') {
- this._onAny(type, listener);
- return this;
- }
- if (typeof listener !== 'function') {
- throw new Error('on only accepts instances of Function');
- }
- this._events || init.call(this);
- // To avoid recursion in the case that type == "newListeners"! Before
- // adding it to the listeners, first emit "newListeners".
- if (this._newListener)
- this.emit('newListener', type, listener);
- if (this.wildcard) {
- growListenerTree.call(this, type, listener);
- return this;
- }
- if (!this._events[type]) {
- // Optimize the case of one listener. Don't need the extra array object.
- this._events[type] = listener;
- }
- else {
- if (typeof this._events[type] === 'function') {
- // Change to array.
- this._events[type] = [this._events[type]];
- }
- // If we've already got an array, just add
- if(prepend){
- this._events[type].unshift(listener);
- }else{
- this._events[type].push(listener);
- }
- // Check for listener leak
- if (
- !this._events[type].warned &&
- this._maxListeners > 0 &&
- this._events[type].length > this._maxListeners
- ) {
- this._events[type].warned = true;
- logPossibleMemoryLeak.call(this, this._events[type].length, type);
- }
- }
- return this;
- }
- EventEmitter.prototype.off = function(type, listener) {
- if (typeof listener !== 'function') {
- throw new Error('removeListener only takes instances of Function');
- }
- var handlers,leafs=[];
- if(this.wildcard) {
- var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
- }
- else {
- // does not use listeners(), so no side effect of creating _events[type]
- if (!this._events[type]) return this;
- handlers = this._events[type];
- leafs.push({_listeners:handlers});
- }
- for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
- var leaf = leafs[iLeaf];
- handlers = leaf._listeners;
- if (isArray(handlers)) {
- var position = -1;
- for (var i = 0, length = handlers.length; i < length; i++) {
- if (handlers[i] === listener ||
- (handlers[i].listener && handlers[i].listener === listener) ||
- (handlers[i]._origin && handlers[i]._origin === listener)) {
- position = i;
- break;
- }
- }
- if (position < 0) {
- continue;
- }
- if(this.wildcard) {
- leaf._listeners.splice(position, 1);
- }
- else {
- this._events[type].splice(position, 1);
- }
- if (handlers.length === 0) {
- if(this.wildcard) {
- delete leaf._listeners;
- }
- else {
- delete this._events[type];
- }
- }
- if (this._removeListener)
- this.emit("removeListener", type, listener);
- return this;
- }
- else if (handlers === listener ||
- (handlers.listener && handlers.listener === listener) ||
- (handlers._origin && handlers._origin === listener)) {
- if(this.wildcard) {
- delete leaf._listeners;
- }
- else {
- delete this._events[type];
- }
- if (this._removeListener)
- this.emit("removeListener", type, listener);
- }
- }
- function recursivelyGarbageCollect(root) {
- if (root === undefined) {
- return;
- }
- var keys = Object.keys(root);
- for (var i in keys) {
- var key = keys[i];
- var obj = root[key];
- if ((obj instanceof Function) || (typeof obj !== "object") || (obj === null))
- continue;
- if (Object.keys(obj).length > 0) {
- recursivelyGarbageCollect(root[key]);
- }
- if (Object.keys(obj).length === 0) {
- delete root[key];
- }
- }
- }
- recursivelyGarbageCollect(this.listenerTree);
- return this;
- };
- EventEmitter.prototype.offAny = function(fn) {
- var i = 0, l = 0, fns;
- if (fn && this._all && this._all.length > 0) {
- fns = this._all;
- for(i = 0, l = fns.length; i < l; i++) {
- if(fn === fns[i]) {
- fns.splice(i, 1);
- if (this._removeListener)
- this.emit("removeListenerAny", fn);
- return this;
- }
- }
- } else {
- fns = this._all;
- if (this._removeListener) {
- for(i = 0, l = fns.length; i < l; i++)
- this.emit("removeListenerAny", fns[i]);
- }
- this._all = [];
- }
- return this;
- };
- EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
- EventEmitter.prototype.removeAllListeners = function(type) {
- if (type === undefined) {
- !this._events || init.call(this);
- return this;
- }
- if (this.wildcard) {
- var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
- for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
- var leaf = leafs[iLeaf];
- leaf._listeners = null;
- }
- }
- else if (this._events) {
- this._events[type] = null;
- }
- return this;
- };
- EventEmitter.prototype.listeners = function(type) {
- if (this.wildcard) {
- var handlers = [];
- var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
- searchListenerTree.call(this, handlers, ns, this.listenerTree, 0);
- return handlers;
- }
- this._events || init.call(this);
- if (!this._events[type]) this._events[type] = [];
- if (!isArray(this._events[type])) {
- this._events[type] = [this._events[type]];
- }
- return this._events[type];
- };
- EventEmitter.prototype.eventNames = function(){
- return Object.keys(this._events);
- }
- EventEmitter.prototype.listenerCount = function(type) {
- return this.listeners(type).length;
- };
- EventEmitter.prototype.listenersAny = function() {
- if(this._all) {
- return this._all;
- }
- else {
- return [];
- }
- };
- if (typeof define === 'function' && define.amd) {
- // AMD. Register as an anonymous module.
- define(function() {
- return EventEmitter;
- });
- } else if (typeof exports === 'object') {
- // CommonJS
- module.exports = EventEmitter;
- }
- else {
- // Browser global.
- window.EventEmitter2 = EventEmitter;
- }
- }();
|