@@ -7,41 +7,59 @@ class Reloader extends React.Component {
7
7
constructor ( props ) {
8
8
super ( props ) ;
9
9
if ( props . config . hot_reload ) {
10
- const { hash , interval } = props . config . hot_reload ;
10
+ const { interval } = props . config . hot_reload ;
11
11
this . state = {
12
- hash : hash ,
13
- interval
12
+ hash : null ,
13
+ interval,
14
+ reloading : false ,
15
+ disabled : false
14
16
}
15
17
} else {
16
18
this . state = {
17
19
disabled : true
18
20
}
19
21
}
22
+ this . _intervalId = null ;
20
23
}
21
24
22
25
componentDidUpdate ( ) {
23
- const { reloadHash } = this . props ;
26
+ const { reloadHash, dispatch } = this . props ;
24
27
if ( reloadHash . status === 200 ) {
25
- if ( reloadHash . content . reloadHash !== this . state . hash ) {
26
- // TODO add soft & hard reload option
27
- // soft -> rebuild the app layout (python reloaded)
28
- // hard -> reload the window (css/js reloaded)
29
- // eslint-disable-next-line no-undef
30
- window . top . location . reload ( ) ;
28
+ if ( this . state . hash === null ) {
29
+ this . setState ( { hash : reloadHash . content . reloadHash } ) ;
30
+ return ;
31
+ }
32
+ if ( reloadHash . content . reloadHash !== this . state . hash && ! this . state . reloading ) {
33
+ window . clearInterval ( this . _intervalId ) ;
34
+ if ( reloadHash . content . hard ) {
35
+ // Assets file have changed, need to reload them.
36
+ window . top . location . reload ( ) ;
37
+ } else if ( ! this . state . reloading ) {
38
+ // Py file has changed, just rebuild the reducers.
39
+ dispatch ( { 'type' : 'RELOAD' } ) ;
40
+ }
31
41
}
32
42
}
33
43
}
34
44
35
45
componentDidMount ( ) {
36
46
const { dispatch } = this . props ;
37
47
const { disabled, interval } = this . state ;
38
- if ( ! disabled ) {
39
- setInterval ( ( ) => {
40
- dispatch ( getReloadHash ( ) )
48
+ if ( ! disabled && ! this . _intervalId ) {
49
+ this . _intervalId = setInterval ( ( ) => {
50
+ if ( ! this . state . reloading ) {
51
+ dispatch ( getReloadHash ( ) ) ;
52
+ }
41
53
} , interval ) ;
42
54
}
43
55
}
44
56
57
+ componentWillUnmount ( ) {
58
+ if ( ! this . state . disabled ) {
59
+ window . clearInterval ( this . _intervalId ) ;
60
+ }
61
+ }
62
+
45
63
render ( ) {
46
64
return null ;
47
65
}
@@ -53,7 +71,8 @@ Reloader.propTypes = {
53
71
id : PropTypes . string ,
54
72
config : PropTypes . object ,
55
73
reloadHash : PropTypes . object ,
56
- dispatch : PropTypes . func
74
+ dispatch : PropTypes . func ,
75
+ interval : PropTypes . number
57
76
} ;
58
77
59
78
export default connect (
0 commit comments