react-app-store-manager 0.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.
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
4
+ <serverData>
5
+ <paths name="server">
6
+ <serverdata>
7
+ <mappings>
8
+ <mapping local="$PROJECT_DIR$" web="/" />
9
+ </mappings>
10
+ </serverdata>
11
+ </paths>
12
+ </serverData>
13
+ </component>
14
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/react-app-store-manager.iml" filepath="$PROJECT_DIR$/.idea/react-app-store-manager.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
@@ -0,0 +1,76 @@
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/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "react-app-store-manager",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "compilled/StoreManager.tsx",
6
+ "scripts": {
7
+ "build": "babel src --out-dir compilled --copy-files"
8
+ },
9
+ "author": "",
10
+ "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
+ "dependencies": {
17
+ "react": "^18.2.0",
18
+ "react-app-store-manager": "^1.0.0"
19
+ }
20
+ }
@@ -0,0 +1,76 @@
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
+ }