@lomray/react-mobx-manager 1.4.1-beta.1 → 1.4.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/manager.d.ts CHANGED
@@ -20,11 +20,6 @@ declare class Manager {
20
20
  parentId: string | null;
21
21
  componentName?: string | undefined;
22
22
  }>;
23
- /**
24
- * Created stores size
25
- * @protected
26
- */
27
- protected storesPool: number;
28
23
  /**
29
24
  * Save persisted stores identities
30
25
  * @private
@@ -151,7 +146,16 @@ declare class Manager {
151
146
  * Prepare store before usage
152
147
  * @protected
153
148
  */
154
- protected prepareStore(store: TStores[string]): Required<IStoreLifecycle>['onDestroy'][];
149
+ protected prepareStore(store: TStores[string]): void;
150
+ /**
151
+ * Prepare store before mount to component
152
+ * @protected
153
+ */
154
+ /**
155
+ * Prepare store before mount to component
156
+ * @protected
157
+ */
158
+ protected prepareMount(store: TStores[string]): Required<IStoreLifecycle>['onDestroy'][];
155
159
  /**
156
160
  * Mount stores to component
157
161
  */
package/lib/manager.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("mobx"),t=require("./on-change-listener.js"),s=require("./wakeup.js");class o{constructor({initState:e,storesParams:t,storage:s,options:r}={}){Object.defineProperty(this,"stores",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"storesRelations",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"storesPool",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"initState",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"storage",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"persistData",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"storesParams",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"options",{enumerable:!0,configurable:!0,writable:!0,value:{shouldDisablePersist:!1,shouldRemoveInitState:!0,isSSR:!1}}),this.initState=e||{},this.storesParams=t||{},this.storage=s,Object.assign(this.options,r||{}),o.instance=this}async init(){return this.storage&&(this.persistData=await this.storage.get()||{}),this}static get(){if(!o.instance)throw new Error("Store manager is not initialized.");return o.instance}getStores(){return this.stores}getStoresRelations(){return this.storesRelations}static getPersistedStoresIds(){return o.persistedStores}getStoreId(e,t={}){const{id:s,contextId:o,key:r}=t;if(s)return s;if(e.libStoreId)return e.libStoreId;let i=e.name||e.constructor.name;return e.isSingleton?i:(i=`${i}--${o}`,r?`${i}--${r}`:i)}createContextId(e){return`ctx${e||this.storesPool+1}`}getStore(e,t={}){const s=this.getStoreId(e,t);return this.stores.has(s)?this.stores.get(s):e.isSingleton?this.createStore(e,{id:s,contextId:"singleton",parentId:"root",componentName:"root-app",componentProps:{}}):this.lookupStore(s,t)}lookupStore(e,t){var s,o;const{contextId:r,parentId:i}=t,n=null===(s=e.split("--"))||void 0===s?void 0:s[0],{ids:a,parentId:l}=null!==(o=this.storesRelations.get(r))&&void 0!==o?o:{ids:new Set,parentId:i},d=[...a].filter((e=>e.startsWith(`${n}--`)));if(1===d.length)return this.stores.get(d[0]);if(d.length>1)console.error("Parent context has multiple stores with the same id, please pass key to getStore function.");else if(l&&"root"!==l)return this.lookupStore(e,{contextId:l})}createStore(e,t){var s,r;const{isSSR:i}=this.options,{id:n,contextId:a,parentId:l,componentName:d,componentProps:c}=t;if((e.isSingleton||i)&&this.stores.has(n))return this.stores.get(n);const u=new e({...this.storesParams,storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),componentProps:c});u.libStoreId=n,u.isSingleton=e.isSingleton,u.libStoreContextId=e.isSingleton?"singleton":a,u.libStoreParentId=e.isSingleton||!l||l===a?"root":l,u.libStoreComponentName=d;const p=this.initState[n],h=this.persistData[n];return p&&Object.assign(u,p),"wakeup"in u&&o.persistedStores.has(n)&&(null===(s=u.wakeup)||void 0===s||s.call(u,u,{initState:p,persistedState:h})),null===(r=u.init)||void 0===r||r.call(u),(u.isSingleton||i)&&this.prepareStore(u),this.storesPool+=1,u}createStores(e,t,s,o,r={}){return e.reduce(((e,[i,n])=>{const[a,l]="store"in n?[n.store,n.id]:[n,this.getStoreId(n,{key:i,contextId:s})];return{...e,[i]:this.createStore(a,{id:l,contextId:s,parentId:t,componentName:o,componentProps:r})}}),{})}prepareStore(e){const{shouldRemoveInitState:t}=this.options,s=e.libStoreId,r=e.libStoreContextId,i=[];this.storesRelations.has(r)||this.storesRelations.set(r,{ids:new Set,parentId:e.libStoreParentId&&e.libStoreParentId!==r?e.libStoreParentId:"root",componentName:e.libStoreComponentName});const{ids:n}=this.storesRelations.get(r);return o.persistedStores.has(s)&&"addOnChangeListener"in e&&i.push(e.addOnChangeListener(e,this)),t&&this.initState[s]&&delete this.initState[s],this.stores.has(s)||(this.stores.set(s,e),n.add(s)),i}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var s;if(t.push(...this.prepareStore(e)),"onMount"in e){const o=null===(s=e.onMount)||void 0===s?void 0:s.call(e);"function"==typeof o&&t.push(o)}"onDestroy"in e&&t.push((()=>{var t;return null===(t=e.onDestroy)||void 0===t?void 0:t.call(e)}))})),()=>{t.forEach((e=>e())),Object.values(e).forEach((e=>{var t;const s=e.libStoreId;if(!e.isSingleton){const{ids:o}=null!==(t=this.storesRelations.get(e.libStoreContextId))&&void 0!==t?t:{ids:new Set};this.stores.delete(s),o.delete(s),this.storesPool=this.storesPool-1,o.size||this.storesRelations.delete(e.libStoreContextId)}}))}}toJSON(){var e,t;const s={};for(const[r,i]of this.stores.entries())s[r]=null!==(t=null===(e=i.toJSON)||void 0===e?void 0:e.call(i))&&void 0!==t?t:o.getObservableProps(i);return s}toPersistedJSON(){var e,t;const s={};for(const r of o.persistedStores){const i=this.stores.get(r);i&&(s[r]=null!==(t=null===(e=i.toJSON)||void 0===e?void 0:e.call(i))&&void 0!==t?t:o.getObservableProps(i))}return s}static getObservableProps(t){const s=e.toJS(t);return Object.entries(s).reduce(((s,[o,r])=>({...s,...e.isObservableProp(t,o)?{[o]:r}:{}})),{})}static persistStore(e,r){return o.persistedStores.has(r)?(console.error(`Duplicate serializable store key: ${r}`),e):(o.persistedStores.add(r),e.libStoreId=r,"wakeup"in e.prototype||(e.prototype.wakeup=s),"addOnChangeListener"in e.prototype||(e.prototype.addOnChangeListener=t),e)}}Object.defineProperty(o,"persistedStores",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),module.exports=o;
1
+ "use strict";var e=require("mobx"),t=require("./on-change-listener.js"),s=require("./wakeup.js");class r{constructor({initState:e,storesParams:t,storage:s,options:o}={}){Object.defineProperty(this,"stores",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"storesRelations",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"initState",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"storage",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"persistData",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"storesParams",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"options",{enumerable:!0,configurable:!0,writable:!0,value:{shouldDisablePersist:!1,shouldRemoveInitState:!0,isSSR:!1}}),this.initState=e||{},this.storesParams=t||{},this.storage=s,Object.assign(this.options,o||{}),r.instance=this}async init(){return this.storage&&(this.persistData=await this.storage.get()||{}),this}static get(){if(!r.instance)throw new Error("Store manager is not initialized.");return r.instance}getStores(){return this.stores}getStoresRelations(){return this.storesRelations}static getPersistedStoresIds(){return r.persistedStores}getStoreId(e,t={}){const{id:s,contextId:r,key:o}=t;if(s)return s;if(e.libStoreId)return e.libStoreId;let i=e.name||e.constructor.name;return e.isSingleton?i:(i=`${i}--${r}`,o?`${i}--${o}`:i)}createContextId(e){return`ctx${e||this.storesRelations.size+1}`}getStore(e,t={}){const s=this.getStoreId(e,t);return this.stores.has(s)?this.stores.get(s):e.isSingleton?this.createStore(e,{id:s,contextId:"singleton",parentId:"root",componentName:"root-app",componentProps:{}}):this.lookupStore(s,t)}lookupStore(e,t){var s,r;const{contextId:o,parentId:i}=t,n=null===(s=e.split("--"))||void 0===s?void 0:s[0],{ids:a,parentId:l}=null!==(r=this.storesRelations.get(o))&&void 0!==r?r:{ids:new Set,parentId:i},d=[...a].filter((e=>e.startsWith(`${n}--`)));if(1===d.length)return this.stores.get(d[0]);if(d.length>1)console.error("Parent context has multiple stores with the same id, please pass key to getStore function.");else if(l&&"root"!==l)return this.lookupStore(e,{contextId:l})}createStore(e,t){var s,o;const{isSSR:i}=this.options,{id:n,contextId:a,parentId:l,componentName:d,componentProps:c}=t;if((e.isSingleton||i)&&this.stores.has(n))return this.stores.get(n);const u=new e({...this.storesParams,storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),componentProps:c});u.libStoreId=n,u.isSingleton=e.isSingleton,u.libStoreContextId=e.isSingleton?"singleton":a,u.libStoreParentId=e.isSingleton||!l||l===a?"root":l,u.libStoreComponentName=d;const p=this.initState[n],h=this.persistData[n];return p&&Object.assign(u,p),"wakeup"in u&&r.persistedStores.has(n)&&(null===(s=u.wakeup)||void 0===s||s.call(u,u,{initState:p,persistedState:h})),null===(o=u.init)||void 0===o||o.call(u),this.prepareStore(u),(u.isSingleton||i)&&this.prepareMount(u),u}createStores(e,t,s,r,o={}){return e.reduce(((e,[i,n])=>{const[a,l]="store"in n?[n.store,n.id]:[n,this.getStoreId(n,{key:i,contextId:s})];return{...e,[i]:this.createStore(a,{id:l,contextId:s,parentId:t,componentName:r,componentProps:o})}}),{})}prepareStore(e){const t=e.libStoreId,s=e.libStoreContextId;this.storesRelations.has(s)||this.storesRelations.set(s,{ids:new Set,parentId:e.libStoreParentId&&e.libStoreParentId!==s?e.libStoreParentId:"root",componentName:e.libStoreComponentName});const{ids:r}=this.storesRelations.get(s);this.stores.has(t)||(this.stores.set(t,e),r.add(t))}prepareMount(e){const{shouldRemoveInitState:t}=this.options,s=e.libStoreId,o=[];return r.persistedStores.has(s)&&"addOnChangeListener"in e&&o.push(e.addOnChangeListener(e,this)),t&&this.initState[s]&&delete this.initState[s],o}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var s;if(t.push(...this.prepareMount(e)),"onMount"in e){const r=null===(s=e.onMount)||void 0===s?void 0:s.call(e);"function"==typeof r&&t.push(r)}"onDestroy"in e&&t.push((()=>{var t;return null===(t=e.onDestroy)||void 0===t?void 0:t.call(e)}))})),()=>{t.forEach((e=>e())),Object.values(e).forEach((e=>{var t;const s=e.libStoreId;if(!e.isSingleton){const{ids:r}=null!==(t=this.storesRelations.get(e.libStoreContextId))&&void 0!==t?t:{ids:new Set};this.stores.delete(s),r.delete(s),r.size||this.storesRelations.delete(e.libStoreContextId)}}))}}toJSON(){var e,t;const s={};for(const[o,i]of this.stores.entries())s[o]=null!==(t=null===(e=i.toJSON)||void 0===e?void 0:e.call(i))&&void 0!==t?t:r.getObservableProps(i);return s}toPersistedJSON(){var e,t;const s={};for(const o of r.persistedStores){const i=this.stores.get(o);i&&(s[o]=null!==(t=null===(e=i.toJSON)||void 0===e?void 0:e.call(i))&&void 0!==t?t:r.getObservableProps(i))}return s}static getObservableProps(t){const s=e.toJS(t);return Object.entries(s).reduce(((s,[r,o])=>({...s,...e.isObservableProp(t,r)?{[r]:o}:{}})),{})}static persistStore(e,o){return r.persistedStores.has(o)?(console.error(`Duplicate serializable store key: ${o}`),e):(r.persistedStores.add(o),e.libStoreId=o,"wakeup"in e.prototype||(e.prototype.wakeup=s),"addOnChangeListener"in e.prototype||(e.prototype.addOnChangeListener=t),e)}}Object.defineProperty(r,"persistedStores",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),module.exports=r;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/react-mobx-manager",
3
- "version": "1.4.1-beta.1",
3
+ "version": "1.4.2-beta.1",
4
4
  "description": "This package provides Mobx stores manager for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",