react-app-store-manager 2.0.3 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/Provider.d.ts +4 -4
- package/dist/Provider.js +3 -3
- package/dist/Store.d.ts +23 -12
- package/dist/Store.js +26 -15
- package/dist/createStore.d.ts +2 -2
- package/dist/isEqual.js +9 -14
- package/dist/useStore.d.ts +2 -2
- package/dist/useStore.js +1 -1
- package/package.json +1 -1
package/dist/Provider.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { ReactNode } from 'react';
|
2
|
-
import {
|
3
|
-
interface Props {
|
2
|
+
import { Store } from "./Store";
|
3
|
+
interface Props<V, E> {
|
4
4
|
children?: ReactNode;
|
5
|
-
store:
|
5
|
+
store: Store<V, E>;
|
6
6
|
}
|
7
|
-
export declare function Provider({ store, children }: Props): JSX.Element;
|
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
@@ -2,23 +2,34 @@ import React, { Context } from "react";
|
|
2
2
|
interface ListenerInterface {
|
3
3
|
onUpdate(): void;
|
4
4
|
}
|
5
|
-
|
6
|
-
|
5
|
+
declare type update<Value> = (store: Value) => Value;
|
6
|
+
export interface StoreInterface<Value, Actions> {
|
7
7
|
value: Value;
|
8
|
+
actions: Actions;
|
8
9
|
update(store: (store: Value) => Value): void;
|
9
|
-
action(actions: (action:
|
10
|
+
action(actions: (action: Actions) => any): any;
|
11
|
+
context: Context<Value>;
|
10
12
|
subscribe(listener: ListenerInterface): void;
|
11
13
|
unsubscribe(listener: ListenerInterface): void;
|
12
14
|
}
|
13
|
-
export declare class Store<Value,
|
14
|
-
|
15
|
+
export declare class Store<Value, Actions> {
|
16
|
+
private __value;
|
17
|
+
private readonly __actions?;
|
18
|
+
private readonly __context;
|
19
|
+
private __listeners;
|
20
|
+
constructor(__value: Value, __actions?: Actions | undefined, __context?: Context<Value>, __listeners?: ListenerInterface[]);
|
15
21
|
value: Value;
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
actions: Actions | undefined;
|
23
|
+
update(store: update<Value>): boolean;
|
24
|
+
private __subscribe;
|
25
|
+
private __unsubscribe;
|
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
|
+
};
|
23
34
|
}
|
24
35
|
export {};
|
package/dist/Store.js
CHANGED
@@ -7,27 +7,38 @@ 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, __actions, __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.__actions = __actions;
|
15
|
+
this.__context = __context;
|
16
|
+
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
|
+
};
|
15
27
|
}
|
16
|
-
Store.prototype.update = function (
|
17
|
-
var newValue =
|
28
|
+
Store.prototype.update = function (store) {
|
29
|
+
var newValue = store(this.value);
|
18
30
|
if (!(0, isEqual_1.isEqual)(newValue, this.value)) {
|
19
31
|
this.value = newValue;
|
20
|
-
this.
|
32
|
+
this.__listeners.map(function (listener) { return listener.onUpdate(); });
|
33
|
+
return true;
|
21
34
|
}
|
35
|
+
return false;
|
22
36
|
};
|
23
|
-
Store.prototype.
|
24
|
-
|
37
|
+
Store.prototype.__subscribe = function (listener) {
|
38
|
+
this.__listeners.push(listener);
|
25
39
|
};
|
26
|
-
Store.prototype.
|
27
|
-
this.
|
28
|
-
};
|
29
|
-
Store.prototype.unsubscribe = function (listener) {
|
30
|
-
this.listeners = this.listeners.filter(function (l) { return l !== listener; });
|
40
|
+
Store.prototype.__unsubscribe = function (listener) {
|
41
|
+
this.__listeners = this.__listeners.filter(function (l) { return l !== listener; });
|
31
42
|
};
|
32
43
|
return Store;
|
33
44
|
}());
|
package/dist/createStore.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import {
|
2
|
-
export declare function createStore<
|
1
|
+
import { Store } from "./Store";
|
2
|
+
export declare function createStore<V, A extends object | undefined = undefined>(value: any, actions?: A): Store<V, A>;
|
package/dist/isEqual.js
CHANGED
@@ -4,29 +4,24 @@ exports.isEqual = void 0;
|
|
4
4
|
function isEqual(newValue, oldValue) {
|
5
5
|
switch (typeof (newValue)) {
|
6
6
|
case 'object':
|
7
|
-
if (newValue === null || oldValue === null)
|
7
|
+
if (newValue === null || oldValue === null)
|
8
8
|
return newValue === oldValue;
|
9
|
+
if (newValue instanceof Date) {
|
10
|
+
return String(newValue) === String(oldValue);
|
9
11
|
}
|
10
|
-
if (newValue instanceof Date && oldValue instanceof Date && String(newValue) !== String(oldValue))
|
11
|
-
return false;
|
12
12
|
for (var prop in newValue) {
|
13
|
-
if (
|
14
|
-
return false;
|
15
|
-
if (!isEqual(newValue === null || newValue === void 0 ? void 0 : newValue[prop], oldValue === null || oldValue === void 0 ? void 0 : oldValue[prop]))
|
13
|
+
if (!(oldValue === null || oldValue === void 0 ? void 0 : oldValue.hasOwnProperty(prop)))
|
16
14
|
return false;
|
17
|
-
|
18
|
-
for (var prop in oldValue) {
|
19
|
-
if (typeof (newValue[prop]) == 'undefined')
|
15
|
+
if (!isEqual(newValue[prop], oldValue[prop]))
|
20
16
|
return false;
|
21
17
|
}
|
22
|
-
|
23
|
-
case 'function':
|
24
|
-
if (typeof (oldValue) !== 'function' || (newValue.toString() !== oldValue.toString()))
|
18
|
+
if (Object.keys(oldValue).length !== Object.keys(newValue).length)
|
25
19
|
return false;
|
26
20
|
break;
|
21
|
+
case 'function':
|
22
|
+
return typeof (oldValue) === 'function' && newValue.toString() === oldValue.toString();
|
27
23
|
default:
|
28
|
-
|
29
|
-
return false;
|
24
|
+
return newValue === oldValue;
|
30
25
|
}
|
31
26
|
return true;
|
32
27
|
}
|
package/dist/useStore.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import {
|
2
|
-
export declare function useStore<T>(store:
|
1
|
+
import { Store } from "./Store";
|
2
|
+
export declare function useStore<T, A = undefined>(store: Store<T, A>): T;
|
package/dist/useStore.js
CHANGED