eventbus-z
Version:
Simple event emitter. Global event emitter
2 lines (1 loc) • 4.17 kB
JavaScript
var e=function(){return e=Object.assign||function(e){for(var o,t=1,c=arguments.length;t<c;t++)for(var n in o=arguments[t])Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n]);return e},e.apply(this,arguments)};function o(e,o,t){if(t||2===arguments.length)for(var c,n=0,l=o.length;n<l;n++)!c&&n in o||(c||(c=Array.prototype.slice.call(o,0,n)),c[n]=o[n]);return e.concat(c||Array.prototype.slice.call(o))}var t="evtBusZxp_scopeZxp",c="globalZxp_defaultName",n=function(e,o){var n;void 0===o&&(o=c);var l="".concat(t,"__").concat(o);return(null===(n=window.top[l])||void 0===n?void 0:n.get(e))||[]},l=function(e,o,t){return void 0===o&&(o=c),n(e,o).filter((function(e){return e.callback!==t}))},p=function(e){void 0===e&&(e=c);var o="".concat(t,"__").concat(e);return window.top[o]||(window.top[o]=new Map),window.top[o]},a=function(o,n,a){if(void 0===a&&(a=!1),!o||!n.callback||"function"!=typeof n.callback)throw new Error("name or callback (function) is required");var i=e(e({},n),{once:a});if(function(e,o){void 0===o&&(o=c);var n="eventBusRegister_".concat(t),l=window.top[n]||{};l&&l[o]||(l[o]=[]),l[o].push(e),window.top[n]=l}(o,n.scopeName),n.single)p(n.scopeName).set(o,[i]);else{var f=l(o,n.scopeName,n.callback);f.push(i),p(n.scopeName).set(o,f)}},i=function(e){for(var o=[],t=1;t<arguments.length;t++)o[t-1]=arguments[t];return e.callback.apply(e,o),e.emitted=!0,e.timeEffect=(new Date).getTime(),e},f=new(function(){function e(){}return e.prototype.$once=function(e,o){a(e,{callback:o},!0)},e.prototype.$on=function(e,o){a(e,{callback:o,single:!0})},e.prototype.$onMultiple=function(e,o){a(e,{callback:o})},e.prototype.$onCached=function(e,o,t){void 0===t&&(t=100),a(e,{callback:o,timeCached:t,single:!0})},e.prototype.$onCachedMultiple=function(e,o,t){void 0===t&&(t=100),a(e,{callback:o,timeCached:t})},e.prototype.$off=function(e,o){f.$scopeOff(e,c,o)},e.prototype.$offAll=function(e){p(c).delete(e)},e.prototype.$emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];f.$scopeEmit.apply(f,o([e,c],t,!1))},e.prototype.$scopeOnce=function(e,o,t){a(e,{callback:t,scopeName:o},!0)},e.prototype.$scopeOn=function(e,o,t){a(e,{callback:t,scopeName:o,single:!0})},e.prototype.$scopeOnMultiple=function(e,o,t){a(e,{callback:t,scopeName:o})},e.prototype.$scopeOnCached=function(e,o,t,c){void 0===c&&(c=100),a(e,{callback:t,timeCached:c,scopeName:o,single:!0})},e.prototype.$scopeOnCachedMultiple=function(e,o,t,c){void 0===c&&(c=100),a(e,{callback:t,timeCached:c,scopeName:o})},e.prototype.$scopeOff=function(e,t,c){if(c&&"function"==typeof c){var n=l(e,t,c);0!==n.length?p(t).set(e,o([],n,!0)):f.$scopeOffAll(e,t)}else f.$scopeOffAll(e,t)},e.prototype.$scopeOffAll=function(e,o){p(o).delete(e)},e.prototype.$scopeEmit=function(e,t){for(var c=[],l=2;l<arguments.length;l++)c[l-2]=arguments[l];var a=o([],n(e,t),!0);a.length>0&&(a.forEach((function(e){e.once?e.emitted||(e=i.apply(void 0,o([e],c,!1))):e.timeCached&&e.timeEffect?(new Date).getTime()-e.timeEffect>=e.timeCached&&(e=i.apply(void 0,o([e],c,!1))):e=i.apply(void 0,o([e],c,!1))})),p(t).set(e,a))},e.prototype.$clearAllEventName=function(e){!function(e){var c="eventBusRegister_".concat(t),n=window.top[c]||{},l={};Object.keys(n).forEach((function(t){var c=n[t].filter((function(o){return o!==e}));c.length>0&&(l[t]=o([],c,!0)),f.$scopeOffAll(e,t)})),window.top[c]=l}(e)},e}());f.$once,f.$on,f.$off,f.$offAll,f.$emit,f.$onCached,f.$onMultiple,f.$scopeOnMultiple,f.$scopeOnce,f.$scopeOn,f.$scopeOff,f.$scopeOffAll,f.$scopeEmit,f.$scopeOnCached,f.$scopeOnCachedMultiple,f.$onCachedMultiple,f.$clearAllEventName;var r=f.$once,u=f.$on,s=f.$off,$=f.$offAll,d=f.$emit,v=f.$onCached,h=f.$onMultiple,m=f.$scopeOnMultiple,y=f.$scopeOnce,O=f.$scopeOn,w=f.$scopeOff,g=f.$scopeOffAll,b=f.$scopeEmit,C=f.$scopeOnCached,k=f.$scopeOnCachedMultiple,A=f.$onCachedMultiple,E=f.$clearAllEventName;export{E as $clearAllEventName,d as $emit,s as $off,$ as $offAll,u as $on,v as $onCached,A as $onCachedMultiple,h as $onMultiple,r as $once,b as $scopeEmit,w as $scopeOff,g as $scopeOffAll,O as $scopeOn,C as $scopeOnCached,k as $scopeOnCachedMultiple,m as $scopeOnMultiple,y as $scopeOnce,f as default};