@lomray/react-mobx-manager 1.0.0-beta.5 → 1.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/README.md CHANGED
@@ -25,7 +25,7 @@
25
25
  The React-mobx-manager package is distributed using [npm](https://www.npmjs.com/), the node package manager.
26
26
 
27
27
  ```
28
- npm i --save-dev @lomray/react-mobx-manager
28
+ npm i --save @lomray/react-mobx-manager
29
29
  ```
30
30
 
31
31
  Import `Manager, StoreManagerProvider` from `@lomray/react-mobx-manager` into your index file after wrap `<App/>` with `<StoreManagerProvider/>`
@@ -54,9 +54,42 @@ Connect mobx store to manager and you're good to go!
54
54
 
55
55
  ```jsx
56
56
  import { withStores } from '@lomray/react-mobx-manager';
57
- import stores from './index.stores';
58
- import User from './index';
59
-
57
+ import { makeAutoObservable } from 'mobx';
58
+
59
+ /**
60
+ * Mobx user store
61
+ */
62
+ class UserStore {
63
+ name = 'Matthew'
64
+
65
+ constructor() {
66
+ makeAutoObservable()
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Define stores for component
72
+ */
73
+ const stores = {
74
+ userStore: UserStore
75
+ };
76
+
77
+ type TProps = StoresType <typeof stores>;
78
+
79
+ /**
80
+ * User component
81
+ * @returns {JSX.Element}
82
+ * @constructor
83
+ */
84
+ const User: FC<TProps> = ({userStore: {name}}) => {
85
+ return (
86
+ <div>{name}</div>
87
+ )
88
+ }
89
+
90
+ /**
91
+ * Connect stores to component
92
+ */
60
93
  export default withStores(User, stores);
61
94
  ```
62
95
 
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./types-f2f6b0e4";
1
+ export * from "./types-4732bbd2";
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, TInitStore } from "./types-f2f6b0e4";
1
+ import { IConstructableStore, IManagerParams, IStorage, IStore, TStoreDefinition, IManagerOptions, TStores, TInitStore } from "./types-4732bbd2";
2
2
  /**
3
3
  * Mobx stores manager
4
4
  */
