react-app-store-manager 3.0.0 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/Store.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import React, { Context } from "react";
2
- interface ListenerInterface {
1
+ import { Context } from "react";
2
+ interface Listener {
3
3
  path: string;
4
4
  onUpdate(): void;
5
5
  }
@@ -7,18 +7,11 @@ export declare class Store<Value> {
7
7
  private __value;
8
8
  private readonly __context;
9
9
  private __listeners;
10
- constructor(__value: Value, __context?: Context<Value>, __listeners?: ListenerInterface[]);
11
- value: Value;
10
+ constructor(__value: Value, __context?: Context<Value>, __listeners?: Listener[]);
12
11
  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
- };
12
+ get<N>(path: (store: Value) => N): N;
13
+ __subscribe(listener: Listener): void;
14
+ __unsubscribe(listener: Listener): void;
22
15
  private updateObjectAddressByString;
23
16
  }
24
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
  };
@@ -13,23 +24,16 @@ var Store = /** @class */ (function () {
13
24
  this.__value = __value;
14
25
  this.__context = __context;
15
26
  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
- };
24
27
  }
25
28
  Store.prototype.update = function (path, update) {
26
29
  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));
30
+ var oldValue = path(this.__value);
31
+ var newValue = update(path(this.__value));
29
32
  if (!(0, isEqual_1.isEqual)(newValue, oldValue)) {
33
+ this.__value = (__assign({}, this.__value));
30
34
  this.updateObjectAddressByString(objectPath, newValue);
31
35
  this.__listeners.forEach(function (listener) {
32
- if (objectPath.startsWith(listener.path)) {
36
+ if (listener.path.includes(objectPath) || objectPath.includes(listener.path)) {
33
37
  listener.onUpdate();
34
38
  }
35
39
  });
@@ -37,6 +41,9 @@ var Store = /** @class */ (function () {
37
41
  }
38
42
  return false;
39
43
  };
44
+ Store.prototype.get = function (path) {
45
+ return path(this.__value);
46
+ };
40
47
  Store.prototype.__subscribe = function (listener) {
41
48
  this.__listeners.push(listener);
42
49
  };
@@ -44,7 +51,7 @@ var Store = /** @class */ (function () {
44
51
  this.__listeners = this.__listeners.filter(function (l) { return l !== listener; });
45
52
  };
46
53
  Store.prototype.updateObjectAddressByString = function (path, value) {
47
- var object = this.value;
54
+ var object = this.__value;
48
55
  var stack = path.split('.');
49
56
  while (stack.length > 1) {
50
57
  // @ts-ignore
@@ -1,2 +1,2 @@
1
1
  import { Store } from "./Store";
2
- export declare function createStore<V extends {}>(value: V): Store<V>;
2
+ export declare function createStore<V extends {}>(value: object): Store<V>;
package/dist/useStore.js CHANGED
@@ -4,17 +4,17 @@ exports.useStore = void 0;
4
4
  var react_1 = require("react");
5
5
  function useStore(store, path) {
6
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];
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
+ };
8
12
  (0, react_1.useEffect)(function () {
9
- var listener = {
10
- path: objectPath,
11
- onUpdate: forceUpdate
12
- };
13
- store.__store.__subscribe(listener);
13
+ store.__subscribe(listener);
14
14
  return function () {
15
- store.__store.__unsubscribe(listener);
15
+ store.__unsubscribe(listener);
16
16
  };
17
17
  }, []);
18
- return path(store.value);
18
+ return store.get(path);
19
19
  }
20
20
  exports.useStore = useStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-app-store-manager",
3
- "version": "3.0.0",
3
+ "version": "3.0.3",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "tsc --declaration"