-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathReloader.react.js
214 lines (197 loc) · 6.77 KB
/
Reloader.react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {
comparator,
equals,
forEach,
has,
isEmpty,
lt,
path,
pathOr,
sort,
} from 'ramda';
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import apiThunk from '../../actions/api';
class Reloader extends React.Component {
constructor(props) {
super(props);
if (props.config.hot_reload) {
const {interval, max_retry} = props.config.hot_reload;
this.state = {
interval,
disabled: false,
intervalId: null,
packages: null,
max_retry,
};
} else {
this.state = {
disabled: true,
};
}
this._retry = 0;
this._head = document.querySelector('head');
this.clearInterval = this.clearInterval.bind(this);
}
clearInterval() {
window.clearInterval(this.state.intervalId);
this.setState({intervalId: null});
}
static getDerivedStateFromProps(props) {
/*
* Save the non-loading requests in the state in order to compare
* current hashes with previous hashes.
* Note that if there wasn't a "loading" state for the requests,
* then we could simply compare `props` with `prevProps` in
* `componentDidUpdate`.
*/
if (
!isEmpty(props.reloadRequest) &&
props.reloadRequest.status !== 'loading'
) {
return {reloadRequest: props.reloadRequest};
}
return null;
}
componentDidUpdate(prevProps, prevState) {
const {reloadRequest} = this.state;
const {dispatch} = this.props;
// In the beginning, reloadRequest won't be defined
if (!reloadRequest) {
return;
}
/*
* When reloadRequest is first defined, prevState won't be defined
* for one render loop.
* The first reloadRequest defines the initial/baseline hash -
* it doesn't require a reload
*/
if (!has('reloadRequest', prevState)) {
return;
}
if (
reloadRequest.status === 200 &&
path(['content', 'reloadHash'], reloadRequest) !==
path(['reloadRequest', 'content', 'reloadHash'], prevState)
) {
// Check for CSS (!content.hard) or new package assets
if (
reloadRequest.content.hard ||
!equals(
reloadRequest.content.packages.length,
pathOr(
[],
['reloadRequest', 'content', 'packages'],
prevState
).length
) ||
!equals(
sort(comparator(lt), reloadRequest.content.packages),
sort(
comparator(lt),
pathOr(
[],
['reloadRequest', 'content', 'packages'],
prevState
)
)
)
) {
// Look if it was a css file.
let was_css = false;
// eslint-disable-next-line prefer-const
for (let a of reloadRequest.content.files) {
if (a.is_css) {
was_css = true;
const nodesToDisable = [];
// Search for the old file by xpath.
const it = document.evaluate(
`//link[contains(@href, "${a.url}")]`,
this._head
);
let node = it.iterateNext();
while (node) {
nodesToDisable.push(node);
node = it.iterateNext();
}
forEach(
n => n.setAttribute('disabled', 'disabled'),
nodesToDisable
);
if (a.modified > 0) {
const link = document.createElement('link');
link.href = `${a.url}?m=${a.modified}`;
link.type = 'text/css';
link.rel = 'stylesheet';
this._head.appendChild(link);
// Else the file was deleted.
}
} else {
// If there's another kind of file here do a hard reload.
was_css = false;
break;
}
}
if (!was_css) {
// Assets file have changed
// or a component lib has been added/removed -
// Must do a hard reload
window.location.reload();
}
} else {
// Backend code changed - can do a soft reload in place
dispatch({type: 'RELOAD'});
}
} else if (reloadRequest.status === 500) {
if (this._retry > this.state.max_retry) {
this.clearInterval();
// Integrate with dev tools ui?!
window.alert(
`
Reloader failed after ${this._retry} times.
Please check your application for errors.
`
);
}
this._retry++;
}
}
componentDidMount() {
const {dispatch, reloadRequest} = this.props;
const {disabled, interval} = this.state;
if (!disabled && !this.state.intervalId) {
const intervalId = window.setInterval(() => {
// Prevent requests from piling up - reloading can take
// many seconds (10-30) and the interval is 3s by default
if (reloadRequest.status !== 'loading') {
dispatch(apiThunk('_reload-hash', 'GET', 'reloadRequest'));
}
}, interval);
this.setState({intervalId});
}
}
componentWillUnmount() {
if (!this.state.disabled && this.state.intervalId) {
this.clearInterval();
}
}
render() {
return null;
}
}
Reloader.defaultProps = {};
Reloader.propTypes = {
id: PropTypes.string,
config: PropTypes.object,
reloadRequest: PropTypes.object,
dispatch: PropTypes.func,
interval: PropTypes.number,
};
export default connect(
state => ({
config: state.config,
reloadRequest: state.reloadRequest,
}),
dispatch => ({dispatch})
)(Reloader);