react-app-store-manager 1.0.0 → 1.1.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/index.d.ts +20 -0
- package/dist/index.js +38 -0
- package/package.json +23 -10
- package/compilled/StoreManager.tsx +0 -76
- package/src/StoreManager.tsx +0 -76
package/dist/index.d.ts
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
export declare function createStore<T>(defaultValue: T): StoreInterface<T>;
|
2
|
+
declare type onChangeFunction = () => void;
|
3
|
+
export interface StoreInterface<T> {
|
4
|
+
value: T;
|
5
|
+
get(): T;
|
6
|
+
update(store: (store: T) => T): void;
|
7
|
+
addListener(onChange: onChangeFunction): void;
|
8
|
+
removeListener(onChange: onChangeFunction): void;
|
9
|
+
}
|
10
|
+
export declare class Store implements StoreInterface<any> {
|
11
|
+
value: any;
|
12
|
+
constructor(defaultValue: any);
|
13
|
+
changeCallbacks: any[];
|
14
|
+
get: () => any;
|
15
|
+
update(callback: any): void;
|
16
|
+
addListener: (onChange: any) => number;
|
17
|
+
removeListener: (onChange: any) => any[];
|
18
|
+
}
|
19
|
+
export declare function useStore<T>(store: StoreInterface<T>): T;
|
20
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
2
|
+
exports.useStore = exports.Store = exports.createStore = void 0;
|
3
|
+
var react_1 = require("react");
|
4
|
+
function createStore(defaultValue) {
|
5
|
+
return new Store(defaultValue);
|
6
|
+
}
|
7
|
+
exports.createStore = createStore;
|
8
|
+
var Store = /** @class */ (function () {
|
9
|
+
function Store(defaultValue) {
|
10
|
+
var _this = this;
|
11
|
+
this.changeCallbacks = [];
|
12
|
+
this.get = function () { return _this.value; };
|
13
|
+
this.addListener = function (onChange) { return _this.changeCallbacks.push(onChange); };
|
14
|
+
this.removeListener = function (onChange) { return _this.changeCallbacks = _this.changeCallbacks.filter(function (cb) { return cb !== onChange; }); };
|
15
|
+
this.value = defaultValue;
|
16
|
+
}
|
17
|
+
Store.prototype.update = function (callback) {
|
18
|
+
var _this = this;
|
19
|
+
this.value = callback(this.value);
|
20
|
+
this.changeCallbacks.forEach(function (callback) {
|
21
|
+
callback(_this.value);
|
22
|
+
});
|
23
|
+
};
|
24
|
+
return Store;
|
25
|
+
}());
|
26
|
+
exports.Store = Store;
|
27
|
+
function useStore(store) {
|
28
|
+
var _a = (0, react_1.useState)(true), change = _a[0], setChange = _a[1];
|
29
|
+
var changeRef = (0, react_1.useRef)(change);
|
30
|
+
changeRef.current = change;
|
31
|
+
var onChange = function () { return setChange(!changeRef.current); };
|
32
|
+
(0, react_1.useEffect)(function () {
|
33
|
+
store.addListener(onChange);
|
34
|
+
return function () { return store.removeListener(onChange); };
|
35
|
+
}, [store]);
|
36
|
+
return store.get();
|
37
|
+
}
|
38
|
+
exports.useStore = useStore;
|
package/package.json
CHANGED
@@ -1,19 +1,32 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-app-store-manager",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.1.0",
|
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",
|
9
|
+
"test": "node ./tests/index.ts"
|
8
10
|
},
|
9
|
-
"
|
11
|
+
"files": [
|
12
|
+
"dist/"
|
13
|
+
],
|
14
|
+
"type": "module",
|
15
|
+
"author": "roma4ka",
|
10
16
|
"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
17
|
"dependencies": {
|
17
|
-
"
|
18
|
+
"@testing-library/jest-dom": "^5.16.4",
|
19
|
+
"@testing-library/react": "^13.3.0",
|
20
|
+
"@testing-library/user-event": "^14.2.1",
|
21
|
+
"@types/node": "^18.0.0",
|
22
|
+
"@types/react-dom": "^18.0.5",
|
23
|
+
"jest": "^28.1.1",
|
24
|
+
"react": "^18.2.0",
|
25
|
+
"react-dom": "^18.2.0",
|
26
|
+
"react-scripts": "^5.0.1",
|
27
|
+
"ts-jest": "^28.0.5"
|
28
|
+
},
|
29
|
+
"devDependencies": {
|
30
|
+
"@types/react": "^18.0.14"
|
18
31
|
}
|
19
32
|
}
|
@@ -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
|
-
}
|