UNPKG

eventbus-z

Version:

Simple event emitter. Global event emitter

2 lines (1 loc) 4.44 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).evtBus={})}(this,(function(e){"use strict";var o=function(){return o=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},o.apply(this,arguments)};function t(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 c="evtBusZxp_scopeZxp",n="globalZxp_defaultName",l=function(e,o){var t;void 0===o&&(o=n);var l="".concat(c,"__").concat(o);return(null===(t=window.top[l])||void 0===t?void 0:t.get(e))||[]},p=function(e,o,t){return void 0===o&&(o=n),l(e,o).filter((function(e){return e.callback!==t}))},i=function(e){void 0===e&&(e=n);var o="".concat(c,"__").concat(e);return window.top[o]||(window.top[o]=new Map),window.top[o]},a=function(e,t,l){if(void 0===l&&(l=!1),!e||!t.callback||"function"!=typeof t.callback)throw new Error("name or callback (function) is required");var a=o(o({},t),{once:l});if(function(e,o){void 0===o&&(o=n);var t="eventBusRegister_".concat(c),l=window.top[t]||{};l&&l[o]||(l[o]=[]),l[o].push(e),window.top[t]=l}(e,t.scopeName),t.single)i(t.scopeName).set(e,[a]);else{var f=p(e,t.scopeName,t.callback);f.push(a),i(t.scopeName).set(e,f)}},f=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},r=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){s.$scopeOff(e,n,o)},e.prototype.$offAll=function(e){i(n).delete(e)},e.prototype.$emit=function(e){for(var o=[],c=1;c<arguments.length;c++)o[c-1]=arguments[c];s.$scopeEmit.apply(s,t([e,n],o,!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,o,c){if(c&&"function"==typeof c){var n=p(e,o,c);0!==n.length?i(o).set(e,t([],n,!0)):s.$scopeOffAll(e,o)}else s.$scopeOffAll(e,o)},e.prototype.$scopeOffAll=function(e,o){i(o).delete(e)},e.prototype.$scopeEmit=function(e,o){for(var c=[],n=2;n<arguments.length;n++)c[n-2]=arguments[n];var p=t([],l(e,o),!0);p.length>0&&(p.forEach((function(e){e.once?e.emitted||(e=f.apply(void 0,t([e],c,!1))):e.timeCached&&e.timeEffect?(new Date).getTime()-e.timeEffect>=e.timeCached&&(e=f.apply(void 0,t([e],c,!1))):e=f.apply(void 0,t([e],c,!1))})),i(o).set(e,p))},e.prototype.$clearAllEventName=function(e){!function(e){var o="eventBusRegister_".concat(c),n=window.top[o]||{},l={};Object.keys(n).forEach((function(o){var c=n[o].filter((function(o){return o!==e}));c.length>0&&(l[o]=t([],c,!0)),s.$scopeOffAll(e,o)})),window.top[o]=l}(e)},e}(),s=new r;s.$once,s.$on,s.$off,s.$offAll,s.$emit,s.$onCached,s.$onMultiple,s.$scopeOnMultiple,s.$scopeOnce,s.$scopeOn,s.$scopeOff,s.$scopeOffAll,s.$scopeEmit,s.$scopeOnCached,s.$scopeOnCachedMultiple,s.$onCachedMultiple,s.$clearAllEventName;var u=s.$once,$=s.$on,d=s.$off,v=s.$offAll,h=s.$emit,m=s.$onCached,y=s.$onMultiple,O=s.$scopeOnMultiple,b=s.$scopeOnce,g=s.$scopeOn,w=s.$scopeOff,C=s.$scopeOffAll,M=s.$scopeEmit,k=s.$scopeOnCached,A=s.$scopeOnCachedMultiple,E=s.$onCachedMultiple,N=s.$clearAllEventName;e.$clearAllEventName=N,e.$emit=h,e.$off=d,e.$offAll=v,e.$on=$,e.$onCached=m,e.$onCachedMultiple=E,e.$onMultiple=y,e.$once=u,e.$scopeEmit=M,e.$scopeOff=w,e.$scopeOffAll=C,e.$scopeOn=g,e.$scopeOnCached=k,e.$scopeOnCachedMultiple=A,e.$scopeOnMultiple=O,e.$scopeOnce=b,e.default=s,Object.defineProperty(e,"__esModule",{value:!0})}));