UNPKG

data-synchronizer

Version:

A versatile library for transferring data across multi-page applications or single page applications.

2 lines (1 loc) 10.2 kB
require("core-js/modules/es.object.set-prototype-of.js"),require("core-js/modules/es.object.assign.js"),require("core-js/modules/es.symbol.js"),require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.object.to-string.js"),require("core-js/modules/es.symbol.iterator.js"),require("core-js/modules/es.array.iterator.js"),require("core-js/modules/es.string.iterator.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/es.function.name.js"),require("core-js/modules/es.regexp.to-string.js"),require("core-js/modules/web.dom-collections.for-each.js"),require("core-js/modules/es.object.entries.js"),require("core-js/modules/es.object.keys.js"),require("core-js/modules/es.string.starts-with.js"),require("core-js/modules/es.array.map.js"),require("core-js/modules/es.array.from.js"),require("core-js/modules/es.set.js"),require("core-js/modules/es.map.js"),require("core-js/modules/es.array.slice.js"),require("core-js/modules/es.regexp.constructor.js"),require("core-js/modules/es.regexp.exec.js"),require("core-js/modules/es.string.replace.js"),require("core-js/modules/es.string.match.js"),require("core-js/modules/es.array.includes.js"),require("core-js/modules/es.string.includes.js"),require("core-js/modules/es.array.find.js"),define(["exports"],(function(e){"use strict";var n=function(e,r){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])},n(e,r)};function r(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function t(){this.constructor=e}n(e,r),e.prototype=null===r?Object.create(r):(t.prototype=r.prototype,new t)}var t=function(){return t=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},t.apply(this,arguments)};function o(e,n){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var t,o,s=r.call(e),i=[];try{for(;(void 0===n||n-- >0)&&!(t=s.next()).done;)i.push(t.value)}catch(e){o={error:e}}finally{try{t&&!t.done&&(r=s.return)&&r.call(s)}finally{if(o)throw o.error}}return i}"function"==typeof SuppressedError&&SuppressedError;var s="string",i="boolean",u="number",c="symbol",a="bigint",f="object",l=[f,function(e){var n="string"==typeof e?JSON.parse(e):e,r={};return Object.entries(n).forEach((function(e){var n=o(e,2),t=n[0],s=o(n[1],2),i=s[0],u=s[1];r[t]=O[i].from(u)})),r},function(e){var n={};return Object.entries(e).forEach((function(e){var r=o(e,2),t=r[0],s=r[1];n[t]=$(s)})),[f,n]}],p="null",d="function",g="array",y=[g,function(e){var n="string"==typeof e?JSON.parse(e):e,r=[];return n.forEach((function(e,n){var t=o(e,2),s=t[0],i=t[1];r[n]=O[s].from(i)})),r},function(e){var n=[];return e.forEach((function(e,r){n[r]=$(e)})),[g,n]}],h=["set",function(e){var n="string"==typeof e?JSON.parse(e):e,r=new Set;return n.forEach((function(e){var n=o(e,2),t=n[0],s=n[1],i=O[t].from(s);r.add(i)})),r},function(e){return["set",Array.from(e).map((function(e){return $(e)}))]}],j=["map",function(e){var n="string"==typeof e?JSON.parse(e):e,r=new Map;return n.forEach((function(e){var n=o(e,2),t=n[0],s=n[1],i=o(t,2),u=i[0],c=i[1],a=o(s,2),f=a[0],l=a[1];t=O[u].from(c),s=O[f].from(l),r.set(t,s)})),r},function(e){var n,r,t=e.entries(),s=[];try{for(var i=function(e){var n="function"==typeof Symbol&&Symbol.iterator,r=n&&e[n],t=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),u=i.next();!u.done;u=i.next()){var c=o(u.value,2),a=c[0],f=c[1],l=$(a),p=$(f);s.push([l,p])}}catch(e){n={error:e}}finally{try{u&&!u.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return["map",s]}],m="date",v="regexp",w="undefined",S="arrowFunction",b="asyncfunction",E="generatorfunction",O=[[s,function(e){return e},function(e){return[s,e]}],[i,function(e){return e},function(e){return[i,e]}],[u,function(e){return+e},function(e){return[u,e.toString()]}],[c,function(e){return Symbol.for(e)},function(e){return[c,e.description]}],[a,function(e){return BigInt(e)},function(e){return[a,e.toString()]}],l,[p,function(){return null},function(e){return[p,""+e]}],[d,function(e){var n=e.startsWith("function")?e:"function "+e;return new Function("return ".concat(n))()},function(e){return[d,e.toString()]}],y,h,j,[m,function(e){return new Date(+e)},function(e){return[m,""+e.getTime()]}],[v,function(e){var n=e.slice(1,e.lastIndexOf("/")),r=e.slice(e.lastIndexOf("/")+1);return new RegExp(n,r)},function(e){return[v,e.toString()]}],[w,function(){},function(){return[w,"undefined"]}],[S,function(e){return new Function("return ".concat(e))()},function(e){return[S,e.toString()]}],[b,function(e){return new Function("return ".concat(e))()},function(e){var n=e.toString();return n.startsWith("async function")||(n=n.replace("async","async function")),[b,n]}],[E,function(e){return new Function("return ".concat(e))()},function(e){var n=e.toString();return n.startsWith("*")&&(n="function ".concat(n)),[E,n.toString()]}]].reduce((function(e,n){var r,t=o(n,3),s=t[0],i=t[1],u=t[2];return Object.assign(e,((r={})[s]={from:i,to:u},r)),e}),{}),M="BroadcastChannel"in window,q="localStorage"in window,x=function(e){var n=JSON.parse(e),r=n.$payload;n.$id;var s=function(e,n){var r={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.indexOf(t)<0&&(r[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(t=Object.getOwnPropertySymbols(e);o<t.length;o++)n.indexOf(t[o])<0&&Object.prototype.propertyIsEnumerable.call(e,t[o])&&(r[t[o]]=e[t[o]])}return r}(n,["$payload","$id"]),i=t(t({},s),{$payload:[]}),u=o(r,2),c=u[0],a=u[1];return i.$payload=O[c].from(a),i},$=function(e){var n,r=O[(n=e,void 0===n?"undefined":null===n?"null":n instanceof Function&&function(e){var n=(null==e?void 0:e.toString())||"";return!n.match(/{[\s\S]*}/)||n.replace(/{[\s\S]*}/,"").includes("=>")}(n)?"arrowFunction":n.constructor.name.toLowerCase())];return r&&(e=r.to(e)),e},C=function(e,n){var r=$(e);return{$origin:location.href,$payload:r,$target:"string"==typeof n?n:null==n?void 0:n.source,$id:I(16)}},B=function(e,n){return!n||new RegExp(n).test(e)},I=function(e){for(var n="";n.length<e;n+=Math.random().toString(36).substr(2));return n.substr(0,e)},_=function(e){return Array.from(new Set(e))},F=new Map,J=function(e,n){"string"==typeof e&&(e=[e]),(e=_(e)).forEach((function(e){var r=new BroadcastChannel(e);r.addEventListener("message",(function(e){try{var r=x(e.data);if(!B(location.href,r.$target))return;"function"==typeof n&&n(r)}catch(e){console.error(e)}}));var t=F.get(e)||[];t.push(r),F.set(e,t)}))},N=function(e,n,r){"string"==typeof e&&(e=[e]),e=_(e);var t=C(n,r);e.forEach((function(e){var n=new BroadcastChannel(e);n.postMessage(JSON.stringify(t));var r=F.get(e)||[];r.push(n),F.set(e,r)}))},L=function(e,n){"string"==typeof e&&(e=[e]),(e=_(e)).forEach((function(e){var r=new BroadcastChannel(e);r.addEventListener("messageerror",(function(e){"function"==typeof n&&n(e)}));var t=F.get(e)||[];t.push(r),F.set(e,t)}))},P=function(e){"string"==typeof e&&(e=[e]),(e=_(e)).forEach((function(n){var r=F.get(n)||[];if(!r.length)throw new Error("the channel named ".concat(e," isn't exist"));r.forEach((function(e){e.close()})),F.delete(n)}))},T=new Map,k=function(e,n){"string"==typeof e&&(e=[e]),e=_(e),window.addEventListener("storage",(function(r){var t=r.key;if(e.includes(t))try{var o=x(r.newValue);if(!B(location.href,o.$target))return;n(o)}catch(e){console.error(e)}}))},A=function(e,n,r){"string"==typeof e&&(e=[e]),e=_(e);var t=C(n,r),o=JSON.stringify(t);e.forEach((function(e){try{window.localStorage.setItem(e,o);var n=new StorageEvent("storage",{key:e,newValue:o});window.dispatchEvent(n)}catch(n){(T.get(e)||[]).forEach((function(e){e(n)}))}}))},D=function(e,n){"string"==typeof e&&(e=[e]),(e=_(e)).forEach((function(e){var r=T.get(e);r?r.push(n):r=[n],T.set(e,r)}))},W=function(e){var n=localStorage.setItem;"string"==typeof e&&(e=[e]),e=_(e),localStorage.setItem=function(r,t){e.includes(r)||n(r,t)};var r=localStorage.getItem;localStorage.getItem=function(n){if(!e.includes(n))return r(n)}},z=function(){},R=function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return r(n,e),n.prototype.onMessage=function(e,n){k(e,n)},n.prototype.onSendMessageError=function(e,n){D(e,n)},n.prototype.sendMessage=function(e,n,r){A(e,n,r)},n.prototype.close=function(e){W(e)},n}(z),V=function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return r(n,e),n.prototype.onMessage=function(e,n){J(e,n)},n.prototype.sendMessage=function(e,n,r){N(e,n,r)},n.prototype.onSendMessageError=function(e,n){L(e,n)},n.prototype.close=function(e){P(e)},n}(z),G=M||q,H=function(){function e(e){if(this.options={engine:"BroadcastChannel"},!G)throw new Error("The library doesn't support your browser.");Object.assign(this.options,e),this.initEngine()}return e.prototype.initEngine=function(){var e=this.options.engine,n={BroadcastChannel:V,LocalStorage:R};this.instance=new n[e](this.options)},e.prototype.onMessage=function(e,n){this.instance.onMessage(e,n)},e.prototype.sendMessage=function(e,n,r){this.instance.sendMessage(e,n,r)},e.prototype.onMessageError=function(e,n){this.instance.onSendMessageError(e,n)},e.prototype.close=function(e){this.instance.close(e)},e}(),K=M||q;e.DataSynchronizer=H,e.useDataSynchronizer=function(e){if(!K)throw new Error("the lib isn't support your browser.");e=Object.assign({engine:"BroadcastChannel"},e);var n=[{engine:"BroadcastChannel",support:M,onMessage:J,sendMessage:N,onSendMessageError:L,close:P},{engine:"LocalStorage",support:q,onMessage:k,sendMessage:A,onSendMessageError:D,close:W}].find((function(n){return n.engine===e.engine&&n.support}));return{onMessage:function(e,r){return n.onMessage(e,r)},sendMessage:function(e,r,t){return n.sendMessage(e,r,t)},onMessageError:function(e,r){return n.onSendMessageError(e,r)},close:function(e){return n.close(e)}}}}));