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

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
@@ -1,4 +1,5 @@
1
1
  /// <reference types="react" />
2
+ import { IConsistentSuspense } from '@lomray/consistent-suspense';
2
3
  import React from 'react';
3
4
  import { FC, ReactElement } from "react";
4
5
  import Manager from "./manager.js";
@@ -7,15 +8,12 @@ interface IStoreManagerProvider {
7
8
  shouldInit?: boolean;
8
9
  fallback?: ReactElement;
9
10
  children?: React.ReactNode;
11
+ suspenseProvider?: Partial<IConsistentSuspense>;
10
12
  }
11
13
  interface IStoreManagerParentProvider {
12
14
  children?: React.ReactNode;
13
15
  parentId: string;
14
16
  }
15
- interface IStoreManagerSuspenseProvider {
16
- children?: React.ReactNode;
17
- id: string | null;
18
- }
19
17
  /**
20
18
  * Mobx store manager context
21
19
  */
@@ -24,15 +22,6 @@ declare const StoreManagerContext: React.Context<Manager>;
24
22
  * To spread relationships
25
23
  */
26
24
  declare const StoreManagerParentContext: React.Context<string>;
27
- /**
28
- * Generate id for suspended component stores
29
- */
30
- declare const StoreManagerSuspenseContext: React.Context<string | null>;
31
- /**
32
- * Mobx store manager parent provider
33
- * @constructor
34
- */
35
- declare const StoreManagerSuspenseProvider: FC<IStoreManagerSuspenseProvider>;
36
25
  /**
37
26
  * Mobx store manager parent provider
38
27
  * @constructor
@@ -45,5 +34,4 @@ declare const StoreManagerParentProvider: FC<Omit<IStoreManagerParentProvider, '
45
34
  declare const StoreManagerProvider: FC<IStoreManagerProvider>;
46
35
  declare const useStoreManagerContext: () => Manager;
47
36
  declare const useStoreManagerParentContext: () => IStoreManagerParentProvider['parentId'];
48
- declare const useStoreManagerSuspenseContext: () => IStoreManagerSuspenseProvider['id'];
49
- export { StoreManagerContext, StoreManagerParentContext, StoreManagerSuspenseContext, StoreManagerProvider, StoreManagerParentProvider, StoreManagerSuspenseProvider, useStoreManagerContext, useStoreManagerParentContext, useStoreManagerSuspenseContext };
37
+ export { StoreManagerContext, StoreManagerParentContext, StoreManagerProvider, StoreManagerParentProvider, useStoreManagerContext, useStoreManagerParentContext };
package/lib/context.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=t(e);const a=r.default.createContext({}),n=r.default.createContext("root"),o=r.default.createContext(null),s=({children:e,parentId:t})=>r.default.createElement(n.Provider,{value:t,children:e});exports.StoreManagerContext=a,exports.StoreManagerParentContext=n,exports.StoreManagerParentProvider=s,exports.StoreManagerProvider=({children:t,storeManager:n,fallback:o,shouldInit:u=!1})=>{const[l,d]=e.useState(!u);return e.useEffect((()=>{u&&n.init().then((()=>d(!0))).catch((e=>{console.error("Failed initialized store manager: ",e)}))}),[u,n]),r.default.createElement(a.Provider,{value:n},r.default.createElement(s,{parentId:"root"},l?t:o||t))},exports.StoreManagerSuspenseContext=o,exports.StoreManagerSuspenseProvider=({children:e,id:t})=>r.default.createElement(o.Provider,{value:t,children:e}),exports.useStoreManagerContext=()=>e.useContext(a),exports.useStoreManagerParentContext=()=>e.useContext(n),exports.useStoreManagerSuspenseContext=()=>e.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 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);
package/lib/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- export * from "./types-abd34741.js";
1
+ export * from "./types-e0655a7e.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";
5
5
  export { default as wakeup } from "./wakeup.js";
6
6
  export { default as withStores } from "./with-stores.js";
7
7
  export { default as Events } from "./events.js";
8
- export { default as MobxSuspense } from "./mobx-suspense.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"),n=require("./with-stores.js"),s=require("./events.js"),a=require("./mobx-suspense.js");exports.StoreManagerContext=e.StoreManagerContext,exports.StoreManagerParentContext=e.StoreManagerParentContext,exports.StoreManagerParentProvider=e.StoreManagerParentProvider,exports.StoreManagerProvider=e.StoreManagerProvider,exports.StoreManagerSuspenseContext=e.StoreManagerSuspenseContext,exports.StoreManagerSuspenseProvider=e.StoreManagerSuspenseProvider,exports.useStoreManagerContext=e.useStoreManagerContext,exports.useStoreManagerParentContext=e.useStoreManagerParentContext,exports.useStoreManagerSuspenseContext=e.useStoreManagerSuspenseContext,exports.Manager=r,exports.onChangeListener=t,exports.wakeup=o,exports.withStores=n,exports.Events=s,exports.MobxSuspense=a;
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;
package/lib/manager.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import StreamStores from "./server/stream-stores.js";
2
- import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-abd34741.js";
1
+ import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, IStoreLifecycle, TInitStore, IStoreParams } from "./types-e0655a7e.js";
3
2
  /**
4
3
  * Mobx stores manager
5
4
  */
