@lomray/react-mobx-manager 2.0.0-beta.7 → 2.0.0-beta.8

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/context.d.ts CHANGED
@@ -3,6 +3,7 @@ import { IConsistentSuspense } from '@lomray/consistent-suspense';
3
3
  import React from 'react';
4
4
  import { FC, ReactElement } from "react";
5
5
  import Manager from "./manager.js";
6
+ import { TStores } from "./types-0ce7ddf0.js";
6
7
  interface IStoreManagerProvider {
7
8
  storeManager: Manager;
8
9
  shouldInit?: boolean;
@@ -11,8 +12,9 @@ interface IStoreManagerProvider {
11
12
  suspenseProvider?: Partial<IConsistentSuspense>;
12
13
  }
13
14
  interface IStoreManagerParentProvider {
14
- children?: React.ReactNode;
15
15
  parentId: string;
16
+ children?: React.ReactNode;
17
+ initStores?: TStores;
16
18
  }
17
19
  /**
18
20
  * Mobx store manager context
@@ -32,6 +34,6 @@ declare const StoreManagerParentProvider: FC<Omit<IStoreManagerParentProvider, '
32
34
  * @constructor
33
35
  */
34
36
  declare const StoreManagerProvider: FC<IStoreManagerProvider>;
35
- declare const useStoreManagerContext: () => Manager;
36
- declare const useStoreManagerParentContext: () => IStoreManagerParentProvider['parentId'];
37
- export { StoreManagerContext, StoreManagerParentContext, StoreManagerProvider, StoreManagerParentProvider, useStoreManagerContext, useStoreManagerParentContext };
37
+ declare const useStoreManager: () => Manager;
38
+ declare const useStoreManagerParent: () => IStoreManagerParentProvider['parentId'];
39
+ export { StoreManagerContext, StoreManagerParentContext, StoreManagerProvider, StoreManagerParentProvider, useStoreManager, useStoreManagerParent };
package/lib/context.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@lomray/consistent-suspense"),t=require("react");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=r(t);const n=a.default.createContext({}),o=a.default.createContext("root"),s=({children:e,parentId:t})=>a.default.createElement(o.Provider,{value:t,children:e});exports.StoreManagerContext=n,exports.StoreManagerParentContext=o,exports.StoreManagerParentProvider=s,exports.StoreManagerProvider=({children:r,storeManager:o,fallback:u,suspenseProvider:l={},shouldInit:i=!1})=>{const[d,c]=t.useState(!i);return t.useEffect((()=>{i&&o.init().then((()=>c(!0))).catch((e=>{console.error("Failed initialized store manager: ",e)}))}),[i,o]),a.default.createElement(e.ConsistentSuspenseProvider,{...l},a.default.createElement(n.Provider,{value:o},a.default.createElement(s,{parentId:"root"},d?r:u||r)))},exports.useStoreManagerContext=()=>t.useContext(n),exports.useStoreManagerParentContext=()=>t.useContext(o);
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@lomray/consistent-suspense"),t=require("react");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=r(t);const o=n.default.createContext({}),a=n.default.createContext("root"),s=({parentId:e,children:t,initStores:r})=>{const o=u();return r&&o.touchedStores(r),n.default.createElement(a.Provider,{value:e,children:t})},u=()=>t.useContext(o);exports.StoreManagerContext=o,exports.StoreManagerParentContext=a,exports.StoreManagerParentProvider=s,exports.StoreManagerProvider=({children:r,storeManager:a,fallback:u,suspenseProvider:i={},shouldInit:l=!1})=>{const[d,c]=t.useState(!l);return t.useEffect((()=>{l&&a.init().then((()=>c(!0))).catch((e=>{console.error("Failed initialized store manager: ",e)}))}),[l,a]),n.default.createElement(e.ConsistentSuspenseProvider,{...i},n.default.createElement(o.Provider,{value:a},n.default.createElement(s,{parentId:"root"},d?r:u||r)))},exports.useStoreManager=u,exports.useStoreManagerParent=()=>t.useContext(a);
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./types-e0655a7e.js";
1
+ export * from "./types-0ce7ddf0.js";
2
2
  export * from "./context.js";
3
3
  export { default as Manager } from "./manager.js";
4
4
  export { default as onChangeListener } from "./on-change-listener.js";
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./context.js"),r=require("./manager.js"),t=require("./on-change-listener.js"),o=require("./wakeup.js"),a=require("./with-stores.js"),n=require("./events.js");exports.StoreManagerContext=e.StoreManagerContext,exports.StoreManagerParentContext=e.StoreManagerParentContext,exports.StoreManagerParentProvider=e.StoreManagerParentProvider,exports.StoreManagerProvider=e.StoreManagerProvider,exports.useStoreManagerContext=e.useStoreManagerContext,exports.useStoreManagerParentContext=e.useStoreManagerParentContext,exports.Manager=r,exports.onChangeListener=t,exports.wakeup=o,exports.withStores=a,exports.Events=n;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./context.js"),r=require("./manager.js"),t=require("./on-change-listener.js"),a=require("./wakeup.js"),o=require("./with-stores.js"),n=require("./events.js");exports.StoreManagerContext=e.StoreManagerContext,exports.StoreManagerParentContext=e.StoreManagerParentContext,exports.StoreManagerParentProvider=e.StoreManagerParentProvider,exports.StoreManagerProvider=e.StoreManagerProvider,exports.useStoreManager=e.useStoreManager,exports.useStoreManagerParent=e.useStoreManagerParent,exports.Manager=r,exports.onChangeListener=t,exports.wakeup=a,exports.withStores=o,exports.Events=n;
package/lib/manager.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-e0655a7e.js";
1
+ import StoreStatus from "./store-status.js";
2
+ import { IConstructableStore, IManagerOptions, IManagerParams, IStorage, IStore, IStoreParams, TInitStore, TStoreDefinition, TStores } from "./types-0ce7ddf0.js";
2
3
  /**
3
4
  * Mobx stores manager
4
5
  */
