UNPKG

@lomray/react-mobx-manager

Version:
3 lines (2 loc) 7.33 kB
import t from"@lomray/event-manager";import{toJS as e,isObservableProp as s}from"mobx";import{ROOT_CONTEXT_ID as o}from"./constants.js";import r from"./deep-merge.js";import i from"./events.js";import n from"./logger.js";import{isPropObservableExported as a,isPropExcludedFromExport as l,isPropSimpleExported as h}from"./make-exported.js";import d from"./on-change-listener.js";import p from"./storages/combined-storage.js";import S from"./store-status.js";import c from"./wakeup.js";class u{static instance;stores=new Map;storesRelations=new Map;static persistedStores=new Set;initState;storage;storesParams;options={shouldDisablePersist:!1,shouldRemoveInitState:!0,failedCreationStrategy:"empty"};suspenseRelations=new Map;logger;constructor({initState:t,storesParams:e,storage:s,options:o,logger:r}={}){if(this.initState=t||{},this.storesParams=e||{},this.logger=r&&"log"in r?r:new n({level:3,...r??{},manager:this}),this.storage=s instanceof p?s:s?new p({default:s}):void 0,Object.assign(this.options,o||{}),u.instance=this,"undefined"!=typeof window){const t=window.mbxM;window.mbxM={push:this.pushInitState},(Array.isArray(t)?t:[]).forEach(this.pushInitState)}}async init(){try{this.storage&&await this.storage.get()}catch(t){this.logger.err("Failed initialized store manager: ",t)}return this}static get(){if(!u.instance)throw new Error("Store manager is not initialized.");return u.instance}getStores(){return this.stores}getStoresRelations(){return this.storesRelations}getSuspenseRelations(){return this.suspenseRelations}static getPersistedStoresIds(){return u.persistedStores}pushInitState=(t={})=>{for(const[e,s]of Object.entries(t))this.initState[e]=s};getStoreId(t,e={}){const{id:s,contextId:o,key:r}=e;if(s)return s;if(t.libStoreId)return t.libStoreId;let i=t.id||t.name||t.constructor.name;return t.isGlobal?i:(i=`${i}--${o}`,r?`${i}--${r}`:i)}getStore(t,e={}){const s=this.getStoreId(t,e);return this.stores.has(s)?this.stores.get(s):t.isGlobal?this.createStore(t,{id:s,contextId:"global",parentId:o,suspenseId:"",componentName:"root-app",componentProps:{}}):this.lookupStore(s,e)}lookupStore(t,e){const{contextId:s,parentId:r}=e,i=t.split("--")?.[0],{ids:n,parentId:a}=this.storesRelations.get(s)??{ids:new Set,parentId:r},l=[...n].filter((t=>t.startsWith(`${i}--`)));if(1===l.length)return this.stores.get(l[0]);if(l.length>1)this.logger.err("Parent context has multiple stores with the same id, please pass key to getStore function.");else if(a&&a!==o)return this.lookupStore(t,{contextId:this.getBiggerContext(a,r)})}getBiggerContext(t,e){if(!t)return e;if(!e)return t;const s=/[^a-zA-Z]/g;return t.replace(s,"")>e.replace(s,"")?t:e}createStore(e,s){const{id:r,contextId:n,parentId:a,suspenseId:l,componentName:h,componentProps:d}=s;if(this.stores.has(r))return this.stores.get(r);const p=new e({...this.storesParams,storeManager:this,getStore:(t,e={contextId:n,parentId:a})=>this.getStore(t,e),componentProps:d,initState:this.initState[r]});return p.libStoreId=r,p.isGlobal=e.isGlobal,p.libStoreContextId=e.isGlobal?"global":n,p.libStoreParentId=e.isGlobal||!a||a===n?o:a,p.libStoreSuspenseId=l,p.libStoreComponentName=h,this.setStoreStatus(p,e.isGlobal?S.inUse:S.init),this.prepareStore(p),t.publish(i.CREATE_STORE,{store:e}),p}createStores(t,e,s,o,r,i={}){const{failedCreationStrategy:n}=this.options,a=t.reduce(((t,[a,l])=>{const{id:h,store:d,isParent:p=!1}="store"in l?l:{store:l,id:void 0,isParent:!1};let S=h||(p?this.getStore(d,{contextId:s,parentId:e})?.libStoreId:this.getStoreId(d,{key:a,contextId:s}));if(!S){const i=`Cannot find or create store '${a}': '${this.getStoreId(d)}'`;if(this.logger.warn(i),this.logger.debug(i,{contextId:s,parentId:e,suspenseId:o,componentName:r,isParent:p},!0),"dummy"!==n)return"empty"===n&&(t.hasCreationFailure=!0),t;S=this.getStoreId(d,{key:a,contextId:s})}const c=this.createStore(d,{id:S,contextId:s,parentId:e,suspenseId:o,componentName:r,componentProps:i});return p?t.parentStores[a]=c:c.isGlobal?t.globalStores[a]=c:t.relativeStores[a]=c,t}),{relativeStores:{},parentStores:{},globalStores:{},hasCreationFailure:!1});return this.createRelationContext(s,e,r),a}createRelationContext(t,e,s){this.storesRelations.has(t)||this.storesRelations.set(t,{ids:new Set,parentId:e&&e!==t?e:o,componentName:s})}removeRelationContext(t){const e=this.storesRelations.get(t);!e||t===o||e.ids.size>0||this.storesRelations.delete(t)}prepareStore(t){const e=t.libStoreId,s=t.libStoreContextId,o=t.libStoreSuspenseId;if(this.stores.has(e))return;const i=this.initState[e];if(i&&r(t,i),"wakeup"in t&&u.persistedStores.has(e)&&t.wakeup?.({initState:i,persistedState:this.storage?.getStoreData(t),manager:this}),u.persistedStores.has(e)&&"addOnChangeListener"in t){const e=t.onDestroy?.bind(t),s=t.addOnChangeListener(t,this);t.onDestroy=()=>{s?.(),e?.()}}t.init?.(),this.createRelationContext(s,t.libStoreParentId,t.libStoreComponentName),this.suspenseRelations.has(o)||this.suspenseRelations.set(o,new Set);const{ids:n}=this.storesRelations.get(s);this.stores.set(e,t),n.add(e),this.suspenseRelations.get(o).add(e)}removeStore(e){const s=e.libStoreId,o=e.libStoreSuspenseId,{ids:r}=this.storesRelations.get(e.libStoreContextId)??{ids:new Set};this.stores.has(s)&&(this.stores.delete(s),r.delete(s),o&&this.suspenseRelations.get(o)?.has(s)&&this.suspenseRelations.get(o).delete(s),this.removeRelationContext(e.libStoreContextId),"onDestroy"in e&&e.onDestroy?.(),t.publish(i.DELETE_STORE,{store:e}))}mountStores(e,{globalStores:s={},relativeStores:o={}}){const{shouldRemoveInitState:r}=this.options,n={...s,...o};return Object.values(n).forEach((e=>{const s=e.libStoreId;r&&this.initState[s]&&delete this.initState[s],this.setStoreStatus(e,S.inUse),t.publish(i.MOUNT_STORE,{store:e})})),()=>{Object.values(n).forEach((e=>{e.isGlobal||(this.setStoreStatus(e,S.unused),t.publish(i.UNMOUNT_STORE,{store:e}))})),this.removeRelationContext(e)}}touchedStores(t){Object.values(t).forEach((t=>{t.libStoreStatus!==S.init||t.isGlobal||this.setStoreStatus(t,S.touched)}))}setStoreStatus(t,e){const{destroyTimers:{init:s=500,touched:o=1e4,unused:r=1e3}={}}=this.options;t.libStoreStatus=e,clearTimeout(t.libDestroyTimer);let i=0;switch(e){case S.init:i=s;break;case S.touched:i=o;break;case S.unused:i=r}i&&(t.libDestroyTimer=setTimeout((()=>this.removeStore(t)),i))}getStoreState(t,e=!1){return t.toJSON?.()??u.getObservableProps(t,e)}toJSON(t,e=!1){const s={},o=Array.isArray(t)?t.reduce(((t,e)=>(this.stores.has(e)&&t.set(e,this.stores.get(e)),t)),new Map):this.stores;for(const[t,r]of o.entries())s[t]=this.getStoreState(r,e);return s}async savePersistedStore(t){if(this.options.shouldDisablePersist||!this.storage)return!1;try{return await this.storage.saveStoreData(t,this.getStoreState(t,!0)),!0}catch(t){this.logger.err("Failed to persist stores: ",t)}return!1}static getObservableProps(t,o=!1){const r=e(t);return Object.entries(r).reduce(((e,[r,i])=>({...e,...s(t,r)&&!l(t,r,o)||h(t,r)?{[r]:i}:{},...a(t,r)?{[r]:u.getObservableProps(t[r])}:{}})),{})}static persistStore(t,e,s={}){return u.persistedStores.add(e),t.libStoreId=e,"libStorageOptions"in t.prototype||(t.prototype.libStorageOptions=s),"wakeup"in t.prototype||(t.prototype.wakeup=c),"addOnChangeListener"in t.prototype||(t.prototype.addOnChangeListener=d),t}}export{u as default}; //# sourceMappingURL=manager.js.map