11import "@babel/polyfill" ;
22
3- import React , { useState , useEffect , ErrorInfo } from "react" ;
3+ import React , { useState , useEffect , ErrorInfo , useMemo , useCallback } from "react" ;
44import ReactDOM from "react-dom" ;
5+ import { MD5 } from "object-hash" ;
56import { GroupStoreMutable } from "./stores/group-store" ;
67import { GroupContext , GroupContextObject } from "./contexts/group-context" ;
7- import { useForceUpdate } from "./force-update" ;
8+ // import { useForceUpdate } from "./force-update";
9+ import { Test1 } from "./tests/test1" ;
810
911import "./reset.scss" ;
1012import "./app.scss" ;
11- import { Test1 } from "./tests/test1" ;
1213
1314const store = new GroupStoreMutable ( ) ;
15+
16+ // tslint:disable-next-line:no-any
17+ ( window as any ) . store = store ;
18+
19+ function useStoreHash ( ) : [ TestState , ( ) => void ] {
20+ const [ state , setState ] = useState < TestState > ( {
21+ storeObject : undefined ,
22+ store : { } ,
23+ hash : ""
24+ } ) ;
25+
26+ const updateStoreHash = useCallback ( ( ) => {
27+ const storeObject = store . toObject ( ) ;
28+ const hash = MD5 ( {
29+ storeHash : MD5 ( store ) ,
30+ storeObjectHash : MD5 ( storeObject )
31+ } ) ;
32+ if ( state . hash === hash ) {
33+ return ;
34+ }
35+ setState ( {
36+ storeObject : storeObject ,
37+ store : store ,
38+ hash : hash
39+ } ) ;
40+ } , [ store ] ) ;
41+
42+ return [ state , updateStoreHash ] ;
43+ }
44+
45+ interface TestState {
46+ storeObject ?: unknown ;
47+ store : unknown ;
48+ hash : string ;
49+ }
50+
1451const Test = ( ) => {
15- const forceUpdate = useForceUpdate ( ) ;
52+ const [ state , updateStoreHash ] = useStoreHash ( ) ;
1653 useEffect ( ( ) => {
1754 const listener = ( ) => {
18- forceUpdate ( ) ;
55+ // console.log("State updated");
56+ updateStoreHash ( ) ;
1957 } ;
20- forceUpdate ( ) ;
58+ updateStoreHash ( ) ;
2159 store . addListener ( listener ) ;
2260 return ( ) => store . removeListener ( listener ) ;
2361 } , [ ] ) ;
2462
63+ // const [groupId] = useState(undefined);
64+ const [ groupId ] = useState ( "person" ) ;
65+
2566 const groupContext : GroupContextObject = {
2667 store : store ,
2768 test : "app" ,
28- groupId : "person"
69+ groupId : groupId
2970 } ;
3071
3172 return (
@@ -38,7 +79,7 @@ const Test = () => {
3879 < div className = "store" >
3980 < pre >
4081 { JSON . stringify (
41- store ,
82+ state . store ,
4283 // tslint:disable-next-line:no-any
4384 ( _ , value : any ) => {
4485 if ( typeof value !== "function" ) {
@@ -51,9 +92,7 @@ const Test = () => {
5192 ) }
5293 </ pre >
5394 </ div >
54- < pre className = "store-result" >
55- { JSON . stringify ( store . toObject ( ) , null , 4 ) }
56- </ pre >
95+ < pre className = "store-result" > { JSON . stringify ( state . storeObject , null , 4 ) } </ pre >
5796 </ >
5897 ) ;
5998} ;
@@ -77,18 +116,18 @@ class App extends React.Component {
77116 const { error } = this . state ;
78117 if ( error != null ) {
79118 const info = this . state . info ! ;
80- console . warn ( error . message ) ;
81- console . log ( info ) ;
82119 return (
83120 < div className = "error" >
84- < p className = "disclaimer" >
85- Oops! An error has occurred! Here’s what we know…
86- </ p >
87- < p className = "message" > { error . message } </ p >
88- < p className = "component-stack" >
121+ < p className = "disclaimer" > Oops! An error has occurred! Here’s what we know…</ p >
122+ < pre className = "message" > { error . message } </ pre >
123+ < div className = "component-stack" >
89124 Component stack:
90125 < pre > { info . componentStack . substr ( 1 ) } </ pre >
91- </ p >
126+ </ div >
127+
128+ < div >
129+ < button onClick = { ( ) => location . reload ( ) } > Reload page</ button >
130+ </ div >
92131 </ div >
93132 ) ;
94133 }
0 commit comments