@@ -116,13 +117,6 @@ declare class Manager {
116
117
  * @protected
117
118
  */
118
119
  protected getStoreId<T>(store: IConstructableStore<T> | TInitStore, params?: IStoreParams): string;
119
- /**
120
- * Generate new context id
121
- */
122
- /**
123
- * Generate new context id
124
- */
125
- createContextId(id?: string): string;
126
120
  /**
127
121
  * Get exist store
128
122
  */
@@ -157,22 +151,18 @@ declare class Manager {
157
151
  createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, suspenseId: string, componentName: string, componentProps?: Record<string, any>): TStores;
158
152
  /**
159
153
  * Prepare store before usage
160
- * @protected
161
154
  */
162
155
  /**
163
156
  * Prepare store before usage
164
- * @protected
165
157
  */
166
158
  protected prepareStore(store: TStores[string]): void;
167
159
  /**
168
- * Prepare store before mount to component
169
- * @protected
160
+ * Remove store
170
161
  */
171
162
  /**
172
- * Prepare store before mount to component
173
- * @protected
163
+ * Remove store
174
164
  */
175
- protected prepareMount(store: TStores[string]): Required<IStoreLifecycle>['onDestroy'][];
165
+ protected removeStore(store: TStores[string]): void;
176
166
  /**
177
167
  * Mount stores to component
178
168
  */
@@ -180,6 +170,20 @@ declare class Manager {
180
170
  * Mount stores to component
181
171
  */
182
172
  mountStores(stores: TStores): () => void;
173
+ /**
174
+ * Change the stores status to touched
175
+ */
176
+ /**
177
+ * Change the stores status to touched
178
+ */
179
+ touchedStores(stores: TStores): void;
180
+ /**
181
+ * Change store status
182
+ */
183
+ /**
184
+ * Change store status
185
+ */
186
+ protected setStoreStatus(store: TStores[string], status: StoreStatus): void;
183
187
  /**
184
188
  * Get store's state
185
189
  */
package/lib/manager.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("@lomray/event-manager"),t=require("mobx"),s=require("./events.js"),r=require("./on-change-listener.js"),o=require("./wakeup.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=i(e);class a{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,"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}}),Object.defineProperty(this,"suspenseRelations",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"pushInitState",{enumerable:!0,configurable:!0,writable:!0,value:(e={})=>{for(const[t,s]of Object.entries(e))this.initState[t]=s}}),this.initState=e||{},this.storesParams=t||{},this.storage=s,Object.assign(this.options,r||{}),a.instance=this,"undefined"==typeof window||window.mobxManager||(window.mobxManager={pushInit:this.pushInitState})}async init(){return this.storage&&(this.persistData=await this.storage.get()||{}),this}static get(){if(!a.instance)throw new Error("Store manager is not initialized.");return a.instance}getStores(){return this.stores}getStoresRelations(){return this.storesRelations}getSuspenseRelations(){return this.suspenseRelations}static getPersistedStoresIds(){return a.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",suspenseId:"",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},u=[...a].filter((e=>e.startsWith(`${n}--`)));if(1===u.length)return this.stores.get(u[0]);if(u.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 r,o;const{isSSR:i}=this.options,{id:l,contextId:u,parentId:d,suspenseId:p,componentName:c,componentProps:h}=t;if((e.isSingleton||i)&&this.stores.has(l))return this.stores.get(l);const S=new e({...this.storesParams,storeManager:this,getStore:(e,t={contextId:u,parentId:d})=>this.getStore(e,t),componentProps:h});S.libStoreId=l,S.isSingleton=e.isSingleton,S.libStoreContextId=e.isSingleton?"singleton":u,S.libStoreParentId=e.isSingleton||!d||d===u?"root":d,S.libStoreSuspenseId=p,S.libStoreComponentName=c;const b=this.initState[l],g=this.persistData[l];return b&&Object.assign(S,b),n.default.publish(s.CREATE_STORE,{store:e}),"wakeup"in S&&a.persistedStores.has(l)&&(null===(r=S.wakeup)||void 0===r||r.call(S,S,{initState:b,persistedState:g})),null===(o=S.init)||void 0===o||o.call(S),this.prepareStore(S),(S.isSingleton||i)&&this.prepareMount(S),S}createStores(e,t,s,r,o,i={}){return e.reduce(((e,[n,a])=>{const[l,u]="store"in a?[a.store,a.id]:[a,this.getStoreId(a,{key:n,contextId:s})];return{...e,[n]:this.createStore(l,{id:u,contextId:s,parentId:t,suspenseId:r,componentName:o,componentProps:i})}}),{})}prepareStore(e){const t=e.libStoreId,r=e.libStoreContextId,o=e.libStoreSuspenseId;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:i}=this.storesRelations.get(r);this.stores.has(t)||(this.stores.set(t,e),i.add(t),n.default.publish(s.ADD_STORE,{store:e})),this.suspenseRelations.has(o)||this.suspenseRelations.set(o,new Set),this.suspenseRelations.get(o).add(t)}prepareMount(e){const{shouldRemoveInitState:t}=this.options,s=e.libStoreId,r=[];return a.persistedStores.has(s)&&"addOnChangeListener"in e&&r.push(e.addOnChangeListener(e,this)),t&&this.initState[s]&&delete this.initState[s],r}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var r;if(this.prepareStore(e),t.push(...this.prepareMount(e)),n.default.publish(s.MOUNT_STORE,{store:e}),"onMount"in e){const s=null===(r=e.onMount)||void 0===r?void 0:r.call(e);"function"==typeof s&&t.push(s)}"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 r=e.libStoreId;if(n.default.publish(s.UNMOUNT_STORE,{store:e}),!e.isSingleton){const{ids:o}=null!==(t=this.storesRelations.get(e.libStoreContextId))&&void 0!==t?t:{ids:new Set};this.stores.delete(r),o.delete(r),n.default.publish(s.DELETE_STORE,{store:e}),o.size||this.storesRelations.delete(e.libStoreContextId)}}))}}toJSON(e){var t,s,r;const o={},i=null!==(t=null==e?void 0:e.reduce(((e,t)=>(this.stores.has(t)&&e.set(t,this.stores.get(t)),e)),new Map))&&void 0!==t?t:this.stores;for(const[e,t]of i.entries())o[e]=null!==(r=null===(s=t.toJSON)||void 0===s?void 0:s.call(t))&&void 0!==r?r:a.getObservableProps(t);return o}toPersistedJSON(){var e,t;const s={};for(const r of a.persistedStores){const o=this.stores.get(r);o&&(s[r]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:a.getObservableProps(o))}return s}static getObservableProps(e){const s=t.toJS(e);return Object.entries(s).reduce(((s,[r,o])=>({...s,...t.isObservableProp(e,r)?{[r]:o}:{}})),{})}static persistStore(e,t){return a.persistedStores.has(t)?(console.warn(`Duplicate serializable store key: ${t}`),e):(a.persistedStores.add(t),e.libStoreId=t,"wakeup"in e.prototype||(e.prototype.wakeup=o),"addOnChangeListener"in e.prototype||(e.prototype.addOnChangeListener=r),e)}}Object.defineProperty(a,"persistedStores",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),module.exports=a;
1
+ "use strict";var e=require("@lomray/event-manager"),t=require("mobx"),s=require("./events.js"),o=require("./on-change-listener.js"),r=require("./store-status.js"),i=require("./wakeup.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=n(e);class l{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}}),Object.defineProperty(this,"suspenseRelations",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"pushInitState",{enumerable:!0,configurable:!0,writable:!0,value:(e={})=>{for(const[t,s]of Object.entries(e))this.initState[t]=s}}),this.initState=e||{},this.storesParams=t||{},this.storage=s,Object.assign(this.options,o||{}),l.instance=this,"undefined"==typeof window||window.mobxManager||(window.mobxManager={pushInit:this.pushInitState})}async init(){return this.storage&&(this.persistData=await this.storage.get()||{}),this}static get(){if(!l.instance)throw new Error("Store manager is not initialized.");return l.instance}getStores(){return this.stores}getStoresRelations(){return this.storesRelations}getSuspenseRelations(){return this.suspenseRelations}static getPersistedStoresIds(){return l.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)}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",suspenseId:"",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},u=[...a].filter((e=>e.startsWith(`${n}--`)));if(1===u.length)return this.stores.get(u[0]);if(u.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){const{id:o,contextId:i,parentId:n,suspenseId:l,componentName:u,componentProps:d}=t;if(this.stores.has(o))return this.stores.get(o);const h=new e({...this.storesParams,storeManager:this,getStore:(e,t={contextId:i,parentId:n})=>this.getStore(e,t),componentProps:d});return h.libStoreId=o,h.isSingleton=e.isSingleton,h.libStoreContextId=e.isSingleton?"singleton":i,h.libStoreParentId=e.isSingleton||!n||n===i?"root":n,h.libStoreSuspenseId=l,h.libStoreComponentName=u,this.setStoreStatus(h,e.isSingleton?r.inUse:r.init),this.prepareStore(h),a.default.publish(s.CREATE_STORE,{store:e}),h}createStores(e,t,s,o,r,i={}){return e.reduce(((e,[n,a])=>{const[l,u]="store"in a?[a.store,a.id]:[a,this.getStoreId(a,{key:n,contextId:s})];return{...e,[n]:this.createStore(l,{id:u,contextId:s,parentId:t,suspenseId:o,componentName:r,componentProps:i})}}),{})}prepareStore(e){var t,o,r;const i=e.libStoreId,n=e.libStoreContextId,u=e.libStoreSuspenseId,d=this.initState[i],h=this.persistData[i];if(this.stores.has(i))return;if(d&&Object.assign(e,d),"wakeup"in e&&l.persistedStores.has(i)&&(null===(t=e.wakeup)||void 0===t||t.call(e,{initState:d,persistedState:h})),l.persistedStores.has(i)&&"addOnChangeListener"in e){const t=null===(o=e.onDestroy)||void 0===o?void 0:o.bind(e),s=e.addOnChangeListener(e,this);e.onDestroy=()=>{null==s||s(),null==t||t()}}null===(r=e.init)||void 0===r||r.call(e),this.storesRelations.has(n)||this.storesRelations.set(n,{ids:new Set,parentId:e.libStoreParentId&&e.libStoreParentId!==n?e.libStoreParentId:"root",componentName:e.libStoreComponentName}),this.suspenseRelations.has(u)||this.suspenseRelations.set(u,new Set);const{ids:c}=this.storesRelations.get(n);this.stores.set(i,e),c.add(i),this.suspenseRelations.get(u).add(i),a.default.publish(s.ADD_STORE,{store:e})}removeStore(e){var t,o,r;const i=e.libStoreId,n=e.libStoreSuspenseId,{ids:l}=null!==(t=this.storesRelations.get(e.libStoreContextId))&&void 0!==t?t:{ids:new Set};this.stores.has(i)&&(this.stores.delete(i),l.delete(i),n&&(null===(o=this.suspenseRelations.get(n))||void 0===o?void 0:o.has(i))&&this.suspenseRelations.get(n).delete(i),l.size||this.storesRelations.delete(e.libStoreContextId),"onDestroy"in e&&(null===(r=e.onDestroy)||void 0===r||r.call(e)),a.default.publish(s.DELETE_STORE,{store:e}))}mountStores(e){const{shouldRemoveInitState:t}=this.options;return Object.values(e).forEach((e=>{const o=e.libStoreId;t&&this.initState[o]&&delete this.initState[o],this.setStoreStatus(e,r.inUse),a.default.publish(s.MOUNT_STORE,{store:e})})),()=>{Object.values(e).forEach((e=>{e.isSingleton||(this.setStoreStatus(e,r.unused),a.default.publish(s.UNMOUNT_STORE,{store:e}))}))}}touchedStores(e){Object.values(e).forEach((e=>{e.libStoreStatus!==r.init||e.isSingleton||this.setStoreStatus(e,r.touched)}))}setStoreStatus(e,t){const{destroyTimers:{init:s=500,touched:o=3e3,unused:i=1e3}={}}=this.options;e.libStoreStatus=t,clearTimeout(e.libDestroyTimer);let n=0;switch(t){case r.init:n=s;break;case r.touched:n=o;break;case r.unused:n=i}n&&(e.libDestroyTimer=setTimeout((()=>this.removeStore(e)),n))}toJSON(e){var t,s,o;const r={},i=null!==(t=null==e?void 0:e.reduce(((e,t)=>(this.stores.has(t)&&e.set(t,this.stores.get(t)),e)),new Map))&&void 0!==t?t:this.stores;for(const[e,t]of i.entries())r[e]=null!==(o=null===(s=t.toJSON)||void 0===s?void 0:s.call(t))&&void 0!==o?o:l.getObservableProps(t);return r}toPersistedJSON(){var e,t;const s={};for(const o of l.persistedStores){const r=this.stores.get(o);r&&(s[o]=null!==(t=null===(e=r.toJSON)||void 0===e?void 0:e.call(r))&&void 0!==t?t:l.getObservableProps(r))}return s}static getObservableProps(e){const s=t.toJS(e);return Object.entries(s).reduce(((s,[o,r])=>({...s,...t.isObservableProp(e,o)?{[o]:r}:{}})),{})}static persistStore(e,t){return l.persistedStores.has(t)?(console.warn(`Duplicate serializable store key: ${t}`),e):(l.persistedStores.add(t),e.libStoreId=t,"wakeup"in e.prototype||(e.prototype.wakeup=i.bind(e)),"addOnChangeListener"in e.prototype||(e.prototype.addOnChangeListener=o),e)}}Object.defineProperty(l,"persistedStores",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),module.exports=l;
@@ -1,4 +1,4 @@
1
- import { IStorePersisted } from "./types-e0655a7e.js";
1
+ import { IStorePersisted } from "./types-0ce7ddf0.js";
2
2
  /**
3
3
  * Listen persist store changes
4
4
  */
