@lomray/react-mobx-manager 1.0.0-beta.6 → 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/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
@@ -70,14 +75,14 @@ declare class Manager {
70
75
  * Get store identity
71
76
  * @protected
72
77
  */
73
- 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;
74
79
  /**
75
80
  * Get exist store
76
81
  */
77
82
  /**
78
83
  * Get exist store
79
84
  */
80
- getStore<T>(store: IConstructableStore<T>, id?: string): T | undefined;
85
+ getStore<T>(store: IConstructableStore<T>, id?: string, index?: number): T | undefined;
81
86
  /**
82
87
  * Create new store instance
83
88
  * @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: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=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){return t||e.id||e.name||e.constructor.name}getStore(e,t){const s=r.getStoreKey(e,t);return this.stores.has(s)?this.stores.get(s):e.isSingleton?this.createStore(e,t):void 0}createStore(e,t){var s,i,o;const n=r.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&&r.persistedStores.has(n)&&(null===(s=l.wakeup)||void 0===s||s.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,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=>{const t=r.getStoreKey(e);e.isSingleton||this.stores.delete(t)}))}}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){const o=r.getStoreKey(e,i);if(r.persistedStores.has(o))throw new Error(`Duplicate serializable store key: ${o}`);return r.persistedStores.add(i),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;
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.6",
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
  }