Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 1a78f29

Browse files
committed
Add soft reload capability.
1 parent 287df08 commit 1a78f29

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

Diff for: src/components/core/Reloader.react.js

+33-14
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,59 @@ class Reloader extends React.Component {
77
constructor(props) {
88
super(props);
99
if (props.config.hot_reload) {
10-
const { hash, interval } = props.config.hot_reload;
10+
const { interval } = props.config.hot_reload;
1111
this.state = {
12-
hash: hash,
13-
interval
12+
hash: null,
13+
interval,
14+
reloading: false,
15+
disabled: false
1416
}
1517
} else {
1618
this.state = {
1719
disabled: true
1820
}
1921
}
22+
this._intervalId = null;
2023
}
2124

2225
componentDidUpdate() {
23-
const { reloadHash } = this.props;
26+
const {reloadHash, dispatch} = this.props;
2427
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+
}
3141
}
3242
}
3343
}
3444

3545
componentDidMount() {
3646
const { dispatch } = this.props;
3747
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+
}
4153
}, interval);
4254
}
4355
}
4456

57+
componentWillUnmount() {
58+
if (!this.state.disabled) {
59+
window.clearInterval(this._intervalId);
60+
}
61+
}
62+
4563
render() {
4664
return null;
4765
}
@@ -53,7 +71,8 @@ Reloader.propTypes = {
5371
id: PropTypes.string,
5472
config: PropTypes.object,
5573
reloadHash: PropTypes.object,
56-
dispatch: PropTypes.func
74+
dispatch: PropTypes.func,
75+
interval: PropTypes.number
5776
};
5877

5978
export default connect(

Diff for: src/reducers/reducer.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ function recordHistory(reducer) {
9090
}
9191
}
9292

93-
export default recordHistory(reducer);
93+
function rootReducer(reducer) {
94+
return function(state, action) {
95+
if (action.type === 'RELOAD') {
96+
const {history} = state;
97+
state = {history};
98+
action = null;
99+
}
100+
return reducer(state, action);
101+
}
102+
}
103+
104+
export default rootReducer(recordHistory(reducer));

0 commit comments

Comments
 (0)