cross-window-emitter
Version:
Co-domain cross-window event triggers
3 lines (2 loc) • 8.81 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).crossWindowEmitter={})}(this,(function(t){"use strict";var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},e(t,r)};var r=function(){return r=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var n=function(){function t(){this.handlers={}}return t.prototype.eventNames=function(){var t,e=(null===(t=Object.getOwnPropertySymbols)||void 0===t?void 0:t.call(Object,this.handlers))||[];return Object.keys(this.handlers).concat(e)},t.prototype.rawListeners=function(t){var e=this.handlers[t];return e?e.map((function(t){return t.raw})):[]},t.prototype.listeners=function(t){var e=this.handlers[t];return e?e.map((function(t){return t.wrap})):[]},t.prototype.hasListener=function(t,e){return this.rawListeners(t).some((function(t){return t===e}))},t.prototype._on=function(t,e,r,n,o){void 0===n&&(n=null),void 0===o&&(o=1);var i={raw:e,wrap:r,context:n};if(this.handlers[t]){var s=1===o?"push":"unshift";this.handlers[t][s](i)}else this.handlers[t]=[i];return this},t.prototype.prependListener=function(t,e,r){return this._on(t,e,e,r,0)},t.prototype.on=function(t,e,r){return this._on(t,e,e,r)},t.prototype._wrapOnce=function(t,e,r){var n=this;void 0===r&&(r=null);var o=function(){for(var i=[],s=0;s<arguments.length;s++)i[s]=arguments[s];e.apply(r,i),n.off(t,o)};return o},t.prototype.once=function(t,e,r){var n=this._wrapOnce(t,e,r);return this._on(t,e,n,r)},t.prototype.prependOnceListener=function(t,e,r){var n=this._wrapOnce(t,e,r);return this._on(t,e,n,r,0)},t.prototype.off=function(t,e){var r=this.handlers[t];if(r)if(e){var n=r.findIndex((function(t){return t.wrap===e||t.raw===e}));-1!==n&&r.splice(n,1)}else delete this.handlers[t];return this},t.prototype.offAll=function(){return this.handlers={},this},t.prototype.emit=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];var n=this.handlers[t];return!!(n&&n.length>0)&&(n.forEach((function(t){t.wrap.apply(t.context,e)})),!0)},t}();var o=1;function i(){return"".concat(Math.random().toString(16).substring(2,8),"_").concat(o++)}var s={},a=function(){function t(t){void 0===t&&(t="default"),this.scope=t,s[this.scope]||(s[this.scope]={}),this.data=s[this.scope]}return t.prototype.getItem=function(t){return t in this.data?this.data[t]:null},t.prototype.setItem=function(t,e){this.data[t]=e},t.prototype.removeItem=function(t){delete this.data[t]},t.prototype.clear=function(){s[this.scope]={},this.data=s[this.scope]},t}(),c=function(){function t(t,e){void 0===e&&(e={});var n=!!t&&function(t){try{var e="object"==typeof t&&null!==t&&!!t.setItem&&!!t.getItem&&!!t.removeItem;if(e){var r=i();if(t.setItem(r,"1"),"1"!==t.getItem(r))return!1;t.removeItem(r)}return e}catch(e){return console.error("[cache2] ".concat(t," is not supported. The default memory cache will be used.")),!1}}(t);this.options=r({needParsed:n},e),this.storage=n?t:new a(this.options.memoryScope)}return t.prototype.get=function(t){var e=this.storage.getItem(t);return this.options.needParsed?function(t,e){try{return JSON.parse(t,e)}catch(e){return t}}(e,this.options.reviver):e},t.prototype.set=function(t,e){var r,n;this.storage.setItem(t,this.options.needParsed?(r=e,n=this.options.replacer,JSON.stringify(r,n)):e)},t.prototype.del=function(t){this.storage.removeItem(t)},t.prototype.clear=function(){"function"==typeof this.storage.clear&&this.storage.clear()},t}(),u=function(t){function n(e,n){var o,s=t.call(this)||this,a="default";return"string"==typeof e?a=e:"object"==typeof e&&(o=e),o||"object"!=typeof n||(o=n),s.options=r({max:-1,stdTTL:0,maxStrategy:"limited",checkperiod:0,prefix:"cache2_"},o),s.storage=new c(s.options.storage,r({memoryScope:a},o)),s.cacheKey=(s.options.prefix||"")+(a||"")||i(),s.startCheckperiod(),s}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}(n,t),n.prototype._check=function(t,e){var r=!0;return 0!==e.t&&e.t<Date.now()&&(r=!1,this.del(t),this.emit("expired",t,e.v)),r},n.prototype._wrap=function(t,e){var r=Date.now(),n="number"==typeof e?e:this.options.stdTTL;return{v:t,t:n>0?r+n:0,n:r}},n.prototype._isLimited=function(t){return this.options.max>-1&&t>=this.options.max},n.prototype._getReplaceKey=function(t,e){var r=t[0];return t.forEach((function(t){(e[t].t<e[r].t||e[t].t===e[r].t&&e[t].n<e[r].n)&&(r=t)})),r},Object.defineProperty(n.prototype,"cacheValues",{get:function(){return this.storage.get(this.cacheKey)||{}},enumerable:!1,configurable:!0}),n.prototype.setCacheValues=function(t){this.storage.set(this.cacheKey,t)},n.prototype.get=function(t){var e=this.cacheValues[t];if(e&&this._check(t,e))return e.v},n.prototype.mget=function(t){var e=this,r={};if(!Array.isArray(t))return r;var n=this.cacheValues;return t.forEach((function(t){var o=n[t];o&&e._check(t,o)&&(r[t]=o.v)})),r},n.prototype.getAll=function(){var t=Object.keys(this.cacheValues);return this.mget(t)},n.prototype.set=function(t,e,r){if(0===this.options.max)return!1;var n=this.cacheValues,o=Object.keys(n);if(!n[t]&&this._isLimited(o.length)){var i=this.keys();if(this._isLimited(i.length)){if("replaced"!==this.options.maxStrategy)return!1;var s=this._getReplaceKey(i,n);this.del(s)}}return n[t]=this._wrap(e,r),this.setCacheValues(n),this.emit("set",t,n[t].v),!0},n.prototype.mset=function(t){var e=this,r=!0;return t.forEach((function(t){var n=e.set(t.key,t.value,t.ttl);r&&!n&&(r=!1)})),r},n.prototype.del=function(t){var e=this,r=this.cacheValues,n=0;return(Array.isArray(t)?t:[t]).forEach((function(t){if(r[t]){n++;var o=r[t];delete r[t],e.emit("del",t,o.v)}})),n>0&&this.setCacheValues(r),n},n.prototype.clear=function(){this.storage.del(this.cacheKey)},n.prototype.keys=function(){var t=this,e=this.cacheValues;return Object.keys(e).filter((function(r){return t._check(r,e[r])}))},n.prototype.has=function(t){var e=this.cacheValues[t];return!(!e||!this._check(t,e))},n.prototype.take=function(t){var e,r=this.cacheValues[t];return r&&this._check(t,r)&&(e=r.v,this.del(t)),e},n.prototype.ttl=function(t,e){var r=this.cacheValues,n=r[t];return!(!n||!this._check(t,n))&&(r[t]=this._wrap(n.v,e),!0)},n.prototype.getTtl=function(t){var e=this.cacheValues,r=e[t];if(r&&this._check(t,r))return e[t].t},n.prototype.getLastModified=function(t){var e=this.cacheValues,r=e[t];if(r&&this._check(t,r))return e[t].n},n.prototype.startCheckperiod=function(){var t=this;this.keys(),this.options.checkperiod>0&&(clearTimeout(this._checkTimeout),this._checkTimeout=setTimeout((function(){t.startCheckperiod()}),this.options.checkperiod))},n.prototype.stopCheckperiod=function(){clearTimeout(this._checkTimeout)},n}(n),h=18e5,p={data:{},add:function(t,e){this.data[t]||(this.data[t]=[]),this.data[t].push({timestamp:Date.now(),fn:e})},remove:function(t,e){this.data[t]&&e?this.data[t]=this.data[t].filter((function(t){return t.fn!==e})):this.data[t]=[]},get:function(t){return this.data[t]||[]},has:function(t){return this.get(t).length>0}},f=new u("__private_cross_window_emitter__",{stdTTL:h,storage:window.localStorage}),l=function(t,e){return function(){var r=f.get(t),n=p.get(t);r&&n.forEach((function(t,o){var i=t.timestamp,s=t.fn;i<r.timestamp&&(null==e||e(),n[o].timestamp=Date.now(),s.apply(null,r.params))}))}},d={data:{},start:function(t,e,r){if(void 0===r&&(r=500),t){this.data[t]||(this.data[t]={timestamp:Date.now(),pollingInterval:r,timer:null});var n=this.data[t];n.pollingInterval=r,clearInterval(n.timer),n.timer=setInterval(e,r)}},stop:function(t){t&&this.data[t]&&clearInterval(this.data[t].timer)},setPollingInterval:function(t,e){if(void 0===e&&(e=500),t&&this.data[t]&&e){e>9e5&&(console.warn("polling interval no more than ".concat(h,".")),e=9e5);var r=this.data[t];clearInterval(r.timer),this.start(t,l(t),e)}}};t.emit=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];f.set(t,{timestamp:Date.now(),params:e||[]})},t.off=function(t,e){p.remove(t,e),p.has(t)||d.stop(t)},t.on=function(t,e){p.add(t,e),d.start(t,l(t))},t.once=function(t,e){var r=!1;p.add(t,e),d.start(t,(function(){r&&(p.remove(t,e),p.has(t)||d.stop(t)),l(t,(function(){r=!0}))()}))},t.setPollingInterval=function(t,e){d.setPollingInterval(t,e)}}));
//# sourceMappingURL=cross-window-emitter.min.js.map