@@ -50,10 +49,10 @@ declare class Manager {
50
49
  */
51
50
  readonly options: IManagerOptions;
52
51
  /**
53
- * Attached stream store service
54
- * @see StreamStores.create
52
+ * Suspense stores relations
53
+ * @see withStores
55
54
  */
56
- streamStores?: StreamStores;
55
+ protected suspenseRelations: Map<string, Set<string>>;
57
56
  /**
58
57
  * @constructor
59
58
  */
@@ -89,6 +88,13 @@ declare class Manager {
89
88
  * Get stores relations
90
89
  */
91
90
  getStoresRelations(): Manager['storesRelations'];
91
+ /**
92
+ * Get suspense relations with stores
93
+ */
94
+ /**
95
+ * Get suspense relations with stores
96
+ */
97
+ getSuspenseRelations(): Manager['suspenseRelations'];
92
98
  /**
93
99
  * Get persisted stores ids
94
100
  */
@@ -148,7 +154,7 @@ declare class Manager {
148
154
  /**
149
155
  * Create stores for component
150
156
  */
151
- createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, componentName: string, componentProps?: Record<string, any>): TStores;
157
+ createStores(map: [string, TStoreDefinition][], parentId: string, contextId: string, suspenseId: string, componentName: string, componentProps?: Record<string, any>): TStores;
152
158
  /**
153
159
  * Prepare store before usage
154
160
  * @protected
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,"streamStores",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),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}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",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 r,o;const{isSSR:i}=this.options,{id:l,contextId:d,parentId:u,componentName:p,componentProps:c}=t;if((e.isSingleton||i)&&this.stores.has(l))return this.stores.get(l);const h=new e({...this.storesParams,storeManager:this,getStore:(e,t={contextId:d,parentId:u})=>this.getStore(e,t),componentProps:c});h.libStoreId=l,h.isSingleton=e.isSingleton,h.libStoreContextId=e.isSingleton?"singleton":d,h.libStoreParentId=e.isSingleton||!u||u===d?"root":u,h.libStoreComponentName=p;const S=this.initState[l],b=this.persistData[l];return S&&Object.assign(h,S),n.default.publish(s.CREATE_STORE,{store:e}),"wakeup"in h&&a.persistedStores.has(l)&&(null===(r=h.wakeup)||void 0===r||r.call(h,h,{initState:S,persistedState:b})),null===(o=h.init)||void 0===o||o.call(h),this.prepareStore(h),(h.isSingleton||i)&&this.prepareMount(h),h}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,r=e.libStoreContextId;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:o}=this.storesRelations.get(r);this.stores.has(t)||(this.stores.set(t,e),o.add(t),n.default.publish(s.ADD_STORE,{store:e}))}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"),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,4 +1,4 @@
1
- import { IStorePersisted } from "./types-abd34741.js";
1
+ import { IStorePersisted } from "./types-e0655a7e.js";
2
2
  /**
3
3
  * Listen persist store changes
4
4
  */
@@ -1,4 +1,4 @@
1
- import { IStorage } from "../types-abd34741.js";
1
+ import { IStorage } from "../types-e0655a7e.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-abd34741.js";
1
+ import { IStorage } from "../types-e0655a7e.js";
2
2
  /**
3
3
  * Local storage for mobx store manager
4
4
  */
@@ -1,4 +1,4 @@
1
- import { TInitStore } from "./types-abd34741.js";
1
+ import { TInitStore } from "./types-e0655a7e.js";
2
2
  interface IPromise<TReturn> extends Promise<TReturn> {
3
3
  status?: 'fulfilled' | 'pending' | 'rejected';
4
4
  value?: TReturn;
@@ -20,6 +20,7 @@ interface IStore extends IStoreLifecycle {
20
20
  libStoreId?: string;
21
21
  libStoreContextId?: string;
22
22
  libStoreParentId?: string;
23
+ libStoreSuspenseId?: string;
23
24
  libStoreComponentName?: string;
24
25
  isSingleton?: boolean;
25
26
  init?: () => void;
@@ -81,6 +82,7 @@ interface IStoreParams {
81
82
  key?: string;
82
83
  contextId?: string;
83
84
  parentId?: string;
85
+ suspenseId?: string;
84
86
  componentName?: string;
85
87
  componentProps?: Record<string, any>;
86
88
  }
package/lib/wakeup.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IStorePersisted } from "./types-abd34741.js";
1
+ import { IStorePersisted } from "./types-e0655a7e.js";
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, IWithStoreOptions } from "./types-abd34741.js";
2
+ import { TMapStores, IWithStoreOptions } from "./types-e0655a7e.js";
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"),n=require("./context.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=a(e),u=a(r);module.exports=(e,a,{customContextId:s}={})=>{const d=t.observer(e),i=s||e.libStoreContextId,c=e.displayName||e.name,l=e=>{var t;const o=n.useStoreManagerContext(),s=n.useStoreManagerParentContext(),l=n.useStoreManagerSuspenseContext(),S=null===(t=u.default.useId)||void 0===t?void 0:t.call(u.default),[{contextId:f,initStores:x}]=r.useState((()=>{const t=o.createContextId(i||l||S);return{contextId:t,initStores:o.createStores(Object.entries(a),s,t,c,e)}}));return r.useEffect((()=>o.mountStores(x)),[x,o]),u.default.createElement(n.StoreManagerSuspenseProvider,{id:null},u.default.createElement(n.StoreManagerParentProvider,{parentId:f},u.default.createElement(d,{...x,...e})))};return o.default(l,e),l.displayName=`Mobx(${c})`,l};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/react-mobx-manager",
3
- "version": "2.0.0-beta.6",
3
+ "version": "2.0.0-beta.7",
4
4
  "description": "This package provides Mobx stores manager for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -12,8 +12,7 @@
12
12
  "*": {
13
13
  "./*": [
14
14
  "lib/*",
15
- "lib/storages/*",
16
- "lib/server/*"
15
+ "lib/storages/*"
17
16
  ]
18
17
  }
19
18
  },
@@ -52,6 +51,7 @@
52
51
  "prepare": "husky install"
53
52
  },
