@lastos/page-spy-plugin-rn-async-storage
Version:
A PageSpy plugin, used for React Native app with async-storage.
3 lines (2 loc) • 3.7 kB
JavaScript
;var e=require("@react-native-async-storage/async-storage");function t(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var o=n.call(e,t||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function n(e,n,o){return(n=t(n))in e?Object.defineProperty(e,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[n]=o,e}const o={...console};["log","info","error","warn","debug"].reduce(((e,t)=>(e[t]=function(){for(var e=arguments.length,n=new Array(e),o=0;o<e;o++)n[o]=arguments[o];console[t]("[PageSpy] [".concat(t.toLocaleUpperCase(),"] "),...n)},e.unproxy[t]=function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];o[t]("[PageSpy] [".concat(t.toLocaleUpperCase(),"] "),...n)},e)),{unproxy:{}});const r={configurable:!0,writable:!0,enumerable:!0};class i{constructor(){n(this,"name","RNAsyncStoragePlugin"),n(this,"$socketStore",null),n(this,"$pageSpyConfig",null)}onInit(e){i.hasInitd||(i.hasInitd=!0,this.$pageSpyConfig=e.config,this.$socketStore=e.socketStore,this.initStorageProxy(),this.listenRefreshEvent())}onReset(){Object.entries(i.originFunctions).forEach((t=>{let[n,o]=t;Object.defineProperty(e,n,{value:o,...r})})),i.hasInitd=!1}async sendRefresh(){try{const t=await e.getAllKeys(),n={type:"asyncStorage",action:"get",data:(await e.multiGet(t)).filter((e=>"string"==typeof e[1])).map((e=>({name:e[0],value:e[1]})))};this.sendStorageItem(n)}catch(e){}}listenRefreshEvent(){var e;null===(e=this.$socketStore)||void 0===e||e.addListener("refresh",(async e=>{let{source:t}=e;const{data:n}=t;"asyncStorage"===n&&this.sendRefresh()}))}initStorageProxy(){["setItem","mergeItem","multiSet","multiMerge","removeItem","multiRemove","clear"].forEach((t=>{e[t]&&(i.originFunctions[t]=e[t])}));const t=this;Object.defineProperties(e,{setItem:{value:(e,n,o)=>i.originFunctions.setItem(e,n,o).then((()=>{t.sendSetItem(e,n)})),...r},mergeItem:{value:(n,o,r)=>i.originFunctions.mergeItem(n,o,r).then((o=>(e.getItem(n).then((e=>{null!==e?t.sendSetItem(n,e):t.sendRemoveItem(n)})),o))),...r},multiSet:{value:(e,n)=>i.originFunctions.multiSet(e,n).then((()=>{e.forEach((e=>{t.sendSetItem(e[0],e[1])}))})),...r},multiMerge:{value:(n,o)=>i.originFunctions.multiMerge(n,o).then((()=>{const o=n.map((e=>e[0]));e.multiGet(o).then((e=>{e.forEach((e=>{null!==e[1]?t.sendSetItem(e[0],e[1]):t.sendRemoveItem(e[0])}))}))})),...r},removeItem:{value:(e,n)=>i.originFunctions.removeItem(e,n).then((()=>{t.sendRemoveItem(e)})),...r},multiRemove:{value:(e,n)=>i.originFunctions.multiRemove(e,n).then((()=>{e.forEach((e=>{t.sendRemoveItem(e)}))})),...r},clear:{value:e=>i.originFunctions.clear(e).then((()=>{t.sendClearItem()})),...r}})}sendSetItem(e,t){this.sendStorageItem({type:"asyncStorage",action:"set",name:e,value:t})}sendRemoveItem(e){this.sendStorageItem({type:"asyncStorage",action:"remove",name:e})}sendClearItem(){this.sendStorageItem({type:"asyncStorage",action:"clear"})}sendStorageItem(e){var t,n,o,r;if(!1===(null===(t=this.$pageSpyConfig)||void 0===t||null===(t=t.dataProcessor)||void 0===t||null===(n=t.storage)||void 0===n?void 0:n.call(t,e)))return;const i=function(e,t){return{role:"client",type:e,data:{...(!(arguments.length>2&&void 0!==arguments[2])||arguments[2])&&{id:Math.random().toString(36).slice(2)},...t}}}("storage",e);null===(o=this.$socketStore)||void 0===o||o.dispatchEvent("public-data",i),null===(r=this.$socketStore)||void 0===r||r.broadcastMessage(i,!0)}}n(i,"hasInitd",!1),n(i,"originFunctions",{}),module.exports=i;
//# sourceMappingURL=index.min.js.map