@lomray/react-mobx-manager 1.2.0 → 1.3.0
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/index.d.ts +1 -1
- package/lib/manager.d.ts +3 -2
- package/lib/manager.js +1 -1
- package/lib/on-change-listener.d.ts +1 -1
- package/lib/storages/async-storage.d.ts +1 -1
- package/lib/storages/local-storage.d.ts +1 -1
- package/lib/{types-5b099751.d.ts → types-e1cf38b5.d.ts} +2 -0
- package/lib/wakeup.d.ts +1 -1
- package/lib/with-stores.d.ts +1 -1
- package/lib/with-stores.js +1 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/manager.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-
|
|
1
|
+
import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-e1cf38b5";
|
|
2
2
|
/**
|
|
3
3
|
* Mobx stores manager
|
|
4
4
|
*/
|
|
@@ -18,6 +18,7 @@ declare class Manager {
|
|
|
18
18
|
protected readonly storesRelations: Map<string, {
|
|
19
19
|
ids: Set<string>;
|
|
20
20
|
parentId: string | null;
|
|
21
|
+
componentName?: string | undefined;
|
|
21
22
|
}>;
|
|
22
23
|
/**
|
|
23
24
|
* Save persisted stores identities
|
|
@@ -136,7 +137,7 @@ declare class Manager {
|
|
|
136
137
|
/**
|
|
137
138
|
* Create stores for component
|
|
138
139
|
*/
|
|
139
|
-
createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string): TStores;
|
|
140
|
+
createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, componentName: string): TStores;
|
|
140
141
|
/**
|
|
141
142
|
* Prepare store before usage
|
|
142
143
|
* @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:
|
|
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.id)return e.id;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:""}):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}=t;if((e.isSingleton||i)&&this.stores.has(n))return this.stores.get(n);const c=new e({storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),...this.storesParams});c.id=n,c.isSingleton=e.isSingleton,c.contextId=e.isSingleton?"singleton":a,c.parentId=e.isSingleton||!l||l===a?"root":l,c.componentName=d;const u=this.initState[n],h=this.persistData[n];return u&&Object.assign(c,u),"wakeup"in c&&r.persistedStores.has(n)&&(null===(s=c.wakeup)||void 0===s||s.call(c,c,{initState:u,persistedState:h})),null===(o=c.init)||void 0===o||o.call(c),(c.isSingleton||i)&&this.prepareStore(c),c}createStores(e,t,s,r){return e.reduce(((e,[o,i])=>{const[n,a]="store"in i?[i.store,i.id]:[i,this.getStoreId(i,{key:o,contextId:s})];return{...e,[o]:this.createStore(n,{id:a,contextId:s,parentId:t,componentName:r})}}),{})}prepareStore(e){const{shouldRemoveInitState:t}=this.options,s=e.id,o=e.contextId,i=[];this.storesRelations.has(o)||this.storesRelations.set(o,{ids:new Set,parentId:e.parentId&&e.parentId!==o?e.parentId:"root",componentName:e.componentName});const{ids:n}=this.storesRelations.get(o);return r.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 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[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.id=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;
|
|
@@ -11,6 +11,7 @@ interface IStore extends IStoreLifecycle {
|
|
|
11
11
|
id?: string;
|
|
12
12
|
contextId?: string;
|
|
13
13
|
parentId?: string;
|
|
14
|
+
componentName?: string;
|
|
14
15
|
isSingleton?: boolean;
|
|
15
16
|
init?: () => void;
|
|
16
17
|
toJSON?: () => Record<string, any>;
|
|
@@ -71,5 +72,6 @@ interface IStoreParams {
|
|
|
71
72
|
key?: string;
|
|
72
73
|
contextId?: string;
|
|
73
74
|
parentId?: string;
|
|
75
|
+
componentName?: string;
|
|
74
76
|
}
|
|
75
77
|
export { IConstructorParams, IStoreLifecycle, IStore, IStorePersisted, TInitStore, IConstructableStore, IStoreConfig, TStoreDefinition, TMapStores, IManagerParams, TWakeup, IStorage, IManagerOptions, TStores, ClassReturnType, StoresType, IStoreParams };
|
package/lib/wakeup.d.ts
CHANGED
package/lib/with-stores.d.ts
CHANGED
package/lib/with-stores.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("hoist-non-react-statics"),t=require("mobx-react-lite"),r=require("react"),a=require("./context.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(e),u=n(r);module.exports=(e,n,s)=>{const c=t.observer(e),d=s||e.contextId,i=({...e})=>{var t;const o=a.useStoreManagerContext(),{parentId:s}=a.useStoreManagerParentContext(),
|
|
1
|
+
"use strict";var e=require("hoist-non-react-statics"),t=require("mobx-react-lite"),r=require("react"),a=require("./context.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(e),u=n(r);module.exports=(e,n,s)=>{const c=t.observer(e),d=s||e.contextId,i=e.displayName||e.name,l=({...e})=>{var t;const o=a.useStoreManagerContext(),{parentId:s}=a.useStoreManagerParentContext(),l=null===(t=u.default.useId)||void 0===t?void 0:t.call(u.default),[{contextId:f,initStores:x}]=r.useState((()=>{const e=o.createContextId(d||l);return{contextId:e,initStores:o.createStores(Object.entries(n),s,e,i)}}));return r.useEffect((()=>o.mountStores(x)),[x,o]),u.default.createElement(a.StoreManagerParentProvider,{parentId:f},u.default.createElement(c,{...x,...e}))};return o.default(l,e),l.displayName=`Mobx(${i})`,l};
|