54
53
  "dependencies": {
54
+ "@lomray/consistent-suspense": "^1.1.1",
55
55
  "@lomray/event-manager": "^1.2.2"
56
56
  },
57
57
  "devDependencies": {
@@ -1,8 +0,0 @@
1
- import { FC, SuspenseProps } from 'react';
2
- /**
3
- * Wrapper around react suspense
4
- * Create unique id for each suspense (used for create/stream stores state)
5
- * @constructor
6
- */
7
- declare const MobxSuspense: FC<SuspenseProps>;
8
- export { MobxSuspense as default };
@@ -1 +0,0 @@
1
- "use strict";var e=require("react"),t=require("./context.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=a(e);module.exports=({children:a,fallback:l,...n})=>{var u;const d=e.useId(),c=e.Children.map(a,((e,a)=>r.default.createElement(t.StoreManagerSuspenseProvider,{id:`${d}${a}`},e))),i=r.default.createElement(r.default.Fragment,null,r.default.createElement("script",{"data-context-id":d,"data-count":null!==(u=null==c?void 0:c.length)&&void 0!==u?u:0}),l);return r.default.createElement(e.Suspense,{...n,children:c,fallback:i})};
@@ -1,71 +0,0 @@
1
- import Manager from "../manager.js";
2
- /**
3
- * Stream mobx manager stores
4
- * NOTE: use with renderToPipeableStream
5
- */
6
- declare class StreamStores {
7
- /**
8
- * Obtained suspensions from application shell
9
- * @protected
10
- */
11
- protected suspendIds: Map<string, {
12
- contextId: string;
13
- count: number;
14
- }>;
15
- /**
16
- * Mobx store manager instance
17
- * @protected
18
- */
19
- protected readonly manager: Manager;
20
- /**
21
- * @constructor
22
- * @protected
23
- */
24
- /**
25
- * @constructor
26
- * @protected
27
- */
28
- protected constructor(manager: Manager);
29
- /**
30
- * Create stream stores service
31
- */
32
- /**
33
- * Create stream stores service
34
- */
35
- static create(manager: Manager, isAttach?: boolean): StreamStores;
36
- /**
37
- * Listen react stream and return suspense stores state to client
38
- */
39
- /**
40
- * Listen react stream and return suspense stores state to client
41
- */
42
- getStreamState(html: string): string | undefined;
43
- /**
44
- * Parse suspensions and related stores context id from application shell
45
- * @protected
46
- */
47
- /**
48
- * Parse suspensions and related stores context id from application shell
49
- * @protected
50
- */
51
- protected obtainSuspensions(html: string): void;
52
- /**
53
- * Replace suspend id
54
- * @protected
55
- */
56
- /**
57
- * Replace suspend id
58
- * @protected
59
- */
60
- protected replaceSuspendIds(formId: string, toId: string): string | undefined;
61
- /**
62
- * Parse complete suspense chunk
63
- * @protected
64
- */
65
- /**
66
- * Parse complete suspense chunk
67
- * @protected
68
- */
69
- protected obtainCompleteSuspense(html: string): string | undefined;
70
- }
71
- export { StreamStores as default };
@@ -1 +0,0 @@
1
- "use strict";class t{constructor(t){Object.defineProperty(this,"suspendIds",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"manager",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.manager=t}static create(e,s=!0){const n=new t(e);return s&&(e.streamStores=n),n}getStreamState(t){return this.obtainSuspensions(t),this.obtainCompleteSuspense(t)}obtainSuspensions(t){if(this.suspendIds)return;const e=[...t.matchAll(/<template id="(?<templateId>[^"]+)".+?<script data-context-id="(?<contextId>[^"]+)".+?data-count="(?<count>[^"]+)">/g)];e.length&&(this.suspendIds=new Map,e.forEach((({groups:t})=>{const{templateId:e,contextId:s,count:n}=null!=t?t:{};e&&this.suspendIds.set(e,{contextId:s,count:Number(n)})})))}replaceSuspendIds(t,e){if(t&&this.suspendIds.has(t))return this.suspendIds.set(e,this.suspendIds.get(t)),this.suspendIds.delete(t),e}obtainCompleteSuspense(t){var e,s,n;if(!t.startsWith("<div hidden id="))return;const{from:r,to:i}=null!==(s=null===(e=t.match(/\$RC\("(?<from>[^"]+)","(?<to>[^"]+)"\)/))||void 0===e?void 0:e.groups)&&void 0!==s?s:{},o=this.replaceSuspendIds(r,i);if(!o)return;const{contextId:a,count:d}=null!==(n=this.suspendIds.get(o))&&void 0!==n?n:{};if(!a||!d)return;const u=this.manager.getStoresRelations(),c=this.manager.createContextId(a),p=[...Array(d)].reduce(((t,e,s)=>{const n=`${c}${s}`;return u.has(n)&&t.push(...u.get(n).ids),t}),[]);if(!p.length)return;return`<script>window.mobxManager.pushInit(${JSON.stringify(this.manager.toJSON(p))});<\/script>`}}module.exports=t;