UNPKG

@ylz/plugins

Version:

ylz plugins

3 lines (2 loc) 1.86 kB
/* * Copyright © 2019-2019 chenwenbin * Released under the MIT License. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["event-bus"]=t()}(this,(function(){"use strict";function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function t(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var n=function(){function n(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,n),this.handles||Object.defineProperty(this,"handles",{value:{},enumerable:!1}),this.Vue=e,this.eventMapUid={}}var i,r,o;return i=n,(r=[{key:"setEventMapUid",value:function(e,t){this.eventMapUid[e]||(this.eventMapUid[e]=[]),this.eventMapUid[e].push(t)}},{key:"$on",value:function(e,t,n){this.handles[e]||(this.handles[e]=[]),this.handles[e].push(t),n instanceof this.Vue&&this.setEventMapUid(n._uid,e)}},{key:"$emit",value:function(){var e=Array.prototype.slice.call(arguments),n=e[0],i=e.slice(1);if(this.handles[n])for(var r=this.handles[n].length,o=0;o<r;o++){var a;(a=this.handles[n])[o].apply(a,t(i))}}},{key:"$offVmEvent",value:function(e){var t=this;(this.eventMapUid[e]||[]).forEach((function(e){t.$off(e)}))}},{key:"$off",value:function(e){delete this.handles[e]}}])&&e(i.prototype,r),o&&e(i,o),n}(),i={install:function(e,t){window.$bus=e.prototype.$bus=new n(e),e.mixin({beforeDestroy:function(){this.$bus.$offVmEvent(this._uid)}})}};return i}));