@@ -1,4 +1,4 @@
1
- import { IStorage } from "../types-e0655a7e.js";
1
+ import { IStorage } from "../types-0ce7ddf0.js";
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-e0655a7e.js";
1
+ import { IStorage } from "../types-0ce7ddf0.js";
2
2
  /**
3
3
  * Local storage for mobx store manager
4
4
  */
@@ -0,0 +1,7 @@
1
+ declare enum StoreStatus {
2
+ init = "init",
3
+ touched = "touched",
4
+ inUse = "in-use",
5
+ unused = "unused"
6
+ }
7
+ export { StoreStatus as default };
@@ -0,0 +1 @@
1
+ "use strict";var u;!function(u){u.init="init",u.touched="touched",u.inUse="in-use",u.unused="unused"}(u||(u={}));var e=u;module.exports=e;
@@ -1,4 +1,4 @@
1
- import { TInitStore } from "./types-e0655a7e.js";
1
+ import { TInitStore } from "./types-0ce7ddf0.js";
2
2
  interface IPromise<TReturn> extends Promise<TReturn> {
3
3
  status?: 'fulfilled' | 'pending' | 'rejected';
4
4
  value?: TReturn;
@@ -1,4 +1,6 @@
1
+ /// <reference types="node" />
1
2
  import Manager from "./manager.js";
3
+ import StoreStatus from "./store-status.js";
2
4
  interface IWindowManager {
3
5
  pushInit: (state: Record<string, any>) => void;
4
6
  }
@@ -14,7 +16,6 @@ interface IConstructorParams<TProps = Record<string, any>> {
14
16
  }
15
17
  interface IStoreLifecycle {
16
18
  onDestroy?: () => void;
17
- onMount?: () => void | (() => void);
18
19
  }
19
20
  interface IStore extends IStoreLifecycle {
20
21
  libStoreId?: string;
@@ -22,6 +23,8 @@ interface IStore extends IStoreLifecycle {
22
23
  libStoreParentId?: string;
23
24
  libStoreSuspenseId?: string;
24
25
  libStoreComponentName?: string;
26
+ libStoreStatus?: StoreStatus;
27
+ libDestroyTimer?: NodeJS.Timeout;
25
28
  isSingleton?: boolean;
26
29
  init?: () => void;
27
30
  toJSON?: () => Record<string, any>;
@@ -48,7 +51,7 @@ interface IManagerParams {
48
51
  options?: IManagerOptions;
49
52
  initState?: Record<string, any>;
50
53
  }
51
- type TWakeup = (store: IStore, state: {
54
+ type TWakeup = (state: {
52
55
  initState?: Record<string, any>;
53
56
  persistedState?: Record<string, any>;
54
57
  }) => void;
@@ -60,7 +63,11 @@ interface IStorage {
60
63
  interface IManagerOptions {
61
64
  shouldDisablePersist?: boolean;
62
65
  shouldRemoveInitState?: boolean;
63
- isSSR?: boolean;
66
+ destroyTimers?: {
67
+ init?: number;
68
+ touched?: number;
69
+ unused?: number;
70
+ };
64
71
  }
65
72
  type TStores = {
66
73
  [storeKey: string]: IStore | IStorePersisted;
package/lib/wakeup.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { IStorePersisted } from "./types-e0655a7e.js";
1
+ import { TStores, TWakeup } from "./types-0ce7ddf0.js";
2
2
  /**
3
- * Restore store state from initial state
3
+ * Restore persisted store state
4
4
  */
5
- declare const wakeup: IStorePersisted['wakeup'];
5
+ declare function wakeup(this: TStores[string], { persistedState }: Parameters<TWakeup>[0]): void;
6
6
  export { wakeup as default };
package/lib/wakeup.js CHANGED
@@ -1 +1 @@
1
- "use strict";module.exports=(e,{persistedState:s})=>{s&&Object.assign(e,s)};
1
+ "use strict";module.exports=function({persistedState:t}){t&&Object.assign(this,t)};
@@ -1,5 +1,5 @@
1
1
  import { FC } from 'react';
2
- import { TMapStores, IWithStoreOptions } from "./types-e0655a7e.js";
2
+ import { TMapStores, IWithStoreOptions } from "./types-0ce7ddf0.js";
3
3
  /**
4
4
  * Make component observable and pass stores as props
5
5
  */
@@ -1 +1 @@
1
- "use strict";var e=require("@lomray/consistent-suspense"),t=require("hoist-non-react-statics"),r=require("mobx-react-lite"),n=require("react"),s=require("./context.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(t),u=o(n);module.exports=(t,o,{customContextId:i}={})=>{const c=r.observer(t),d=i||t.libStoreContextId,l=t.displayName||t.name,x=t=>{const r=s.useStoreManagerContext(),a=s.useStoreManagerParentContext(),{suspenseId:i}=e.useConsistentSuspenseContext(),x=e.useId(),[{contextId:m,initStores:S}]=n.useState((()=>{const e=r.createContextId(d||x);return{contextId:e,initStores:r.createStores(Object.entries(o),a,e,i,l,t)}}));return n.useEffect((()=>r.mountStores(S)),[S,r]),u.default.createElement(s.StoreManagerParentProvider,{parentId:m},u.default.createElement(c,{...S,...t}))};return a.default(x,t),x.displayName=`Mobx(${l})`,x};
1
+ "use strict";var e=require("@lomray/consistent-suspense"),t=require("hoist-non-react-statics"),r=require("mobx-react-lite"),n=require("react"),s=require("./context.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(t),u=o(n);module.exports=(t,o,{customContextId:i}={})=>{const c=r.observer(t),d=i||t.libStoreContextId,l=t.displayName||t.name,m=t=>{const r=s.useStoreManager(),a=s.useStoreManagerParent(),{suspenseId:i}=e.useConsistentSuspense(),m=e.useId(),[{contextId:S,initStores:f,mountStores:p}]=n.useState((()=>{const e=d||m;return{contextId:e,initStores:r.createStores(Object.entries(o),a,e,i,l,t),mountStores:()=>r.mountStores(f)}}));return n.useEffect(p,[p]),u.default.createElement(s.StoreManagerParentProvider,{parentId:S,initStores:f},u.default.createElement(c,{...t,...f}))};return a.default(m,t),m.displayName=`Mobx(${l})`,Object.defineProperty(m,"name",{value:m.displayName,writable:!1,enumerable:!1})};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/react-mobx-manager",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.8",
4
4
  "description": "This package provides Mobx stores manager for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -51,12 +51,12 @@
51
51
  "prepare": "husky install"
52
52
  },
53
53
  "dependencies": {
54
- "@lomray/consistent-suspense": "^1.1.1",
54
+ "@lomray/consistent-suspense": "^1.2.0",
55
55
  "@lomray/event-manager": "^1.2.2"
56
56
  },
57
57
  "devDependencies": {
58
- "@commitlint/cli": "^17.6.3",
59
- "@commitlint/config-conventional": "^17.6.3",
58
+ "@commitlint/cli": "^17.6.6",
59
+ "@commitlint/config-conventional": "^17.6.6",
60
60
  "@lomray/eslint-config-react": "^3.0.0",
61
61
  "@lomray/prettier-config": "^1.2.0",
62
62
  "@rollup/plugin-terser": "^0.4.3",