event-sky
Version:
javascript event pub/sub utility
2 lines (1 loc) • 4.03 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.EventSky=n()}(this,function(){"use strict";function e(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}var n={createNewEventMap:function(e){return{handlers:0,_handlers:0,_name:e,on:{},once:{},beforeAll:{},afterAll:{}}},curryWhenHandler:function(e){var n=this;return function(t,r){if("string"!=typeof t||t.length<1||"function"!=typeof r)return console.warn('EventSky warning: "'+t+'" event must be a string and handler must be a function - not set with .'+e+" handler"),n;n.events[t]||(n.events[t]=n._utils.createNewEventMap(t)),n.events[t][e]&&n.events[t][e]===r&&(n.config.ignoreDuplicateHandler||console.warn("EventSky warning: duplicate handler for ."+e+"('"+t+"')")),n.events[t].handlers++;var o=n.events[t]._handlers++,i="id_"+t+"."+e+"."+o;return n.events[t][e][i]=r,i}},validateEventName:function(e,n){return e?!(!n||"string"!=typeof n)||(console.error("EventSky error: .trigger(event) did not receive a string param"),!1):console.error("EventSky._utils needs to be passed a valid context param")}},t=function(){function e(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(n,t,r){return t&&e(n.prototype,t),r&&e(n,r),n}}();return new(function(){function r(){var t=this;e(this,r),this.config={firehose:!1,ignoreDuplicateHandler:!1},this._utils=n,this.events={},this._firehose=function(e){return t.config.firehose?console.log("EventSky Firehose >> "+e):null},this.on=this._utils.curryWhenHandler.bind(this)("on"),this.once=this._utils.curryWhenHandler.bind(this)("once"),this.beforeAll=this._utils.curryWhenHandler.bind(this)("beforeAll"),this.afterAll=this._utils.curryWhenHandler.bind(this)("afterAll"),this.off=this.off.bind(this),this.trigger=this.trigger.bind(this),this.off.all=this.allOff.bind(this)}return t(r,[{key:"off",value:function(e,n){var t=this;return Object.keys(this.events).forEach(function(r){Object.keys(t.events[r]).forEach(function(o){["beforeAll","on","once","afterAll"].includes(o)&&Object.keys(t.events[r][o]).forEach(function(i){var s=t.events[r][o][i]===n&&r===e,a=!n&&i===e;(s||a)&&(delete t.events[r][o][i],t.events[r].handlers--)})})}),this}},{key:"allOff",value:function(e){return delete this.events[e],this._firehose('.off.all("'+e+'") removed all event handlers'),this.events[e]=this._utils.createNewEventMap(),this}},{key:"trigger",value:function(e,n){if(!this._utils.validateEventName(this,e))return this;if(!this.events[e])return this._firehose('"'+e+'" triggered with no handlers setup'),this;this._firehose('"'+e+'" triggered');var t=this.events[e].beforeAll;Object.keys(t).forEach(function(o){try{t[o](n)}catch(i){t[o]?console.error("EventSky error: .beforeAll('"+e+"', "+(r[o].name||"anonymous")+") handler trapped an error",{error:i,data:n}):console.error("EventSky error: .beforeAll('"+e+"', ...) handler does not exist",{eventId:o,data:n})}});var r=this.events[e].on;Object.keys(r).forEach(function(t){try{r[t](n)}catch(o){r[t]?console.error("EventSky error: .on('"+e+"', "+(r[t].name||"anonymous")+") handler trapped an error",{error:o,data:n}):console.error("EventSky error: .on('"+e+"', ...) handler does not exist",{eventId:t,data:n})}});var o=this.events[e].once;Object.keys(o).forEach(function(t){try{o[t](n)}catch(i){o[t]?console.error("EventSky error: .once('"+e+"', "+(r[t].name||"anonymous")+") handler trapped an error",{error:i,data:n}):console.error("EventSky error: .once('"+e+"', ...) handler does not exist",{eventId:t,data:n})}});var i=this.events[e].afterAll;return Object.keys(i).forEach(function(t){try{i[t](n)}catch(o){i[t]?console.error("EventSky error: .afterAll('"+e+"', "+(r[t].name||"anonymous")+") handler trapped an error",{error:o,data:n}):console.error("EventSky error: .afterAll('"+e+"', ...) handler does not exist",{eventId:t,data:n})}}),this}}]),r}())});