UNPKG

mini-event-emitter

Version:

An easy javascript EventEmitter which has no dependencies and the possibility to group events, log errors and trace events

2 lines (1 loc) 4.79 kB
!function(){return function t(i,n,e){function r(s,h){if(!n[s]){if(!i[s]){var u="function"==typeof require&&require;if(!h&&u)return u(s,!0);if(o)return o(s,!0);var f=new Error("Cannot find module '"+s+"'");throw f.code="MODULE_NOT_FOUND",f}var m=n[s]={exports:{}};i[s][0].call(m.exports,function(t){return r(i[s][1][t]||t)},m,m.exports,t,i,n,e)}return n[s].exports}for(var o="function"==typeof require&&require,s=0;s<e.length;s++)r(e[s]);return r}}()({1:[function(t,i,n){var e,r;e=t("./handler"),r=function(){function t(t){var i;i=new e(this,t),this.on=i.on,this.off=i.off,this.emit=i.emit,this.emitIf=i.emitIf,this.trigger=i.emit,this.triggerIf=i.emitIf}return t.prototype.listen=function(t,i,n){},t}(),window.MiniEventEmitter=r},{"./handler":2}],2:[function(t,i,n){var e,r=function(t,i){return function(){return t.apply(i,arguments)}};e=function(){function t(t,i){this.mini=t,this.removeFn=r(this.removeFn,this),this.removeFns=r(this.removeFns,this),this.emit=r(this.emit,this),this.emitIf=r(this.emitIf,this),this.off=r(this.off,this),this.on=r(this.on,this),this.mini.settings={name:(null!=i?i.name:void 0)||"MiniEventEmitter",error:(null!=i?i.error:void 0)||!1,trace:(null!=i?i.trace:void 0)||!1},this.mini.events={},this.mini.groups={}}return t.prototype.on=function(t,i,n){var e,r,o;return i=(o=this.optional(i,n))[0],n=o[1],this.valid("on",t,i,n)?((r=this.mini.groups)[i]?r[i][t]?r[i][t].push(n):r[i][t]=[n]:(r[i]={},r[i][t]=[n]),(e=this.mini.events)[t]?e[t].push(n):e[t]=[n],this.mini):this.mini},t.prototype.off=function(t,i,n){var e,r,o;return 0===arguments.length?this.removeAll():(i=(o=this.optional(i,n))[0],n=o[1],this.valid("off",t,i,n)?this.mini.groups[i]?t?(e=this.mini.groups[i][t])?n?-1===(r=e.indexOf(n))?(this.error("off",2,t,i),this.mini):(this.removeFn(t,i,e,n,r),this.mini):this.removeFns(t,i,e):(this.error("off",8,t,i),this.mini):this.removeGroup(i):(this.error("off",7,t,i),this.mini):this.mini)},t.prototype.emitIf=function(){return this._emit(arguments,!0)},t.prototype.emit=function(){return this._emit(arguments)},t.prototype._emit=function(t,i){var n,e,r,o;if(n=(t=Array.from(t)).shift(),!(r=this.validEvent(n,i)))return this.mini;for(this.trace(n,t),o=r.length-1;o>=0;o+=-1)(e=r[o]).apply(e,t);return this.mini},t.prototype.valid=function(t,i,n,e){if("on"===t){if(!this.isString(i))return this.error(t,1);if(!this.isString(n))return this.error(t,5);if(!this.isFunction(e))return this.error(t,6,i,n)}if("off"===t){if(null!==i&&!this.isString(i))return this.error(t,1);if(!this.isString(n))return this.error(t,5);if(e&&!this.isFunction(e))return this.error(t,6,i,n)}return!0},t.prototype.validEvent=function(t,i){var n;return t?this.isString(t)?(n=this.mini.events[t])?n:(i||this.error("emit",4,t),null):this.error("emit",1):this.error("emit",3)},t.prototype.error=function(t,i,n,e){var r;return r=t+" ~ ",1===i&&(r+="Event name must be a string"),2===i&&(r+='Provided function to remove with event "'+n+'" in group "'+e+'" is not found'),3===i&&(r+="Event was not provided"),4===i&&(r+='EventListener for event "'+n+'" does not exist'),5===i&&(r+="Provided group must be a string"),6===i&&(r+='The last param provided with event "'+n+'" and group "'+e+'" is expected to be a function'),7===i&&(r+='Provided group "'+e+'" is not found'),8===i&&(r+='Event "'+n+'" does not exist for the provided group "'+e+'"'),this.mini.listen("error",n,[r,t,i,e]),this.mini.settings.error?(r=this.mini.settings.name+" ~ "+r,console&&(console.warn?console.warn(r):console.log(r)),null):null},t.prototype.optional=function(t,i){return null==i&&this.isFunction(t)?(i=t,t=""):t||(t=""),[t,i]},t.prototype.removeAll=function(){return this.mini.events={},this.mini.groups={},this.mini},t.prototype.removeGroup=function(t){var i,n,e;for(i in e=this.mini.groups[t])n=e[i],this.removeFns(i,t,n);return this.mini},t.prototype.removeFns=function(t,i,n){var e,r;for(r=n.length-1;r>=0;r+=-1)e=n[r],this.removeFn(t,i,n,e);return this.mini},t.prototype.removeFn=function(t,i,n,e,r){if(r||(r=n.indexOf(e)),n.splice(r,1),0===n.length&&delete this.mini.groups[i][t],0===this.objLength(this.mini.groups[i])&&delete this.mini.groups[i],r=this.mini.events[t].indexOf(e),this.mini.events[t].splice(r,1),0===this.mini.events[t].length)return delete this.mini.events[t]},t.prototype.trace=function(t,i){var n;if(this.mini.listen("trace",t,i),this.mini.settings.trace)return n=this.mini.settings.name+" ~ trace ~ "+t,0===i.length?console.debug?console.log("%c "+n,"color: #13d"):console.log(n):console.debug?console.log("%c "+n,"color: #13d",i):console.log(n,i)},t.prototype.isString=function(t){return"string"==typeof t||t instanceof String},t.prototype.objLength=function(t){return Object.keys(t).length},t.prototype.isFunction=function(t){return"function"==typeof t},t}(),i.exports=e},{}]},{},[1]);