react-app-store-manager 2.1.1 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/Provider.d.ts +2 -2
- package/dist/Provider.js +3 -3
- package/dist/Store.d.ts +17 -18
- package/dist/Store.js +40 -16
- package/dist/createStore.d.ts +2 -2
- package/dist/createStore.js +2 -2
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/useStore.d.ts +2 -2
- package/dist/useStore.js +14 -2
- package/package.json +1 -1
package/dist/Provider.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { ReactNode } from 'react';
|
2
|
-
import {
|
2
|
+
import { Store } from "./Store";
|
3
3
|
interface Props<V, E> {
|
4
4
|
children?: ReactNode;
|
5
|
-
store:
|
5
|
+
store: Store<V, E>;
|
6
6
|
}
|
7
7
|
export declare function Provider<V, E>({ store, children }: Props<V, E>): JSX.Element;
|
8
8
|
export {};
|
package/dist/Provider.js
CHANGED
@@ -27,7 +27,7 @@ exports.Provider = void 0;
|
|
27
27
|
var react_1 = __importStar(require("react"));
|
28
28
|
function Provider(_a) {
|
29
29
|
var store = _a.store, children = _a.children;
|
30
|
-
var Provider = store.
|
30
|
+
var Provider = store.__store.__context;
|
31
31
|
var _b = (0, react_1.useReducer)(function (x) { return !x; }, true), updater = _b[0], forceUpdate = _b[1];
|
32
32
|
var value = (0, react_1.useMemo)(function () { return store.value; }, [store.value, updater]);
|
33
33
|
(0, react_1.useEffect)(function () {
|
@@ -36,8 +36,8 @@ function Provider(_a) {
|
|
36
36
|
forceUpdate();
|
37
37
|
}
|
38
38
|
};
|
39
|
-
store.
|
40
|
-
return function () { return store.
|
39
|
+
store.__store.__subscribe(listener);
|
40
|
+
return function () { return store.__store.__unsubscribe(listener); };
|
41
41
|
}, [store]);
|
42
42
|
return (react_1["default"].createElement(Provider.Provider, { value: value }, children));
|
43
43
|
}
|
package/dist/Store.d.ts
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
import React, { Context } from "react";
|
2
2
|
interface ListenerInterface {
|
3
|
+
path: string;
|
3
4
|
onUpdate(): void;
|
4
5
|
}
|
5
|
-
export
|
6
|
+
export declare class Store<Value> {
|
7
|
+
private __value;
|
8
|
+
private readonly __context;
|
9
|
+
private __listeners;
|
10
|
+
constructor(__value: Value, __context?: Context<Value>, __listeners?: ListenerInterface[]);
|
6
11
|
value: Value;
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
private listeners;
|
19
|
-
constructor(value: Value, actions: Actions);
|
20
|
-
update(callback: any): void;
|
21
|
-
action(callback: any): any;
|
22
|
-
subscribe(listener: ListenerInterface): void;
|
23
|
-
unsubscribe(listener: ListenerInterface): void;
|
12
|
+
update<N>(path: (store: Value) => N, update: (value: N) => N): boolean;
|
13
|
+
private __subscribe;
|
14
|
+
private __unsubscribe;
|
15
|
+
__store: {
|
16
|
+
__context: React.Context<Value>;
|
17
|
+
__value: Value;
|
18
|
+
__listeners: ListenerInterface[];
|
19
|
+
__subscribe: (listener: ListenerInterface) => void;
|
20
|
+
__unsubscribe: (listener: ListenerInterface) => void;
|
21
|
+
};
|
22
|
+
private updateObjectAddressByString;
|
24
23
|
}
|
25
24
|
export {};
|
package/dist/Store.js
CHANGED
@@ -7,27 +7,51 @@ exports.Store = void 0;
|
|
7
7
|
var react_1 = __importDefault(require("react"));
|
8
8
|
var isEqual_1 = require("./isEqual");
|
9
9
|
var Store = /** @class */ (function () {
|
10
|
-
function Store(
|
11
|
-
|
12
|
-
|
13
|
-
this.
|
14
|
-
this.
|
10
|
+
function Store(__value, __context, __listeners) {
|
11
|
+
if (__context === void 0) { __context = react_1["default"].createContext(__value); }
|
12
|
+
if (__listeners === void 0) { __listeners = []; }
|
13
|
+
this.__value = __value;
|
14
|
+
this.__context = __context;
|
15
|
+
this.__listeners = __listeners;
|
16
|
+
this.value = this.__value;
|
17
|
+
this.__store = {
|
18
|
+
__context: this.__context,
|
19
|
+
__value: this.__value,
|
20
|
+
__listeners: this.__listeners,
|
21
|
+
__subscribe: this.__subscribe,
|
22
|
+
__unsubscribe: this.__unsubscribe
|
23
|
+
};
|
15
24
|
}
|
16
|
-
Store.prototype.update = function (
|
17
|
-
var
|
18
|
-
|
19
|
-
|
20
|
-
|
25
|
+
Store.prototype.update = function (path, update) {
|
26
|
+
var objectPath = path.toString().split("=>")[1].trim().split(".").filter(function (val, key) { return key > 0; }).join(".");
|
27
|
+
var oldValue = path(this.value);
|
28
|
+
var newValue = update(path(this.value));
|
29
|
+
if (!(0, isEqual_1.isEqual)(newValue, oldValue)) {
|
30
|
+
this.updateObjectAddressByString(objectPath, newValue);
|
31
|
+
this.__listeners.forEach(function (listener) {
|
32
|
+
if (objectPath.startsWith(listener.path) || listener.path.startsWith(objectPath)) {
|
33
|
+
listener.onUpdate();
|
34
|
+
}
|
35
|
+
});
|
36
|
+
return true;
|
21
37
|
}
|
38
|
+
return false;
|
22
39
|
};
|
23
|
-
Store.prototype.
|
24
|
-
|
40
|
+
Store.prototype.__subscribe = function (listener) {
|
41
|
+
this.__listeners.push(listener);
|
25
42
|
};
|
26
|
-
Store.prototype.
|
27
|
-
this.
|
43
|
+
Store.prototype.__unsubscribe = function (listener) {
|
44
|
+
this.__listeners = this.__listeners.filter(function (l) { return l !== listener; });
|
28
45
|
};
|
29
|
-
Store.prototype.
|
30
|
-
|
46
|
+
Store.prototype.updateObjectAddressByString = function (path, value) {
|
47
|
+
var object = this.value;
|
48
|
+
var stack = path.split('.');
|
49
|
+
while (stack.length > 1) {
|
50
|
+
// @ts-ignore
|
51
|
+
object = object[stack.shift()];
|
52
|
+
}
|
53
|
+
// @ts-ignore
|
54
|
+
object[stack.shift()] = value;
|
31
55
|
};
|
32
56
|
return Store;
|
33
57
|
}());
|
package/dist/createStore.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import {
|
2
|
-
export declare function createStore<V
|
1
|
+
import { Store } from "./Store";
|
2
|
+
export declare function createStore<V extends {}>(value: V): Store<V>;
|
package/dist/createStore.js
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
exports.__esModule = true;
|
3
3
|
exports.createStore = void 0;
|
4
4
|
var Store_1 = require("./Store");
|
5
|
-
function createStore(value
|
6
|
-
return new Store_1.Store(value
|
5
|
+
function createStore(value) {
|
6
|
+
return new Store_1.Store(value);
|
7
7
|
}
|
8
8
|
exports.createStore = createStore;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
exports.__esModule = true;
|
17
|
-
__exportStar(require("./Provider"), exports);
|
18
17
|
__exportStar(require("./createStore"), exports);
|
19
18
|
__exportStar(require("./useStore"), exports);
|
package/dist/useStore.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import {
|
2
|
-
export declare function useStore<
|
1
|
+
import { Store } from "./Store";
|
2
|
+
export declare function useStore<S, V extends any = any>(store: Store<S>, path: (store: S) => V): V;
|
package/dist/useStore.js
CHANGED
@@ -2,7 +2,19 @@
|
|
2
2
|
exports.__esModule = true;
|
3
3
|
exports.useStore = void 0;
|
4
4
|
var react_1 = require("react");
|
5
|
-
function useStore(store) {
|
6
|
-
|
5
|
+
function useStore(store, path) {
|
6
|
+
var objectPath = path.toString().split("=>")[1].trim().split(".").filter(function (val, key) { return key > 0; }).join(".");
|
7
|
+
var _a = (0, react_1.useReducer)(function (x) { return !x; }, true), forceUpdate = _a[1];
|
8
|
+
(0, react_1.useEffect)(function () {
|
9
|
+
var listener = {
|
10
|
+
path: objectPath,
|
11
|
+
onUpdate: forceUpdate
|
12
|
+
};
|
13
|
+
store.__store.__subscribe(listener);
|
14
|
+
return function () {
|
15
|
+
store.__store.__unsubscribe(listener);
|
16
|
+
};
|
17
|
+
}, []);
|
18
|
+
return path(store.value);
|
7
19
|
}
|
8
20
|
exports.useStore = useStore;
|