-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventEmitter.min.js
8 lines (8 loc) · 5.35 KB
/
EventEmitter.min.js
1
2
3
4
5
6
7
8
/*!
* Event Emitter (the best you'll get ^^)
* author: Stefan Benicke
* version: 1.2.0
* url: https://github.com/opusonline/EventEmitter.js
* license: MIT
*/
!function(e,t){"use strict";function n(e,t,n){var s;Array.isArray(e)?(this.listener=e[0],s=e[1]):this.listener=e,this.listener.hasOwnProperty("boundThis")&&(s=this.listener.boundThis),this.context=s,this.namespaces=t,this.once=n||!1}function s(){}function r(e,t,s){var r,i=e.split(".");e=i.splice(0,1)[0],r=new n(t,i,s),"undefined"==typeof this._events&&(this._events={}),this._events.hasOwnProperty(e)===!1?this._events[e]=[r]:this._events[e].push(r),this._events.hasOwnProperty("newListener")&&this.emit("newListener",e,r)}function i(e,t,n,s){var r,i,h;for(r=0,i=this._events[e].length;i>r;r++)n(this._events[e][r],t,s)?(this._events.hasOwnProperty("removeListener")&&this.emit("removeListener",e,this._events[e][r]),delete this._events[e][r]):(h||(h=[]),h.push(this._events[e][r]));Array.isArray(h)&&h.length>0?this._events[e]=h:delete this._events[e]}function h(e){var t,n;if("undefined"!=typeof e){for(t=0,n=this._events[e].length;n>t;t++)this.emit("removeListener",e,this._events[e][t]),delete this._events[e][t];delete this._events[e]}else for(e in this._events)this._events.hasOwnProperty(e)&&h.call(this,e)}function o(e,t){return f(t,e.namespaces)}function a(e,t,n){return(Array.isArray(n)&&e.listener===n[0]&&e.context===n[1]||e.listener===n)&&f(t,e.namespaces)}function u(e,t){return e.once===!0&&f(t,e.namespaces)}function f(e,t){return Array.isArray(e)?0===e.length?!0:0===t.length?!1:e.every(function(e){return-1!==t.indexOf(e)}):!0}function l(e,t){var n,s;if(!Array.isArray(e))return e;if(Array.isArray(t))for(n=0,s=t.length;s>n;n++)e.push(t[n]);else e.push(t);return e}var p,v=e.EventEmitter;s.noConflict=function(){return e.EventEmitter=v,s},s.inherits=function(e){e.super_=s,e.prototype=Object.create(s.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})},s.prototype.listeners=function(e){var t,n,s,r=[];if("undefined"==typeof this._events)return r;if(0===arguments.length||"undefined"==typeof e){for(e in this._events)if(this._events.hasOwnProperty(e))for(t=0,n=this._events[e].length;n>t;t++)r.push(this._events[e][t]);return r}if(arguments.length>1){for(t=0,n=arguments.length;n>t;t++)l(r,this.listeners.call(this,arguments[t]));return r}if(-1!==e.indexOf(".")&&(s=e.split("."),e=s.splice(0,1)[0]),""===e){for(e in this._events)if(this._events.hasOwnProperty(e))for(t=0,n=this._events[e].length;n>t;t++)f(s,this._events[e][t].namespaces)&&r.push(this._events[e][t])}else{if(this._events.hasOwnProperty(e)===!1)return r;for(t=0,n=this._events[e].length;n>t;t++)f(s,this._events[e][t].namespaces)&&r.push(this._events[e][t])}return r},s.prototype.emit=function(e){var t,n,s,r,h;if("undefined"==typeof this._events||arguments.length<1||"undefined"==typeof e)return this;if(Array.isArray(e)){for(s=0,r=e.length;r>s;s++)arguments[0]=e[s],this.emit.apply(this,arguments);return this}if(-1!==e.indexOf(".")&&(h=e.split("."),e=h.splice(0,1)[0]),this._events.hasOwnProperty(e)===!1)return this;for(arguments.length>1&&(t=Array.prototype.slice.call(arguments,1)),s=0,r=this._events[e].length;r>s;s++)f(h,this._events[e][s].namespaces)&&(this._events[e][s].listener.apply(this._events[e][s].context,t),this._events[e][s].once&&(n=!0));return n&&i.call(this,e,h,u),this},s.prototype.on=function(e){var t,n;if(arguments.length<2||"undefined"==typeof e)return this;if(Array.isArray(e)){for(t=0,n=e.length;n>t;t++)arguments[0]=e[t],this.on.apply(this,arguments);return this}for(t=1,n=arguments.length;n>t;t++)r.call(this,e,arguments[t]);return this},s.prototype.once=function(e){var t,n;if(arguments.length<2||"undefined"==typeof e)return this;if(Array.isArray(e)){for(t=0,n=e.length;n>t;t++)arguments[0]=e[t],this.once.apply(this,arguments);return this}for(t=1,n=arguments.length;n>t;t++)r.call(this,e,arguments[t],!0);return this},s.prototype.off=function(e){var t,n,s;if("undefined"==typeof this._events)return this;if(0===arguments.length)this._events.hasOwnProperty("removeListener")?h.call(this):delete this._events;else{if("undefined"==typeof e)return this;if(Array.isArray(e)){for(t=0,n=e.length;n>t;t++)arguments[0]=e[t],this.off.apply(this,arguments);return this}if(-1!==e.indexOf(".")&&(s=e.split("."),e=s.splice(0,1)[0]),1===arguments.length)if(Array.isArray(s))if(""===e)for(e in this._events)this._events.hasOwnProperty(e)&&i.call(this,e,s,o);else{if(this._events.hasOwnProperty(e)===!1)return this;i.call(this,e,s,o)}else{if(this._events.hasOwnProperty(e)===!1)return this;this._events.hasOwnProperty("removeListener")?h.call(this,e):delete this._events[e]}else if(""===e){for(e in this._events)if(this._events.hasOwnProperty(e))for(t=1,n=arguments.length;n>t;t++)i.call(this,e,s,a,arguments[t])}else{if(this._events.hasOwnProperty(e)===!1)return this;for(t=1,n=arguments.length;n>t;t++)i.call(this,e,s,a,arguments[t])}}return this},s.prototype.addListener=s.prototype.on,s.prototype.removeListener=s.prototype.off,s.prototype.removeAllListeners=s.prototype.off,Function.prototype.bind&&(p=Function.prototype.bind,Function.prototype.bind=function(e){var t=this,n=Array.prototype.slice.call(arguments,1),s=p.apply(t,arguments);return Object.defineProperties(s,{boundThis:{value:e},boundArgs:{value:n},targetFunction:{value:t}})}),"function"==typeof define&&define.amd?define(function(){return s}):"undefined"!=typeof module?module.exports=s:e.EventEmitter=s}(this);