UNPKG

@lomray/react-mobx-manager

Version:
3 lines (2 loc) 1.42 kB
import t from"../deep-compare.js";class e{storages;persistData={};defaultId;constructor(t){this.storages=t,this.defaultId=Object.keys(t)?.[0]}async get(){try{const t=await Promise.all(Object.values(this.storages).map((t=>t.get()||{})));return this.persistData=Object.keys(this.storages).reduce(((e,s,r)=>({...e,[s]:t[r]})),{}),this.persistData}catch(t){return{}}}flush(){return Promise.all(Object.values(this.storages).map((t=>t.flush())))}set(t,e){const s=this.storages[e??this.defaultId];if(s)return s.set(t)}getStoreOptions(t){return{attributes:{[this.defaultId]:["*"]},behaviour:"exclude",...t.libStorageOptions??{}}}getStoreData(t){const e=t.libStoreId,{attributes:s}=this.getStoreOptions(t);return Object.entries(s).reduce(((t,[s,r])=>{const a=this.persistData[s]?.[e]??{};return{...t,..."*"===r[0]?a:r.reduce(((t,e)=>({...t,...void 0!==a[e]?{[e]:a[e]}:{}})),{})}}),{})}async saveStoreData(e,s){const r=e.libStoreId,{attributes:a,behaviour:i}=this.getStoreOptions(e),o=new Set(Object.keys(s??{})),u=Object.entries(a).map((([e,a])=>{const u=("*"===a[0]?[...o]:a).reduce(((t,e)=>o.has(e)?("exclude"===i&&o.delete(e),{...t,[e]:s?.[e]}):t),{}),c={...this.persistData?.[e]??{},[r]:u};return t(this.persistData?.[e]?.[r]??{},u)?null:(this.persistData[e]?.[r]||(this.persistData[e][r]={}),this.persistData[e][r]=u,this.set(c,e))}));await Promise.all(u)}}export{e as default}; //# sourceMappingURL=combined-storage.js.map