react-app-store-manager 2.1.2 → 3.0.2
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/dist/Store.d.ts +10 -28
- package/dist/Store.js +37 -17
- package/dist/createStore.d.ts +1 -1
- package/dist/createStore.js +2 -2
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/useStore.d.ts +1 -1
- package/dist/useStore.js +14 -2
- package/package.json +1 -1
package/dist/Store.d.ts
CHANGED
@@ -1,35 +1,17 @@
|
|
1
|
-
import
|
2
|
-
interface
|
1
|
+
import { Context } from "react";
|
2
|
+
interface Listener {
|
3
|
+
path: string;
|
3
4
|
onUpdate(): void;
|
4
5
|
}
|
5
|
-
declare
|
6
|
-
export interface StoreInterface<Value, Actions> {
|
7
|
-
value: Value;
|
8
|
-
actions: Actions;
|
9
|
-
update(store: (store: Value) => Value): void;
|
10
|
-
action(actions: (action: Actions) => any): any;
|
11
|
-
context: Context<Value>;
|
12
|
-
subscribe(listener: ListenerInterface): void;
|
13
|
-
unsubscribe(listener: ListenerInterface): void;
|
14
|
-
}
|
15
|
-
export declare class Store<Value, Actions> {
|
6
|
+
export declare class Store<Value> {
|
16
7
|
private __value;
|
17
|
-
private readonly __actions?;
|
18
8
|
private readonly __context;
|
19
9
|
private __listeners;
|
20
|
-
constructor(__value: Value,
|
21
|
-
value:
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
private
|
26
|
-
__store: {
|
27
|
-
__context: React.Context<Value>;
|
28
|
-
__value: Value;
|
29
|
-
__actions: Actions | undefined;
|
30
|
-
__listeners: ListenerInterface[];
|
31
|
-
__subscribe: (listener: ListenerInterface) => void;
|
32
|
-
__unsubscribe: (listener: ListenerInterface) => void;
|
33
|
-
};
|
10
|
+
constructor(__value: Value, __context?: Context<Value>, __listeners?: Listener[]);
|
11
|
+
update<N>(path: (store: Value) => N, update: (value: N) => N): boolean;
|
12
|
+
get<N>(path: (store: Value) => N): N;
|
13
|
+
__subscribe(listener: Listener): void;
|
14
|
+
__unsubscribe(listener: Listener): void;
|
15
|
+
private updateObjectAddressByString;
|
34
16
|
}
|
35
17
|
export {};
|
package/dist/Store.js
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
3
|
+
__assign = Object.assign || function(t) {
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
+
s = arguments[i];
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
+
t[p] = s[p];
|
8
|
+
}
|
9
|
+
return t;
|
10
|
+
};
|
11
|
+
return __assign.apply(this, arguments);
|
12
|
+
};
|
2
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
15
|
};
|
@@ -7,39 +18,48 @@ exports.Store = void 0;
|
|
7
18
|
var react_1 = __importDefault(require("react"));
|
8
19
|
var isEqual_1 = require("./isEqual");
|
9
20
|
var Store = /** @class */ (function () {
|
10
|
-
function Store(__value,
|
21
|
+
function Store(__value, __context, __listeners) {
|
11
22
|
if (__context === void 0) { __context = react_1["default"].createContext(__value); }
|
12
23
|
if (__listeners === void 0) { __listeners = []; }
|
13
24
|
this.__value = __value;
|
14
|
-
this.__actions = __actions;
|
15
25
|
this.__context = __context;
|
16
26
|
this.__listeners = __listeners;
|
17
|
-
this.value = this.__value;
|
18
|
-
this.actions = this.__actions;
|
19
|
-
this.__store = {
|
20
|
-
__context: this.__context,
|
21
|
-
__value: this.__value,
|
22
|
-
__actions: this.__actions,
|
23
|
-
__listeners: this.__listeners,
|
24
|
-
__subscribe: this.__subscribe,
|
25
|
-
__unsubscribe: this.__unsubscribe
|
26
|
-
};
|
27
27
|
}
|
28
|
-
Store.prototype.update = function (
|
29
|
-
var
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
Store.prototype.update = function (path, update) {
|
29
|
+
var objectPath = path.toString().split("=>")[1].trim().split(".").filter(function (val, key) { return key > 0; }).join(".");
|
30
|
+
var oldValue = path(this.__value);
|
31
|
+
var newValue = update(path(this.__value));
|
32
|
+
if (!(0, isEqual_1.isEqual)(newValue, oldValue)) {
|
33
|
+
this.__value = (__assign({}, this.__value));
|
34
|
+
this.updateObjectAddressByString(objectPath, newValue);
|
35
|
+
this.__listeners.forEach(function (listener) {
|
36
|
+
if (listener.path.includes(objectPath) || objectPath.includes(listener.path)) {
|
37
|
+
listener.onUpdate();
|
38
|
+
}
|
39
|
+
});
|
33
40
|
return true;
|
34
41
|
}
|
35
42
|
return false;
|
36
43
|
};
|
44
|
+
Store.prototype.get = function (path) {
|
45
|
+
return path(this.__value);
|
46
|
+
};
|
37
47
|
Store.prototype.__subscribe = function (listener) {
|
38
48
|
this.__listeners.push(listener);
|
39
49
|
};
|
40
50
|
Store.prototype.__unsubscribe = function (listener) {
|
41
51
|
this.__listeners = this.__listeners.filter(function (l) { return l !== listener; });
|
42
52
|
};
|
53
|
+
Store.prototype.updateObjectAddressByString = function (path, value) {
|
54
|
+
var object = this.__value;
|
55
|
+
var stack = path.split('.');
|
56
|
+
while (stack.length > 1) {
|
57
|
+
// @ts-ignore
|
58
|
+
object = object[stack.shift()];
|
59
|
+
}
|
60
|
+
// @ts-ignore
|
61
|
+
object[stack.shift()] = value;
|
62
|
+
};
|
43
63
|
return Store;
|
44
64
|
}());
|
45
65
|
exports.Store = Store;
|
package/dist/createStore.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
import { Store } from "./Store";
|
2
|
-
export declare function createStore<V
|
2
|
+
export declare function createStore<V extends {}>(value: object): 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
1
|
import { Store } from "./Store";
|
2
|
-
export declare function useStore<
|
2
|
+
export declare function useStore<S, V extends any = any>(store: Store<S>, path: (store: S) => V): any;
|
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; }, false), refresh = _a[1];
|
8
|
+
var listener = {
|
9
|
+
path: objectPath,
|
10
|
+
onUpdate: refresh
|
11
|
+
};
|
12
|
+
(0, react_1.useEffect)(function () {
|
13
|
+
store.__subscribe(listener);
|
14
|
+
return function () {
|
15
|
+
store.__unsubscribe(listener);
|
16
|
+
};
|
17
|
+
}, []);
|
18
|
+
return store.get(path);
|
7
19
|
}
|
8
20
|
exports.useStore = useStore;
|