react-app-store-manager 1.0.0 → 1.0.1
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/index.d.ts +30 -0
- package/dist/index.js +70 -0
- package/package.json +14 -10
- package/compilled/StoreManager.tsx +0 -76
- package/src/StoreManager.tsx +0 -76
package/dist/index.d.ts
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
import React, { ReactNode } from "react";
|
2
|
+
export declare function createStore<T>(defaultValue: T): StoreInterface<T>;
|
3
|
+
export declare function useStore<T>(store: StoreInterface<T>): T;
|
4
|
+
export interface StateProviderProps {
|
5
|
+
children: ReactNode;
|
6
|
+
store: StoreInterface<any>;
|
7
|
+
}
|
8
|
+
export declare const StateProvider: ({ children, store }: StateProviderProps) => JSX.Element;
|
9
|
+
declare type getFunction<S> = () => S;
|
10
|
+
declare type changeFunction<S> = (store: S) => S;
|
11
|
+
export interface StoreInterface<T> {
|
12
|
+
get(): T;
|
13
|
+
update(callback: (state: T) => T): void;
|
14
|
+
context: React.Context<any>;
|
15
|
+
getCallback?: getFunction<T>;
|
16
|
+
changeCallback?: changeFunction<T>;
|
17
|
+
addListener(onGet: getFunction<T>, onChange: changeFunction<T>): void;
|
18
|
+
removeListener(): void;
|
19
|
+
}
|
20
|
+
export declare class Store implements StoreInterface<any> {
|
21
|
+
context: React.Context<any>;
|
22
|
+
constructor(defaultValue: any);
|
23
|
+
getCallback?: getFunction<any>;
|
24
|
+
changeCallback?: changeFunction<any>;
|
25
|
+
get: () => any;
|
26
|
+
update: (callback: any) => any;
|
27
|
+
addListener(onGet: getFunction<any>, onChange: changeFunction<any>): void;
|
28
|
+
removeListener(): void;
|
29
|
+
}
|
30
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,70 @@
|
|
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
|
+
exports.Store = exports.StateProvider = exports.useStore = exports.createStore = void 0;
|
27
|
+
var react_1 = __importStar(require("react"));
|
28
|
+
function createStore(defaultValue) {
|
29
|
+
return new Store(defaultValue);
|
30
|
+
}
|
31
|
+
exports.createStore = createStore;
|
32
|
+
function useStore(store) {
|
33
|
+
return (0, react_1.useContext)(store.context);
|
34
|
+
}
|
35
|
+
exports.useStore = useStore;
|
36
|
+
var StateProvider = function (_a) {
|
37
|
+
var children = _a.children, store = _a.store;
|
38
|
+
// @ts-ignore
|
39
|
+
var _b = (0, react_1.useState)(store.context._currentValue), storeContent = _b[0], setStoreContent = _b[1];
|
40
|
+
var storeRef = (0, react_1.useRef)();
|
41
|
+
(0, react_1.useMemo)(function () {
|
42
|
+
storeRef.current = storeContent;
|
43
|
+
store.addListener(function () { return storeRef.current; }, function (store) {
|
44
|
+
setStoreContent(store);
|
45
|
+
storeRef.current = store;
|
46
|
+
});
|
47
|
+
return function () { return store.removeListener(); };
|
48
|
+
}, [store]);
|
49
|
+
var StoreContext = store.context;
|
50
|
+
return (react_1.default.createElement(StoreContext.Provider, { value: storeContent }, children));
|
51
|
+
};
|
52
|
+
exports.StateProvider = StateProvider;
|
53
|
+
var Store = /** @class */ (function () {
|
54
|
+
function Store(defaultValue) {
|
55
|
+
var _this = this;
|
56
|
+
this.get = function () { return _this.getCallback && _this.getCallback(); };
|
57
|
+
this.update = function (callback) { return _this.changeCallback && _this.changeCallback(callback); };
|
58
|
+
this.context = (0, react_1.createContext)(defaultValue);
|
59
|
+
}
|
60
|
+
Store.prototype.addListener = function (onGet, onChange) {
|
61
|
+
this.getCallback = onGet;
|
62
|
+
this.changeCallback = onChange;
|
63
|
+
};
|
64
|
+
Store.prototype.removeListener = function () {
|
65
|
+
this.getCallback = undefined;
|
66
|
+
this.changeCallback = undefined;
|
67
|
+
};
|
68
|
+
return Store;
|
69
|
+
}());
|
70
|
+
exports.Store = Store;
|
package/package.json
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-app-store-manager",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.1",
|
4
4
|
"description": "",
|
5
|
-
"main": "index.js",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
6
7
|
"scripts": {
|
7
|
-
"build": "
|
8
|
+
"build": "tsc"
|
8
9
|
},
|
9
|
-
"
|
10
|
+
"files": [
|
11
|
+
"dist/"
|
12
|
+
],
|
13
|
+
"author": "roma4ka",
|
10
14
|
"license": "ISC",
|
11
|
-
"devDependencies": {
|
12
|
-
"@babel/cli": "^7.17.10",
|
13
|
-
"@babel/core": "^7.18.5",
|
14
|
-
"@babel/preset-env": "^7.18.2"
|
15
|
-
},
|
16
15
|
"dependencies": {
|
17
|
-
"
|
16
|
+
"jest": "^28.1.1",
|
17
|
+
"react": "^18.2.0",
|
18
|
+
"ts-jest": "^28.0.5"
|
19
|
+
},
|
20
|
+
"devDependencies": {
|
21
|
+
"@types/react": "^18.0.14"
|
18
22
|
}
|
19
23
|
}
|
@@ -1,76 +0,0 @@
|
|
1
|
-
import React, {createContext, useContext, useMemo, useRef, useState} from "react";
|
2
|
-
|
3
|
-
export function createStore<T>(defaultValue: T) : StoreInterface<T> {
|
4
|
-
return new Store(defaultValue)
|
5
|
-
}
|
6
|
-
|
7
|
-
interface StoreInterface<T> {
|
8
|
-
get() : T
|
9
|
-
update(callback : (state: T) => T) : void
|
10
|
-
|
11
|
-
context: React.Context<any>
|
12
|
-
|
13
|
-
getCallback: getFunction<T>
|
14
|
-
changeCallback: changeFunction<T>
|
15
|
-
|
16
|
-
addListener(onGet : getFunction<T>,onChange : changeFunction<T>): void;
|
17
|
-
removeListener() : void
|
18
|
-
}
|
19
|
-
|
20
|
-
type getFunction<S> = () => S
|
21
|
-
type changeFunction<S> = (store: S) => S
|
22
|
-
|
23
|
-
class Store implements StoreInterface<any> {
|
24
|
-
|
25
|
-
context
|
26
|
-
|
27
|
-
constructor(defaultValue: any) {
|
28
|
-
this.context = createContext(defaultValue)
|
29
|
-
}
|
30
|
-
|
31
|
-
getCallback: any
|
32
|
-
changeCallback: any
|
33
|
-
|
34
|
-
get = () => this.getCallback()
|
35
|
-
|
36
|
-
update = (callback: any) => this.changeCallback(callback)
|
37
|
-
|
38
|
-
addListener(onGet : getFunction<any>,onChange : changeFunction<any>) : void {
|
39
|
-
this.getCallback = onGet
|
40
|
-
this.changeCallback = onChange
|
41
|
-
}
|
42
|
-
removeListener() {
|
43
|
-
this.getCallback = null
|
44
|
-
this.changeCallback = null
|
45
|
-
}
|
46
|
-
}
|
47
|
-
|
48
|
-
export const StateProvider = ({children,store} : {children: any,store: StoreInterface<any>}) => {
|
49
|
-
// @ts-ignore
|
50
|
-
const [storeContent,setStoreContent] = useState(store.context._currentValue)
|
51
|
-
const storeRef = useRef()
|
52
|
-
|
53
|
-
storeRef.current = storeContent
|
54
|
-
|
55
|
-
useMemo(() => {
|
56
|
-
store.addListener(() => storeRef.current,(store) => {
|
57
|
-
setStoreContent(store)
|
58
|
-
storeRef.current = store
|
59
|
-
})
|
60
|
-
|
61
|
-
return () => store.removeListener()
|
62
|
-
},[store])
|
63
|
-
|
64
|
-
const StoreContext = store.context
|
65
|
-
return (
|
66
|
-
<StoreContext.Provider
|
67
|
-
value={storeContent}
|
68
|
-
>
|
69
|
-
{children}
|
70
|
-
</StoreContext.Provider>
|
71
|
-
)
|
72
|
-
}
|
73
|
-
|
74
|
-
export function useStore<T>(store: StoreInterface<T>) {
|
75
|
-
return useContext<T>(store.context)
|
76
|
-
}
|
package/src/StoreManager.tsx
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
import React, {createContext, useContext, useMemo, useRef, useState} from "react";
|
2
|
-
|
3
|
-
export function createStore<T>(defaultValue: T) : StoreInterface<T> {
|
4
|
-
return new Store(defaultValue)
|
5
|
-
}
|
6
|
-
|
7
|
-
interface StoreInterface<T> {
|
8
|
-
get() : T
|
9
|
-
update(callback : (state: T) => T) : void
|
10
|
-
|
11
|
-
context: React.Context<any>
|
12
|
-
|
13
|
-
getCallback: getFunction<T>
|
14
|
-
changeCallback: changeFunction<T>
|
15
|
-
|
16
|
-
addListener(onGet : getFunction<T>,onChange : changeFunction<T>): void;
|
17
|
-
removeListener() : void
|
18
|
-
}
|
19
|
-
|
20
|
-
type getFunction<S> = () => S
|
21
|
-
type changeFunction<S> = (store: S) => S
|
22
|
-
|
23
|
-
class Store implements StoreInterface<any> {
|
24
|
-
|
25
|
-
context
|
26
|
-
|
27
|
-
constructor(defaultValue: any) {
|
28
|
-
this.context = createContext(defaultValue)
|
29
|
-
}
|
30
|
-
|
31
|
-
getCallback: any
|
32
|
-
changeCallback: any
|
33
|
-
|
34
|
-
get = () => this.getCallback()
|
35
|
-
|
36
|
-
update = (callback: any) => this.changeCallback(callback)
|
37
|
-
|
38
|
-
addListener(onGet : getFunction<any>,onChange : changeFunction<any>) : void {
|
39
|
-
this.getCallback = onGet
|
40
|
-
this.changeCallback = onChange
|
41
|
-
}
|
42
|
-
removeListener() {
|
43
|
-
this.getCallback = null
|
44
|
-
this.changeCallback = null
|
45
|
-
}
|
46
|
-
}
|
47
|
-
|
48
|
-
export const StateProvider = ({children,store} : {children: any,store: StoreInterface<any>}) => {
|
49
|
-
// @ts-ignore
|
50
|
-
const [storeContent,setStoreContent] = useState(store.context._currentValue)
|
51
|
-
const storeRef = useRef()
|
52
|
-
|
53
|
-
storeRef.current = storeContent
|
54
|
-
|
55
|
-
useMemo(() => {
|
56
|
-
store.addListener(() => storeRef.current,(store) => {
|
57
|
-
setStoreContent(store)
|
58
|
-
storeRef.current = store
|
59
|
-
})
|
60
|
-
|
61
|
-
return () => store.removeListener()
|
62
|
-
},[store])
|
63
|
-
|
64
|
-
const StoreContext = store.context
|
65
|
-
return (
|
66
|
-
<StoreContext.Provider
|
67
|
-
value={storeContent}
|
68
|
-
>
|
69
|
-
{children}
|
70
|
-
</StoreContext.Provider>
|
71
|
-
)
|
72
|
-
}
|
73
|
-
|
74
|
-
export function useStore<T>(store: StoreInterface<T>) {
|
75
|
-
return useContext<T>(store.context)
|
76
|
-
}
|