@@ -11,6 +11,11 @@ declare class Manager {
11
11
  * Created stores
12
12
  */
13
13
  private readonly stores;
14
+ /**
15
+ * Counters for multiple stores
16
+ * @private
17
+ */
18
+ private readonly counterStores;
14
19
  /**
15
20
  * Save persisted stores identities
16
21
  * @private
@@ -57,11 +62,9 @@ declare class Manager {
57
62
  init(): Promise<Manager>;
58
63
  /**
59
64
  * Get manager instance
60
- * NOTE: Need call 'init' before call this method
61
65
  */
62
66
  /**
63
67
  * Get manager instance
64
- * NOTE: Need call 'init' before call this method
65
68
  */
66
69
  static get(): Manager;
67
70
  /**
@@ -72,14 +75,14 @@ declare class Manager {
72
75
  * Get store identity
73
76
  * @protected
74
77
  */
75
- protected static getStoreKey<T>(store: IConstructableStore<T> | TInitStore, id?: string): string;
78
+ protected static getStoreKey<T>(store: IConstructableStore<T> | TInitStore, id?: string, index?: number): string;
76
79
  /**
77
80
  * Get exist store
78
81
  */
79
82
  /**
80
83
  * Get exist store
81
84
  */
82
- getStore<T>(store: IConstructableStore<T>, id?: string): T | undefined;
85
+ getStore<T>(store: IConstructableStore<T>, id?: string, index?: number): T | undefined;
83
86
  /**
84
87
  * Create new store instance
85
88
  * @protected
package/lib/manager.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("mobx"),t=require("./on-change-listener.js"),r=require("./wakeup.js");class s{constructor({initState:e,storesParams:t,storage:r,options:i}={}){Object.defineProperty(this,"stores",{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}}),this.initState=e||{},this.storesParams=t||{},this.storage=r,Object.assign(this.options,i||{}),s.instance=this}async init(){return this.storage&&(this.persistData=await this.storage.get()||{}),this}static get(){if(!s.instance)throw new Error("Store manager is not initialized.");return s.instance}static getStoreKey(e,t){return t||e.id||e.name||e.constructor.name}getStore(e,t){const r=s.getStoreKey(e,t);return this.stores.has(r)?this.stores.get(r):e.isSingleton?this.createStore(e,t):void 0}createStore(e,t){var r,i,o;const n=s.getStoreKey(e,t),a=this.stores.get(n);if(a)return a;const l=new e({storeManager:this,...this.storesParams});l.id=n,l.isSingleton=e.isSingleton;const u=this.initState[n],c=this.persistData[n];return u&&(Object.assign(l,u),this.options.shouldRemoveInitState&&delete this.initState[n]),"wakeup"in l&&s.persistedStores.has(n)&&(null===(r=l.wakeup)||void 0===r||r.call(l,l,{initState:u,persistedState:c}),null===(i=l.addOnChangeListener)||void 0===i||i.call(l,l,this)),this.stores.set(n,l),null===(o=l.init)||void 0===o||o.call(l),l}createStores(e){return e.reduce(((e,[t,r])=>{const[s,i]="store"in r?[r.store,r.id]:[r];return{...e,[t]:this.createStore(s,i)}}),{})}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var r;if("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=>{const t=s.getStoreKey(e);e.isSingleton||this.stores.delete(t)}))}}toJSON(){var e,t;const r={};for(const[i,o]of this.stores.entries())r[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:s.getObservableProps(o);return r}toPersistedJSON(){var e,t;const r={};for(const i of s.persistedStores){const o=this.stores.get(i);o&&(r[i]=null!==(t=null===(e=o.toJSON)||void 0===e?void 0:e.call(o))&&void 0!==t?t:s.getObservableProps(o))}return r}static getObservableProps(t){const r=e.toJS(t);return Object.entries(r).reduce(((r,[s,i])=>({...r,...e.isObservableProp(t,s)?{[s]:i}:{}})),{})}static persistStore(e,i){const o=s.getStoreKey(e,i);if(s.persistedStores.has(o))throw new Error(`Duplicate serializable store key: ${o}`);return s.persistedStores.add(i),e.id=o,"wakeup"in e.prototype||(e.prototype.wakeup=r),"addOnChangeListener"in e.prototype||(e.prototype.addOnChangeListener=t),e}}Object.defineProperty(s,"persistedStores",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),module.exports=s;
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,"counterStores",{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}static getStoreKey(e,t,s){const r=t||e.id||e.name||e.constructor.name;return s?`${r}--index${s}`:r}getStore(e,t,s){const i=r.getStoreKey(e,t,s);return this.stores.has(i)?this.stores.get(i):e.isSingleton?this.createStore(e,t):void 0}createStore(e,t){var s,i,o;const n=r.getStoreKey(e,t);if((e.isSingleton||this.options.isSSR)&&this.stores.has(n))return this.stores.get(n);const a=this.counterStores.get(n)||0,l=r.getStoreKey(e,t,a>0?a+1:void 0),u=new e({storeManager:this,...this.storesParams});u.id=l,u.isSingleton=e.isSingleton;const c=this.initState[l],h=this.persistData[l];return c&&(Object.assign(u,c),this.options.shouldRemoveInitState&&delete this.initState[l]),"wakeup"in u&&r.persistedStores.has(l)&&(null===(s=u.wakeup)||void 0===s||s.call(u,u,{initState:c,persistedState:h}),null===(i=u.addOnChangeListener)||void 0===i||i.call(u,u,this)),this.stores.set(l,u),this.counterStores.set(n,a+1),null===(o=u.init)||void 0===o||o.call(u),u}createStores(e){return e.reduce(((e,[t,s])=>{const[r,i]="store"in s?[s.store,s.id]:[s];return{...e,[t]:this.createStore(r,i)}}),{})}mountStores(e){const t=[];return Object.values(e).forEach((e=>{var s;const i=r.getStoreKey(e);if(this.stores.has(i)||this.stores.set(i,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=r.getStoreKey(e);e.isSingleton||(this.stores.delete(s),this.counterStores.set(s.replace(/--index.+$/,""),(null!==(t=this.counterStores.get(s))&&void 0!==t?t:0)-1))}))}}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){if(r.persistedStores.has(i))throw new Error(`Duplicate serializable store key: ${i}`);return 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,4 +1,4 @@
1
- import { IStorePersisted } from "./types-f2f6b0e4";
1
+ import { IStorePersisted } from "./types-4732bbd2";
2
2
  /**
3
3
  * Listen persist store changes
4
4
  */
@@ -1,4 +1,4 @@
1
- import { IStorage } from "../types-f2f6b0e4";
1
+ import { IStorage } from "../types-4732bbd2";
2
2
  declare class LocalStorage implements IStorage {
3
3
  globalKey: string;
4
4
  /**
@@ -18,10 +18,15 @@ interface IStorePersisted extends IStore {
18
18
  }
19
19
  type TInitStore<TSto extends IStore | IStorePersisted = IStore> = TSto & (IStorePersisted | IStore);
20
20
  type IConstructableStore<TSto extends IStore = IStore> = (new (props: IConstructorParams) => TInitStore<TSto>) & Partial<IStorePersisted>;
21
- type TStoreDefinition<TSto extends IStore | IStorePersisted = any> = IConstructableStore<TSto> | {
22
- store: IConstructableStore<TSto>;
23
- id: string;
21
+ /**
22
+ * Store params
23
+ */
24
+ type IStoreConfig = {
25
+ id?: string;
24
26
  };
27
+ type TStoreDefinition<TSto extends IStore | IStorePersisted = any> = IConstructableStore<TSto> | ({
28
+ store: IConstructableStore<TSto>;
29
+ } & IStoreConfig);
25
30
  type TMapStores = Record<string, TStoreDefinition>;
26
31
  interface IManagerParams {
27
32
  storesParams?: Omit<IConstructorParams, 'storeManager'>;
@@ -41,6 +46,7 @@ interface IStorage {
41
46
  interface IManagerOptions {
42
47
  shouldDisablePersist?: boolean;
43
48
  shouldRemoveInitState?: boolean;
49
+ isSSR?: boolean;
44
50
  }
45
51
  type TStores = {
46
52
  [storeKey: string]: IStore | IStorePersisted;
@@ -48,11 +54,13 @@ type TStores = {
48
54
  /**
49
55
  * Convert class type to class constructor
50
56
  */
51
- type ClassReturnType<T> = T extends new (...args: any) => infer R ? R : never;
57
+ type ClassReturnType<T> = T extends new (...args: any) => infer R ? R : T extends {
58
+ store: any;
59
+ } ? ClassReturnType<T['store']> : never;
52
60
  /**
53
61
  * Stores map to type
54
62
  */
55
63
  type StoresType<TSt> = {
56
64
  [keys in keyof TSt]: ClassReturnType<TSt[keys]>;
57
65
  };
58
- export { IConstructorParams, IStoreLifecycle, IStore, IStorePersisted, TInitStore, IConstructableStore, TStoreDefinition, TMapStores, IManagerParams, TWakeup, IStorage, IManagerOptions, TStores, ClassReturnType, StoresType };
66
+ export { IConstructorParams, IStoreLifecycle, IStore, IStorePersisted, TInitStore, IConstructableStore, IStoreConfig, TStoreDefinition, TMapStores, IManagerParams, TWakeup, IStorage, IManagerOptions, TStores, ClassReturnType, StoresType };
package/lib/wakeup.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IStorePersisted } from "./types-f2f6b0e4";
1
+ import { IStorePersisted } from "./types-4732bbd2";
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-f2f6b0e4";
2
+ import { TMapStores } from "./types-4732bbd2";
3
3
  /**
4
4
  * Make component observable and pass stores as props
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/react-mobx-manager",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.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",
@@ -41,29 +41,29 @@
41
41
  "prepare": "husky install"
42
42
  },
43
43
  "devDependencies": {
44
- "@commitlint/cli": "^17.0.3",
45
- "@commitlint/config-conventional": "^17.0.3",
44
+ "@commitlint/cli": "^17.1.2",
45
+ "@commitlint/config-conventional": "^17.1.0",
46
46
  "@lomray/eslint-config-react": "^2.0.0",
47
47
  "@lomray/prettier-config": "^1.2.0",
48
48
  "@types/hoist-non-react-statics": "^3.3.1",
49
49
  "@types/react": "^18.0.15",
50
- "eslint": "^8.20.0",
50
+ "eslint": "8.21.0",
51
51
  "eslint-config-prettier": "^8.5.0",
52
- "eslint-plugin-jsx-a11y": "^6.6.0",
52
+ "eslint-plugin-jsx-a11y": "^6.6.1",
53
53
  "eslint-plugin-prettier": "^4.2.1",
54
54
  "husky": "^8.0.1",
55
55
  "lint-staged": "^13.0.3",
56
56
  "prettier": "^2.7.1",
57
- "rollup": "^2.77.0",
57
+ "rollup": "^2.79.0",
58
58
  "rollup-plugin-terser": "^7.0.2",
59
59
  "rollup-plugin-ts": "^3.0.2",
60
- "semantic-release": "^19.0.3",
60
+ "semantic-release": "^19.0.5",
61
61
  "ttypescript": "^1.5.13",
62
62
  "typescript": "^4.7.4"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "hoist-non-react-statics": "^3.3.2",
66
- "mobx": "^6.6.1",
66
+ "mobx": "^6.6.2",
67
67
  "mobx-react-lite": "^3.4.0",
68
68
  "react": "^18 || ^17"
69
69
  }