react-app-store-manager 1.4.7 → 1.4.8

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { createStore } from "./src/createStore";
2
+ import Provider from "./src/Provider";
3
+ import { useStore } from "./src/useStore";
4
+ export declare type createStore = typeof createStore;
5
+ export declare type Provider = typeof Provider;
6
+ export declare type useStore = typeof useStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-app-store-manager",
3
- "version": "1.4.7",
3
+ "version": "1.4.8",
4
4
  "description": "",
5
5
  "types": "index.d.ts",
6
6
  "scripts": {
@@ -9,7 +9,8 @@
9
9
  },
10
10
  "main": "index.js",
11
11
  "files": [
12
- "."
12
+ "index.d.ts",
13
+ "src"
13
14
  ],
14
15
  "author": "roma4ka",
15
16
  "license": "ISC",
@@ -0,0 +1,8 @@
1
+ import { ReactNode } from 'react';
2
+ import { StoreInterface } from "./Store";
3
+ interface Props {
4
+ children?: ReactNode;
5
+ store: StoreInterface<any, any>;
6
+ }
7
+ declare const Provider: ({ store, children }: Props) => JSX.Element;
8
+ export default Provider;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const react_1 = __importStar(require("react"));
27
+ const Provider = ({ store, children }) => {
28
+ const Provider = store.context;
29
+ const [updater, forceUpdate] = (0, react_1.useReducer)(x => !x, true);
30
+ const value = (0, react_1.useMemo)(() => store.value, [store.value, updater]);
31
+ (0, react_1.useEffect)(() => {
32
+ const listener = {
33
+ onUpdate() {
34
+ forceUpdate();
35
+ }
36
+ };
37
+ store.subscribe(listener);
38
+ return () => store.unsubscribe(listener);
39
+ }, [store]);
40
+ return (react_1.default.createElement(Provider.Provider, { value: value }, children));
41
+ };
42
+ exports.default = Provider;
package/src/Store.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import React, { Context } from "react";
2
+ interface ListenerInterface {
3
+ onUpdate(): void;
4
+ }
5
+ export interface ActionsInterface {
6
+ [action: string]: (payload: any) => any;
7
+ }
8
+ export interface StoreInterface<Value, Action> {
9
+ context: Context<Value>;
10
+ value: Value;
11
+ actions: ActionsInterface;
12
+ listeners: ListenerInterface[];
13
+ update(store: (store: Value) => Value): void;
14
+ action(actions: (action: Action) => any): void;
15
+ subscribe(listener: ListenerInterface): void;
16
+ unsubscribe(listener: ListenerInterface): void;
17
+ }
18
+ export declare class Store<Value, Action> implements StoreInterface<Value, Action> {
19
+ context: React.Context<Value>;
20
+ value: Value;
21
+ actions: any;
22
+ listeners: ListenerInterface[];
23
+ constructor(value: Value, actions: any);
24
+ update(callback: any): void;
25
+ action(callback: any): void;
26
+ subscribe(listener: ListenerInterface): void;
27
+ unsubscribe(listener: ListenerInterface): void;
28
+ }
29
+ export {};
package/src/Store.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Store = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const isEqual_1 = require("./isEqual");
9
+ class Store {
10
+ constructor(value, actions) {
11
+ this.listeners = [];
12
+ this.context = react_1.default.createContext(value);
13
+ this.value = value;
14
+ this.actions = actions;
15
+ }
16
+ update(callback) {
17
+ const newValue = callback(this.value);
18
+ if (!(0, isEqual_1.isEqual)(newValue, this.value)) {
19
+ this.value = newValue;
20
+ this.listeners.map(listener => listener.onUpdate());
21
+ }
22
+ }
23
+ action(callback) {
24
+ callback(this.actions);
25
+ }
26
+ subscribe(listener) {
27
+ this.listeners.push(listener);
28
+ }
29
+ unsubscribe(listener) {
30
+ this.listeners = this.listeners.filter(l => l !== listener);
31
+ }
32
+ }
33
+ exports.Store = Store;
@@ -0,0 +1,2 @@
1
+ import { StoreInterface } from "./Store";
2
+ export declare function createStore<Value, Actions>(value: Value, actions?: Actions): StoreInterface<Value, Actions>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createStore = void 0;
4
+ const Store_1 = require("./Store");
5
+ function createStore(value, actions) {
6
+ return new Store_1.Store(value, actions !== null && actions !== void 0 ? actions : {});
7
+ }
8
+ exports.createStore = createStore;
@@ -0,0 +1 @@
1
+ export declare function isEqual(newValue: any, oldValue: any): boolean;
package/src/isEqual.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEqual = void 0;
4
+ function isEqual(newValue, oldValue) {
5
+ switch (typeof (newValue)) {
6
+ case 'object':
7
+ if (newValue === null || oldValue === null) {
8
+ return newValue === oldValue;
9
+ }
10
+ if (newValue instanceof Date && oldValue instanceof Date && String(newValue) !== String(oldValue))
11
+ return false;
12
+ for (let prop in newValue) {
13
+ if (newValue.hasOwnProperty(prop) !== (oldValue === null || oldValue === void 0 ? void 0 : oldValue.hasOwnProperty(prop)))
14
+ return false;
15
+ if (!isEqual(newValue === null || newValue === void 0 ? void 0 : newValue[prop], oldValue === null || oldValue === void 0 ? void 0 : oldValue[prop]))
16
+ return false;
17
+ }
18
+ for (let prop in oldValue) {
19
+ if (typeof (newValue[prop]) == 'undefined')
20
+ return false;
21
+ }
22
+ break;
23
+ case 'function':
24
+ if (typeof (oldValue) !== 'function' || (newValue.toString() !== oldValue.toString()))
25
+ return false;
26
+ break;
27
+ default:
28
+ if (newValue !== oldValue)
29
+ return false;
30
+ }
31
+ return true;
32
+ }
33
+ exports.isEqual = isEqual;
@@ -0,0 +1,2 @@
1
+ import { StoreInterface } from "./Store";
2
+ export declare function useStore<T>(store: StoreInterface<T, any>): T;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useStore = void 0;
4
+ const react_1 = require("react");
5
+ function useStore(store) {
6
+ return (0, react_1.useContext)(store.context);
7
+ }
8
+ exports.useStore = useStore;