@lomray/react-mobx-manager 1.1.0 → 1.2.0-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/SECURITY.md ADDED
@@ -0,0 +1,11 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | 1.1.x | :white_check_mark: |
8
+
9
+ ## Reporting a Vulnerability
10
+
11
+ Please report (suspected) security vulnerabilities to this repository issues.
package/lib/manager.d.ts CHANGED
@@ -68,6 +68,27 @@ declare class Manager {
68
68
  * Get manager instance
69
69
  */
70
70
  static get(): Manager;
71
+ /**
72
+ * Get all stores
73
+ */
74
+ /**
75
+ * Get all stores
76
+ */
77
+ getStores(): Manager['stores'];
78
+ /**
79
+ * Get stores relations
80
+ */
81
+ /**
82
+ * Get stores relations
83
+ */
84
+ getStoresRelations(): Manager['storesRelations'];
85
+ /**
86
+ * Get persisted stores ids
87
+ */
88
+ /**
89
+ * Get persisted stores ids
90
+ */
91
+ static getPersistedStoresIds(): Set<string>;
71
92
  /**
72
93
  * Get store identity
73
94
  * @protected
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 r{constructor({initState:e,storesParams:t,storage:s,options:i}={}){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,i||{}),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}getStoreId(e,t={}){const{id:s,contextId:r,key:i}=t;if(s)return s;if(e.id)return e.id;let o=e.name||e.constructor.name;return e.isSingleton?o:(o=`${o}--${r}`,i?`${o}--${i}`:o)}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"}):this.lookupStore(s,t)}lookupStore(e,t){var s,r;const{contextId:i,parentId:o}=t,n=null===(s=e.split("--"))||void 0===s?void 0:s[0],{ids:a,parentId:l}=null!==(r=this.storesRelations.get(i))&&void 0!==r?r:{ids:new Set,parentId:o},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,i;const{isSSR:o}=this.options,{id:n,contextId:a,parentId:l}=t;if((e.isSingleton||o)&&this.stores.has(n))return this.stores.get(n);const d=new e({storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),...this.storesParams});d.id=n,d.isSingleton=e.isSingleton,d.contextId=e.isSingleton?"singleton":a,d.parentId=e.isSingleton||!l||l===a?"root":l;const c=this.initState[n],u=this.persistData[n];return c&&Object.assign(d,c),"wakeup"in d&&r.persistedStores.has(n)&&(null===(s=d.wakeup)||void 0===s||s.call(d,d,{initState:c,persistedState:u})),null===(i=d.init)||void 0===i||i.call(d),(d.isSingleton||o)&&this.prepareStore(d),d}createStores(e,t,s){return e.reduce(((e,[r,i])=>{const[o,n]="store"in i?[i.store,i.id]:[i,this.getStoreId(i,{key:r,contextId:s})];return{...e,[r]:this.createStore(o,{id:n,contextId:s,parentId:t})}}),{})}prepareStore(e){const{shouldRemoveInitState:t}=this.options,s=e.id,i=e.contextId,o=[];this.storesRelations.has(i)||this.storesRelations.set(i,{ids:new Set,parentId:e.parentId&&e.parentId!==i?e.parentId:"root"});const{ids:n}=this.storesRelations.get(i);return r.persistedStores.has(s)&&"addOnChangeListener"in e&&o.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)),o}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var s;if(t.push(...this.prepareStore(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.id;if(!e.isSingleton){const{ids:r}=null!==(t=this.storesRelations.get(e.contextId))&&void 0!==t?t:{ids:new Set};this.stores.delete(s),r.delete(s),r.size||this.storesRelations.delete(e.contextId)}}))}}toJSON(){var e,t;const s={};for(const[i,o]of this.stores.entries())s[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:r.getObservableProps(o);return s}toPersistedJSON(){var e,t;const s={};for(const i of r.persistedStores){const o=this.stores.get(i);o&&(s[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:r.getObservableProps(o))}return s}static getObservableProps(t){const s=e.toJS(t);return Object.entries(s).reduce(((s,[r,i])=>({...s,...e.isObservableProp(t,r)?{[r]:i}:{}})),{})}static persistStore(e,i){return r.persistedStores.has(i)?(console.error(`Duplicate serializable store key: ${i}`),e):(r.persistedStores.add(i),e.id=i,"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;
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:i}={}){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,i||{}),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:i}=t;if(s)return s;if(e.id)return e.id;let o=e.name||e.constructor.name;return e.isSingleton?o:(o=`${o}--${r}`,i?`${o}--${i}`:o)}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"}):this.lookupStore(s,t)}lookupStore(e,t){var s,r;const{contextId:i,parentId:o}=t,n=null===(s=e.split("--"))||void 0===s?void 0:s[0],{ids:a,parentId:l}=null!==(r=this.storesRelations.get(i))&&void 0!==r?r:{ids:new Set,parentId:o},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,i;const{isSSR:o}=this.options,{id:n,contextId:a,parentId:l}=t;if((e.isSingleton||o)&&this.stores.has(n))return this.stores.get(n);const d=new e({storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),...this.storesParams});d.id=n,d.isSingleton=e.isSingleton,d.contextId=e.isSingleton?"singleton":a,d.parentId=e.isSingleton||!l||l===a?"root":l;const u=this.initState[n],c=this.persistData[n];return u&&Object.assign(d,u),"wakeup"in d&&r.persistedStores.has(n)&&(null===(s=d.wakeup)||void 0===s||s.call(d,d,{initState:u,persistedState:c})),null===(i=d.init)||void 0===i||i.call(d),(d.isSingleton||o)&&this.prepareStore(d),d}createStores(e,t,s){return e.reduce(((e,[r,i])=>{const[o,n]="store"in i?[i.store,i.id]:[i,this.getStoreId(i,{key:r,contextId:s})];return{...e,[r]:this.createStore(o,{id:n,contextId:s,parentId:t})}}),{})}prepareStore(e){const{shouldRemoveInitState:t}=this.options,s=e.id,i=e.contextId,o=[];this.storesRelations.has(i)||this.storesRelations.set(i,{ids:new Set,parentId:e.parentId&&e.parentId!==i?e.parentId:"root"});const{ids:n}=this.storesRelations.get(i);return r.persistedStores.has(s)&&"addOnChangeListener"in e&&o.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)),o}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var s;if(t.push(...this.prepareStore(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.id;if(!e.isSingleton){const{ids:r}=null!==(t=this.storesRelations.get(e.contextId))&&void 0!==t?t:{ids:new Set};this.stores.delete(s),r.delete(s),r.size||this.storesRelations.delete(e.contextId)}}))}}toJSON(){var e,t;const s={};for(const[i,o]of this.stores.entries())s[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:r.getObservableProps(o);return s}toPersistedJSON(){var e,t;const s={};for(const i of r.persistedStores){const o=this.stores.get(i);o&&(s[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:r.getObservableProps(o))}return s}static getObservableProps(t){const s=e.toJS(t);return Object.entries(s).reduce(((s,[r,i])=>({...s,...e.isObservableProp(t,r)?{[r]:i}:{}})),{})}static persistStore(e,i){return r.persistedStores.has(i)?(console.error(`Duplicate serializable store key: ${i}`),e):(r.persistedStores.add(i),e.id=i,"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.1.0",
3
+ "version": "1.2.0-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",