-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventEmitter.min.js
1 lines (1 loc) · 3.2 KB
/
EventEmitter.min.js
1
!function(a){"use strict";"function"==typeof define&&define.amd?define(a):"object"==typeof module?module.exports=a():window.EventEmitter=a()}(function(){"use strict";var a=function(){return this._events={},this},b=function(a,b,c){var d=[];b||(b=0),c||(c=a.length);for(var e=b;c>e;e++)d.push(a[e]);return d},c=a.execListener=function(a,b){return"function"==typeof a?a.apply(a,b):"object"==typeof a&&"function"==typeof a.handleEvent?a.handleEvent.apply(a,b):void 0},d=a.regexps={},e=a.eventRegexp=function(a){return d[a]||(d[a]=new RegExp("^"+a.replace("*",".*")+"$")),d[a]};a.prototype.emit=a.prototype.fire=a.prototype.trigger=function(a){var d=b(arguments,1);d.push(a);var f=e(a);a:for(var g in this._events){var h=e(g);if(h.test(a)||f.test(g))for(var i=this._events[g],j=0,k=i.length;k>j;j++){var l=c(i[j],d,a);if(l===!1)break a}}return this},a.prototype.on=a.prototype.addListener=a.prototype.addEventListener=function(a,b){return this._events[a]||(this._events[a]=[]),this._events[a].push(b),e(a),this.emit("newListener",a,b),this},a.prototype.once=a.prototype.addOnceListener=function(a,b){var d=this,e=function(){c(b,arguments),d.removeListener(a,this)};return this.addListener(a,e),this},a.prototype.many=a.prototype.addManyListener=function(a,b,d){var e=this,f=0,g=function(){c(b,arguments),f++,f===d&&e.removeListener(a,this)};return this.addListener(a,g),this},a.prototype.off=a.prototype.removeListener=a.prototype.removeEventListener=function(a,b,c){var d=e(a);if(c){for(var f in this._events)if(d.test(f)){for(var g=this._events[f],h=[],i=0,j=g.length;j>i;i++)g[i]!==b&&h.push(g[i]);this._events[f]=h}}else for(var f in this._events)if(d.test(f)){var k=this._events[f].indexOf(b);if(k>-1){this._events[f].splice(k,1);break}}return this},a.prototype.offAll=a.prototype.removeAllListeners=function(a){if(a){var b=e(a);for(var c in this._events)b.test(c)&&(this._events[c]=[])}else this._events={};return this},a.prototype.count=a.prototype.countListeners=function(a){var b=0;if(a){var c=e(a);for(var d in this._events){var f=e(d);(f.test(a)||c.test(d))&&(b+=this._events[d].length)}}else for(var d in this._events)b+=this._events[d].length;return b},a.prototype.listeners=a.prototype.getListeners=function(a){var b=[];if(a){var c=e(a);for(var d in this._events){var f=e(d);(f.test(a)||c.test(d))&&b.push.apply(b,this._events[d])}}else{var b=[];for(var d in this._events)b.push.apply(b,this._events[d])}return b};var f=function g(a,b){var c=this;return c.emitter=a,c.scope=b,b+=":",c.emit=function(c){return arguments[0]=b+c,a.emit.apply(a,arguments),this},c.on=c.addListener=c.addEventListener=function(c,d){return a.addListener(b+c,d),this},c.once=c.addOnceListener=function(c,d){return a.addOnceListener(b+c,d),this},c.many=c.addManyListener=function(c,d,e){return a.addManyListener(b+c,d,e),this},c.off=c.removeListener=c.removeEventListener=function(c,d){return a.removeListener(b+c,d),this},c.offAll=c.removeAllListeners=function(c){return a.removeAllListeners(b+(c?c:"*")),this},c.count=c.countListeners=function(c){return a.countListeners(b+(c?c:"*"))},c.listeners=c.getListeners=function(c){return a.getListeners(b+(c?c:"*"))},c.namespace=function(c){return new g(a,b+c)},c};return a.Namespace=f,a.prototype.namespace=function(a){return new f(this,a)},a});