@lomray/react-mobx-manager 1.3.1-beta.2 → 1.4.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/README.md CHANGED
@@ -422,9 +422,12 @@ class MyStore {
422
422
  * storeManager - access to store manager
423
423
  * apiClient - your custom param, see 'storesParams' in Manager
424
424
  */
425
- constructor({ getStore, storeManager, apiClient }: IConstructorParams) {
425
+ constructor({ getStore, storeManager, apiClient, componentProps }: IConstructorParams) {
426
426
  this.apiClient = apiClient;
427
427
  this.someParentStore = getStore(SomeParentStore);
428
+
429
+ // In case when store is't singletone you can get access to component props
430
+ console.log(componentProps);
428
431
 
429
432
  makeObservable(this, {
430
433
  state: observable,
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./types-4e5aab57";
1
+ export * from "./types-c6a5e53a";
2
2
  export * from "./context";
3
3
  export { default as Manager } from "./manager";
4
4
  export { default as onChangeListener } from "./on-change-listener";
package/lib/manager.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-4e5aab57";
1
+ import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-c6a5e53a";
2
2
  /**
3
3
  * Mobx stores manager
4
4
  */
@@ -137,7 +137,7 @@ declare class Manager {
137
137
  /**
138
138
  * Create stores for component
139
139
  */
140
- createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, componentName: string): TStores;
140
+ createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, componentName: string, componentProps?: Record<string, any>): TStores;
141
141
  /**
142
142
  * Prepare store before usage
143
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: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:""}):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 u=new e({storeManager:this,getStore:(e,t={contextId:a,parentId:l})=>this.getStore(e,t),...this.storesParams});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 c=this.initState[n],h=this.persistData[n];return c&&Object.assign(u,c),"wakeup"in u&&r.persistedStores.has(n)&&(null===(s=u.wakeup)||void 0===s||s.call(u,u,{initState:c,persistedState:h})),null===(o=u.init)||void 0===o||o.call(u),(u.isSingleton||i)&&this.prepareStore(u),u}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.libStoreId,o=e.libStoreContextId,i=[];this.storesRelations.has(o)||this.storesRelations.set(o,{ids:new Set,parentId:e.libStoreParentId&&e.libStoreParentId!==o?e.libStoreParentId:"root",componentName:e.libStoreComponentName});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.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;
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),(u.isSingleton||i)&&this.prepareStore(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{shouldRemoveInitState:t}=this.options,s=e.libStoreId,o=e.libStoreContextId,i=[];this.storesRelations.has(o)||this.storesRelations.set(o,{ids:new Set,parentId:e.libStoreParentId&&e.libStoreParentId!==o?e.libStoreParentId:"root",componentName:e.libStoreComponentName});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.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;
@@ -1,4 +1,4 @@
1
- import { IStorePersisted } from "./types-4e5aab57";
1
+ import { IStorePersisted } from "./types-c6a5e53a";
2
2
  /**
3
3
  * Listen persist store changes
4
4
  */
@@ -1,4 +1,4 @@
1
- import { IStorage } from "../types-4e5aab57";
1
+ import { IStorage } from "../types-c6a5e53a";
2
2
  interface IAsyncStorage {
3
3
  getItem: (key: string) => Promise<string | null>;
4
4
  setItem: (key: string, value: string) => Promise<void>;
@@ -1,4 +1,4 @@
1
- import { IStorage } from "../types-4e5aab57";
1
+ import { IStorage } from "../types-c6a5e53a";
2
2
  /**
3
3
  * Local storage for mobx store manager
4
4
  */
@@ -1,7 +1,8 @@
1
1
  import Manager from "./manager";
2
- interface IConstructorParams {
2
+ interface IConstructorParams<TProps = Record<string, any>> {
3
3
  storeManager: Manager;
4
4
  getStore: <T>(store: IConstructableStore<T>, params?: Partial<IStoreParams>) => T | undefined;
5
+ componentProps: TProps;
5
6
  }
6
7
  interface IStoreLifecycle {
7
8
  onDestroy?: () => void;
@@ -33,7 +34,7 @@ type TStoreDefinition<TSto extends IStore | IStorePersisted = any> = IConstructa
33
34
  } & IStoreConfig);
34
35
  type TMapStores = Record<string, TStoreDefinition>;
35
36
  interface IManagerParams {
36
- storesParams?: Omit<IConstructorParams, 'storeManager' | 'getStore'>;
37
+ storesParams?: Omit<IConstructorParams, 'storeManager' | 'getStore' | 'componentProps'>;
37
38
  storage?: IStorage;
38
39
  options?: IManagerOptions;
39
40
  initState?: Record<string, any>;
@@ -73,5 +74,6 @@ interface IStoreParams {
73
74
  contextId?: string;
74
75
  parentId?: string;
75
76
  componentName?: string;
77
+ componentProps?: Record<string, any>;
76
78
  }
77
79
  export { IConstructorParams, IStoreLifecycle, IStore, IStorePersisted, TInitStore, IConstructableStore, IStoreConfig, TStoreDefinition, TMapStores, IManagerParams, TWakeup, IStorage, IManagerOptions, TStores, ClassReturnType, StoresType, IStoreParams };
package/lib/wakeup.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IStorePersisted } from "./types-4e5aab57";
1
+ import { IStorePersisted } from "./types-c6a5e53a";
2
2
  /**
3
3
  * Restore store state from initial state
4
4
  */
@@ -1,5 +1,5 @@
1
1
  import { FC } from 'react';
2
- import { TMapStores } from "./types-4e5aab57";
2
+ import { TMapStores } from "./types-c6a5e53a";
3
3
  /**
4
4
  * Make component observable and pass stores as props
5
5
  */
@@ -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 i=t.observer(e),c=s||e.libStoreContextId,d=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(c||l);return{contextId:e,initStores:o.createStores(Object.entries(n),s,e,d)}}));return r.useEffect((()=>o.mountStores(x)),[x,o]),u.default.createElement(a.StoreManagerParentProvider,{parentId:f},u.default.createElement(i,{...x,...e}))};return o.default(l,e),l.displayName=`Mobx(${d})`,l};
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 i=t.observer(e),c=s||e.libStoreContextId,d=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 t=o.createContextId(c||l);return{contextId:t,initStores:o.createStores(Object.entries(n),s,t,d,e)}}));return r.useEffect((()=>o.mountStores(x)),[x,o]),u.default.createElement(a.StoreManagerParentProvider,{parentId:f},u.default.createElement(i,{...x,...e}))};return o.default(l,e),l.displayName=`Mobx(${d})`,l};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/react-mobx-manager",
3
- "version": "1.3.1-beta.2",
3
+ "version": "1.4.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",