@lomray/react-mobx-manager 1.0.0-beta.3 → 1.0.0-beta.6
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 +98 -5
- package/lib/context.js +1 -38
- package/lib/index.js +1 -21
- package/lib/manager.d.ts +0 -2
- package/lib/manager.js +1 -228
- package/lib/on-change-listener.js +1 -25
- package/lib/storages/local-storage.js +1 -30
- package/lib/wakeup.js +1 -12
- package/lib/with-stores.js +1 -35
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -1,9 +1,102 @@
|
|
|
1
|
-
# Mobx stores manager for React
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
<p align="center">
|
|
3
|
+
<img src="https://user-images.githubusercontent.com/95251720/180519123-eb8a36e7-e7af-41f2-9a01-ae6d6b6a94f3.svg" alt="Bootstrap logo" width="200" height="165">
|
|
4
|
+
</p>
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
<h3 align='center'>Mobx stores manager for React</h3>
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
9
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
10
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
11
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
12
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
13
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
14
|
+
[](https://sonarcloud.io/summary/new_code?id=Lomray-Software_react-mobx-manager)
|
|
15
|
+
|
|
16
|
+
## Table of contents
|
|
17
|
+
|
|
18
|
+
- [Getting started](#getting-started)
|
|
19
|
+
- [Bugs and feature requests](#bugs-and-feature-requests)
|
|
20
|
+
- [Copyright](#copyright)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Getting started
|
|
24
|
+
|
|
25
|
+
The React-mobx-manager package is distributed using [npm](https://www.npmjs.com/), the node package manager.
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
npm i --save @lomray/react-mobx-manager
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Import `Manager, StoreManagerProvider` from `@lomray/react-mobx-manager` into your index file after wrap `<App/>` with `<StoreManagerProvider/>`
|
|
32
|
+
|
|
33
|
+
```jsx
|
|
34
|
+
import React from 'react';
|
|
35
|
+
import ReactDOM from 'react-dom/client';
|
|
36
|
+
import './index.css';
|
|
37
|
+
import { Manager, StoreManagerProvider } from '@lomray/react-mobx-manager';
|
|
38
|
+
import App from './app';
|
|
39
|
+
|
|
40
|
+
const storeManager = new Manager();
|
|
41
|
+
|
|
42
|
+
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
|
|
43
|
+
|
|
44
|
+
root.render(
|
|
45
|
+
<React.StrictMode>
|
|
46
|
+
<StoreManagerProvider storeManager={storeManager} shouldInit>
|
|
47
|
+
<App />
|
|
48
|
+
</StoreManagerProvider>
|
|
49
|
+
</React.StrictMode>,
|
|
50
|
+
);
|
|
9
51
|
```
|
|
52
|
+
|
|
53
|
+
Connect mobx store to manager and you're good to go!
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
import { withStores } from '@lomray/react-mobx-manager';
|
|
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
|
+
*/
|
|
93
|
+
export default withStores(User, stores);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Bugs and feature requests
|
|
97
|
+
|
|
98
|
+
Bug or a feature request, [please open a new issue](https://github.com/Lomray-Software/react-mobx-manager/issues/new).
|
|
99
|
+
|
|
100
|
+
## Copyright
|
|
101
|
+
|
|
102
|
+
Code and documentation copyright 2022 the [Lomray Software](https://lomray.com/).
|
package/lib/context.js
CHANGED
|
@@ -1,38 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var React = require('react');
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Mobx store manager context
|
|
13
|
-
*/
|
|
14
|
-
const StoreManagerContext = React__default["default"].createContext({});
|
|
15
|
-
/**
|
|
16
|
-
* Mobx store manager provider
|
|
17
|
-
* @constructor
|
|
18
|
-
*/
|
|
19
|
-
const StoreManagerProvider = ({ children, storeManager, fallback, shouldInit = false, }) => {
|
|
20
|
-
const [isInit, setInit] = React.useState(!shouldInit);
|
|
21
|
-
React.useEffect(() => {
|
|
22
|
-
if (!shouldInit) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
storeManager
|
|
26
|
-
.init()
|
|
27
|
-
.then(() => setInit(true))
|
|
28
|
-
.catch((e) => {
|
|
29
|
-
console.error('Failed initialized store manager: ', e);
|
|
30
|
-
});
|
|
31
|
-
}, [shouldInit, storeManager]);
|
|
32
|
-
return (React__default["default"].createElement(StoreManagerContext.Provider, { value: storeManager, children: isInit ? children : fallback || children }));
|
|
33
|
-
};
|
|
34
|
-
const useStoreManagerContext = () => React.useContext(StoreManagerContext);
|
|
35
|
-
|
|
36
|
-
exports.StoreManagerContext = StoreManagerContext;
|
|
37
|
-
exports.StoreManagerProvider = StoreManagerProvider;
|
|
38
|
-
exports.useStoreManagerContext = useStoreManagerContext;
|
|
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({});exports.StoreManagerContext=a,exports.StoreManagerProvider=({children:t,storeManager:o,fallback:n,shouldInit:i=!1})=>{const[s,u]=e.useState(!i);return e.useEffect((()=>{i&&o.init().then((()=>u(!0))).catch((e=>{console.error("Failed initialized store manager: ",e)}))}),[i,o]),r.default.createElement(a.Provider,{value:o,children:s?t:n||t})},exports.useStoreManagerContext=()=>e.useContext(a);
|
package/lib/index.js
CHANGED
|
@@ -1,21 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var context = require('./context.js');
|
|
6
|
-
var manager = require('./manager.js');
|
|
7
|
-
var onChangeListener = require('./on-change-listener.js');
|
|
8
|
-
var wakeup = require('./wakeup.js');
|
|
9
|
-
var withStores = require('./with-stores.js');
|
|
10
|
-
var localStorage = require('./storages/local-storage.js');
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
exports.StoreManagerContext = context.StoreManagerContext;
|
|
15
|
-
exports.StoreManagerProvider = context.StoreManagerProvider;
|
|
16
|
-
exports.useStoreManagerContext = context.useStoreManagerContext;
|
|
17
|
-
exports.Manager = manager;
|
|
18
|
-
exports.onChangeListener = onChangeListener;
|
|
19
|
-
exports.wakeup = wakeup;
|
|
20
|
-
exports.withStores = withStores;
|
|
21
|
-
exports.MobxLocalStorage = localStorage;
|
|
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"),s=require("./with-stores.js"),a=require("./storages/local-storage.js");exports.StoreManagerContext=e.StoreManagerContext,exports.StoreManagerProvider=e.StoreManagerProvider,exports.useStoreManagerContext=e.useStoreManagerContext,exports.Manager=r,exports.onChangeListener=t,exports.wakeup=o,exports.withStores=s,exports.MobxLocalStorage=a;
|
package/lib/manager.d.ts
CHANGED
|
@@ -57,11 +57,9 @@ declare class Manager {
|
|
|
57
57
|
init(): Promise<Manager>;
|
|
58
58
|
/**
|
|
59
59
|
* Get manager instance
|
|
60
|
-
* NOTE: Need call 'init' before call this method
|
|
61
60
|
*/
|
|
62
61
|
/**
|
|
63
62
|
* Get manager instance
|
|
64
|
-
* NOTE: Need call 'init' before call this method
|
|
65
63
|
*/
|
|
66
64
|
static get(): Manager;
|
|
67
65
|
/**
|
package/lib/manager.js
CHANGED
|
@@ -1,228 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var mobx = require('mobx');
|
|
4
|
-
var onChangeListener = require('./on-change-listener.js');
|
|
5
|
-
var wakeup = require('./wakeup.js');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Mobx stores manager
|
|
9
|
-
*/
|
|
10
|
-
class Manager {
|
|
11
|
-
/**
|
|
12
|
-
* Manger instance
|
|
13
|
-
*/
|
|
14
|
-
static instance;
|
|
15
|
-
/**
|
|
16
|
-
* Created stores
|
|
17
|
-
*/
|
|
18
|
-
stores = new Map();
|
|
19
|
-
/**
|
|
20
|
-
* Save persisted stores identities
|
|
21
|
-
* @private
|
|
22
|
-
*/
|
|
23
|
-
static persistedStores = new Set();
|
|
24
|
-
/**
|
|
25
|
-
* Initial stores state (local storage, custom etc.)
|
|
26
|
-
* @private
|
|
27
|
-
*/
|
|
28
|
-
initState;
|
|
29
|
-
/**
|
|
30
|
-
* Storage for persisted stores
|
|
31
|
-
* @private
|
|
32
|
-
*/
|
|
33
|
-
storage;
|
|
34
|
-
/**
|
|
35
|
-
* Restored persist storage data
|
|
36
|
-
* @protected
|
|
37
|
-
*/
|
|
38
|
-
persistData = {};
|
|
39
|
-
/**
|
|
40
|
-
* Additional store's constructor params
|
|
41
|
-
* @private
|
|
42
|
-
*/
|
|
43
|
-
storesParams;
|
|
44
|
-
/**
|
|
45
|
-
* Manager options
|
|
46
|
-
* @private
|
|
47
|
-
*/
|
|
48
|
-
options = {
|
|
49
|
-
shouldDisablePersist: false,
|
|
50
|
-
shouldRemoveInitState: true,
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* @constructor
|
|
54
|
-
*/
|
|
55
|
-
constructor({ initState, storesParams, storage, options } = {}) {
|
|
56
|
-
this.initState = initState || {};
|
|
57
|
-
this.storesParams = storesParams || {};
|
|
58
|
-
this.storage = storage;
|
|
59
|
-
Object.assign(this.options, options || {});
|
|
60
|
-
Manager.instance = this;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Init store manager
|
|
64
|
-
*/
|
|
65
|
-
async init() {
|
|
66
|
-
if (this.storage) {
|
|
67
|
-
this.persistData = (await this.storage.get()) || {};
|
|
68
|
-
}
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Get manager instance
|
|
73
|
-
* NOTE: Need call 'init' before call this method
|
|
74
|
-
*/
|
|
75
|
-
static get() {
|
|
76
|
-
if (!Manager.instance) {
|
|
77
|
-
throw new Error('Store manager is not initialized.');
|
|
78
|
-
}
|
|
79
|
-
return Manager.instance;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Get store identity
|
|
83
|
-
* @protected
|
|
84
|
-
*/
|
|
85
|
-
static getStoreKey(store, id) {
|
|
86
|
-
return id || store.id || store['name'] || store.constructor.name;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get exist store
|
|
90
|
-
*/
|
|
91
|
-
getStore(store, id) {
|
|
92
|
-
const storeKey = Manager.getStoreKey(store, id);
|
|
93
|
-
if (this.stores.has(storeKey)) {
|
|
94
|
-
return this.stores.get(storeKey);
|
|
95
|
-
}
|
|
96
|
-
// in case get from another store
|
|
97
|
-
if (store.isSingleton) {
|
|
98
|
-
return this.createStore(store, id);
|
|
99
|
-
}
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Create new store instance
|
|
104
|
-
* @protected
|
|
105
|
-
*/
|
|
106
|
-
createStore(store, id) {
|
|
107
|
-
const storeKey = Manager.getStoreKey(store, id);
|
|
108
|
-
const existStore = this.stores.get(storeKey);
|
|
109
|
-
if (existStore) {
|
|
110
|
-
return existStore;
|
|
111
|
-
}
|
|
112
|
-
const newStore = new store({ storeManager: this, ...this.storesParams });
|
|
113
|
-
// assign id to new store
|
|
114
|
-
newStore.id = storeKey;
|
|
115
|
-
newStore.isSingleton = store.isSingleton;
|
|
116
|
-
const initState = this.initState[storeKey];
|
|
117
|
-
const persistedState = this.persistData[storeKey];
|
|
118
|
-
if (initState) {
|
|
119
|
-
Object.assign(newStore, initState);
|
|
120
|
-
if (this.options.shouldRemoveInitState) {
|
|
121
|
-
delete this.initState[storeKey];
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
// Detect persisted store and restore state
|
|
125
|
-
if ('wakeup' in newStore && Manager.persistedStores.has(storeKey)) {
|
|
126
|
-
newStore.wakeup?.(newStore, { initState, persistedState });
|
|
127
|
-
newStore.addOnChangeListener?.(newStore, this);
|
|
128
|
-
}
|
|
129
|
-
this.stores.set(storeKey, newStore);
|
|
130
|
-
newStore.init?.();
|
|
131
|
-
return newStore;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Create stores for component
|
|
135
|
-
*/
|
|
136
|
-
createStores(map) {
|
|
137
|
-
return map.reduce((res, [key, store]) => {
|
|
138
|
-
const [s, id] = 'store' in store ? [store.store, store.id] : [store];
|
|
139
|
-
return {
|
|
140
|
-
...res,
|
|
141
|
-
[key]: this.createStore(s, id),
|
|
142
|
-
};
|
|
143
|
-
}, {});
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Mount stores to component
|
|
147
|
-
*/
|
|
148
|
-
mountStores(stores) {
|
|
149
|
-
const unmountCallbacks = [];
|
|
150
|
-
Object.values(stores).forEach((store) => {
|
|
151
|
-
if ('onMount' in store) {
|
|
152
|
-
const unsubscribe = store.onMount?.();
|
|
153
|
-
if (typeof unsubscribe === 'function') {
|
|
154
|
-
unmountCallbacks.push(unsubscribe);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
if ('onDestroy' in store) {
|
|
158
|
-
unmountCallbacks.push(() => store.onDestroy?.());
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
return () => {
|
|
162
|
-
unmountCallbacks.forEach((callback) => callback());
|
|
163
|
-
Object.values(stores).forEach((store) => {
|
|
164
|
-
const storeKey = Manager.getStoreKey(store);
|
|
165
|
-
if (!store.isSingleton) {
|
|
166
|
-
this.stores.delete(storeKey);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Get store's state
|
|
173
|
-
*/
|
|
174
|
-
toJSON() {
|
|
175
|
-
const result = {};
|
|
176
|
-
for (const [storeKey, store] of this.stores.entries()) {
|
|
177
|
-
result[storeKey] = store.toJSON?.() ?? Manager.getObservableProps(store);
|
|
178
|
-
}
|
|
179
|
-
return result;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Get persisted store's data
|
|
183
|
-
*/
|
|
184
|
-
toPersistedJSON() {
|
|
185
|
-
const result = {};
|
|
186
|
-
for (const storeKey of Manager.persistedStores) {
|
|
187
|
-
const store = this.stores.get(storeKey);
|
|
188
|
-
if (!store) {
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
result[storeKey] = store['toJSON']?.() ?? Manager.getObservableProps(store);
|
|
192
|
-
}
|
|
193
|
-
return result;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Get observable store props (fields)
|
|
197
|
-
* @private
|
|
198
|
-
*/
|
|
199
|
-
static getObservableProps(store) {
|
|
200
|
-
const props = mobx.toJS(store);
|
|
201
|
-
return Object.entries(props).reduce((res, [prop, value]) => ({
|
|
202
|
-
...res,
|
|
203
|
-
...(mobx.isObservableProp(store, prop) ? { [prop]: value } : {}),
|
|
204
|
-
}), {});
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Persist store
|
|
208
|
-
*/
|
|
209
|
-
static persistStore(store, id) {
|
|
210
|
-
const storeKey = Manager.getStoreKey(store, id);
|
|
211
|
-
if (Manager.persistedStores.has(storeKey)) {
|
|
212
|
-
throw new Error(`Duplicate serializable store key: ${storeKey}`);
|
|
213
|
-
}
|
|
214
|
-
Manager.persistedStores.add(id);
|
|
215
|
-
store.id = storeKey;
|
|
216
|
-
// add default wakeup handler
|
|
217
|
-
if (!('wakeup' in store.prototype)) {
|
|
218
|
-
store.prototype.wakeup = wakeup;
|
|
219
|
-
}
|
|
220
|
-
// add default changes listener
|
|
221
|
-
if (!('addOnChangeListener' in store.prototype)) {
|
|
222
|
-
store.prototype.addOnChangeListener = onChangeListener;
|
|
223
|
-
}
|
|
224
|
-
return store;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
module.exports = Manager;
|
|
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,25 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var mobx = require('mobx');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Listen persist store changes
|
|
7
|
-
*/
|
|
8
|
-
const onChangeListener = (store, manager) => {
|
|
9
|
-
const { shouldDisablePersist } = manager.options;
|
|
10
|
-
if (shouldDisablePersist || !manager.storage) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
return mobx.reaction(() => store.toJSON?.() ?? mobx.toJS(store), () => {
|
|
14
|
-
try {
|
|
15
|
-
manager.storage?.set(manager.toPersistedJSON())?.catch((e) => {
|
|
16
|
-
console.error('Failed to persist stores #1: ', e);
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
catch (e) {
|
|
20
|
-
console.error('Failed to persist stores #2: ', e);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
module.exports = onChangeListener;
|
|
1
|
+
"use strict";var o=require("mobx");module.exports=(r,e)=>{const{shouldDisablePersist:t}=e.options;if(!t&&e.storage)return o.reaction((()=>{var e,t;return null!==(t=null===(e=r.toJSON)||void 0===e?void 0:e.call(r))&&void 0!==t?t:o.toJS(r)}),(()=>{var o,r;try{null===(r=null===(o=e.storage)||void 0===o?void 0:o.set(e.toPersistedJSON()))||void 0===r||r.catch((o=>{console.error("Failed to persist stores #1: ",o)}))}catch(o){console.error("Failed to persist stores #2: ",o)}}))};
|
|
@@ -1,30 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class LocalStorage {
|
|
4
|
-
globalKey = 'stores';
|
|
5
|
-
/**
|
|
6
|
-
* @inheritDoc
|
|
7
|
-
*/
|
|
8
|
-
get() {
|
|
9
|
-
try {
|
|
10
|
-
return JSON.parse(localStorage.getItem(this.globalKey) || '{}');
|
|
11
|
-
}
|
|
12
|
-
catch (e) {
|
|
13
|
-
return {};
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* @inheritDoc
|
|
18
|
-
*/
|
|
19
|
-
flush() {
|
|
20
|
-
return localStorage.removeItem(this.globalKey);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* @inheritDoc
|
|
24
|
-
*/
|
|
25
|
-
set(value) {
|
|
26
|
-
return localStorage.setItem(this.globalKey, JSON.stringify(value || '{}'));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = LocalStorage;
|
|
1
|
+
"use strict";module.exports=class{constructor(){Object.defineProperty(this,"globalKey",{enumerable:!0,configurable:!0,writable:!0,value:"stores"})}get(){try{return JSON.parse(localStorage.getItem(this.globalKey)||"{}")}catch(e){return{}}}flush(){return localStorage.removeItem(this.globalKey)}set(e){return localStorage.setItem(this.globalKey,JSON.stringify(e||"{}"))}};
|
package/lib/wakeup.js
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Restore store state from initial state
|
|
5
|
-
*/
|
|
6
|
-
const wakeup = (store, { persistedState }) => {
|
|
7
|
-
if (persistedState) {
|
|
8
|
-
Object.assign(store, persistedState);
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
module.exports = wakeup;
|
|
1
|
+
"use strict";module.exports=(e,{persistedState:s})=>{s&&Object.assign(e,s)};
|
package/lib/with-stores.js
CHANGED
|
@@ -1,35 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var hoistNonReactStatics = require('hoist-non-react-statics');
|
|
4
|
-
var mobxReactLite = require('mobx-react-lite');
|
|
5
|
-
var React = require('react');
|
|
6
|
-
var context = require('./context.js');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var hoistNonReactStatics__default = /*#__PURE__*/_interopDefaultLegacy(hoistNonReactStatics);
|
|
11
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Make component observable and pass stores as props
|
|
15
|
-
*/
|
|
16
|
-
const withStores = (Component, stores) => {
|
|
17
|
-
const storesMap = Object.entries(stores);
|
|
18
|
-
const ObservableComponent = mobxReactLite.observer(Component);
|
|
19
|
-
const componentName = Component.displayName || Component.name;
|
|
20
|
-
const Element = React__default["default"].memo(({ ...props }) => {
|
|
21
|
-
const storeManager = context.useStoreManagerContext();
|
|
22
|
-
const [initStores] = React.useState(() => storeManager.createStores(storesMap));
|
|
23
|
-
/**
|
|
24
|
-
* - Check if store has 'onMount' (call if exist)
|
|
25
|
-
* - Check if store has 'onDestroy' method (call if exist)
|
|
26
|
-
*/
|
|
27
|
-
React.useEffect(() => storeManager.mountStores(initStores), [initStores, storeManager]);
|
|
28
|
-
return React__default["default"].createElement(ObservableComponent, { ...initStores, ...props });
|
|
29
|
-
});
|
|
30
|
-
hoistNonReactStatics__default["default"](Element, Component);
|
|
31
|
-
Element.displayName = `Mobx(${componentName})`;
|
|
32
|
-
return Element;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
module.exports = withStores;
|
|
1
|
+
"use strict";var e=require("hoist-non-react-statics"),t=require("mobx-react-lite"),r=require("react"),a=require("./context.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=o(e),u=o(r);module.exports=(e,o)=>{const n=Object.entries(o),c=t.observer(e),i=e.displayName||e.name,l=({...e})=>{const t=a.useStoreManagerContext(),[o]=r.useState((()=>t.createStores(n)));return r.useEffect((()=>t.mountStores(o)),[o,t]),u.default.createElement(c,{...o,...e})};return s.default(l,e),l.displayName=`Mobx(${i})`,l};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lomray/react-mobx-manager",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.6",
|
|
4
4
|
"description": "This package provides Mobx stores manager for react.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"homepage": "https://github.com/Lomray-Software/react-mobx-manager",
|
|
32
32
|
"scripts": {
|
|
33
33
|
"build": "rollup -c",
|
|
34
|
+
"build:dev": "rollup -c --environment BUILD:development",
|
|
34
35
|
"build:watch": "rollup -c -w --environment BUILD:development",
|
|
35
36
|
"prettier:format": "prettier --write 'src/**/*.{ts,tsx,*.ts,*tsx}'",
|
|
36
37
|
"prettier:check": "prettier --check --debug-check 'src/**/*.{ts,tsx,*.ts,*tsx}'",
|
|
@@ -54,6 +55,7 @@
|
|
|
54
55
|
"lint-staged": "^13.0.3",
|
|
55
56
|
"prettier": "^2.7.1",
|
|
56
57
|
"rollup": "^2.77.0",
|
|
58
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
57
59
|
"rollup-plugin-ts": "^3.0.2",
|
|
58
60
|
"semantic-release": "^19.0.3",
|
|
59
61
|
"ttypescript": "^1.5.13",
|