UNPKG

dz-emitter

Version:

Event launcher/publish and subscribe, one-to-one implementation of the time launcher in Vue2.

3 lines (2 loc) 1.46 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).dzEmitter=n()}(this,function(){"use strict";function a(e,n){for(var t=0;t<n.length;t++){var i=n[t];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(){function i(){var e,n,t;!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,i),t={},(n="_events")in(e=this)?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t}var e,n,t;return e=i,t=[{key:"addEvent",value:function(e,n){var t=2<arguments.length&&void 0!==arguments[2]&&arguments[2],i=this._events;Object.keys(i).includes(e)?(i[e].isOnce=t,i[e].callbacks.push(n)):i[e]={isOnce:t,callbacks:[n]}}}],(n=[{key:"emit",value:function(e,n){try{var t=this._events[e],i=t.callbacks,a=t.isOnce;i.forEach(function(e){return e(n)}),a&&this.off(e)}catch(e){console.error(e)}}},{key:"on",value:function(e,n){i.addEvent.call(this,e,n)}},{key:"once",value:function(e,n){i.addEvent.call(this,e,n,!0)}},{key:"off",value:function(e,n){var t;arguments.length||(this._events={}),e&&!n&&delete this._events[e],e&&n&&(0<=(e=(t=this._events[e].callbacks).findIndex(function(e){return e===n}))&&t.splice(e,1))}}])&&a(e.prototype,n),t&&a(e,t),i}()}); //# sourceMappingURL=dz-emitter.js.map