react-app-store-manager 2.1.2 → 3.0.0

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 CHANGED
@@ -1,35 +1,24 @@
1
1
  import React, { Context } from "react";
2
2
  interface ListenerInterface {
3
+ path: string;
3
4
  onUpdate(): void;
4
5
  }
5
- declare type update<Value> = (store: Value) => Value;
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, __actions?: Actions | undefined, __context?: Context<Value>, __listeners?: ListenerInterface[]);
10
+ constructor(__value: Value, __context?: Context<Value>, __listeners?: ListenerInterface[]);
21
11
  value: Value;
22
- actions: Actions | undefined;
23
- update(store: update<Value>): boolean;
12
+ update<N>(path: (store: Value) => N, update: (value: N) => N): boolean;
24
13
  private __subscribe;
25
14
  private __unsubscribe;
26
15
  __store: {
27
16
  __context: React.Context<Value>;
28
17
  __value: Value;
29
- __actions: Actions | undefined;
30
18
  __listeners: ListenerInterface[];
31
19
  __subscribe: (listener: ListenerInterface) => void;
32
20
  __unsubscribe: (listener: ListenerInterface) => void;
33
21
  };
22
+ private updateObjectAddressByString;
34
23
  }
35
24
  export {};
package/dist/Store.js CHANGED
@@ -7,29 +7,32 @@ 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(__value, __actions, __context, __listeners) {
10
+ function Store(__value, __context, __listeners) {
11
11
  if (__context === void 0) { __context = react_1["default"].createContext(__value); }
12
12
  if (__listeners === void 0) { __listeners = []; }
13
13
  this.__value = __value;
14
- this.__actions = __actions;
15
14
  this.__context = __context;
16
15
  this.__listeners = __listeners;
17
16
  this.value = this.__value;
18
- this.actions = this.__actions;
19
17
  this.__store = {
20
18
  __context: this.__context,
21
19
  __value: this.__value,
22
- __actions: this.__actions,
23
20
  __listeners: this.__listeners,
24
21
  __subscribe: this.__subscribe,
25
22
  __unsubscribe: this.__unsubscribe
26
23
  };
27
24
  }
28
- Store.prototype.update = function (store) {
29
- var newValue = store(this.value);
30
- if (!(0, isEqual_1.isEqual)(newValue, this.value)) {
31
- this.value = newValue;
32
- this.__listeners.map(function (listener) { return listener.onUpdate(); });
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)) {
33
+ listener.onUpdate();
34
+ }
35
+ });
33
36
  return true;
34
37
  }
35
38
  return false;
@@ -40,6 +43,16 @@ var Store = /** @class */ (function () {
40
43
  Store.prototype.__unsubscribe = function (listener) {
41
44
  this.__listeners = this.__listeners.filter(function (l) { return l !== listener; });
42
45
  };
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;
55
+ };
43
56
  return Store;
44
57
  }());
45
58
  exports.Store = Store;
@@ -1,2 +1,2 @@
1
1
  import { Store } from "./Store";
2
- export declare function createStore<V, A extends object | undefined = undefined>(value: any, actions?: A): Store<V, A>;
2
+ export declare function createStore<V extends {}>(value: V): Store<V>;
@@ -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, actions) {
6
- return new Store_1.Store(value, actions);
5
+ function createStore(value) {
6
+ return new Store_1.Store(value);
7
7
  }
8
8
  exports.createStore = createStore;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export * from "./Provider";
2
1
  export * from "./createStore";
3
2
  export * from "./useStore";
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);
@@ -1,2 +1,2 @@
1
1
  import { Store } from "./Store";
2
- export declare function useStore<T, A = undefined>(store: Store<T, A>): T;
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
- return (0, react_1.useContext)(store.__store.__context);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-app-store-manager",
3
- "version": "2.1.2",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "tsc --declaration"