This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathReloader.react.js
161 lines (149 loc) · 5.53 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
/* eslint-disable no-undef,react/no-did-update-set-state,no-magic-numbers */
import R from 'ramda';
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {getReloadHash} 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 = {
hash: null,
interval,
disabled: false,
intervalId: null,
packages: null,
max_retry
};
} else {
this.state = {
disabled: true,
};
}
this._retry = 0;
this._head = document.querySelector('head');
}
componentDidUpdate() {
const {reloadRequest, dispatch} = this.props;
if (reloadRequest.status === 200) {
if (this.state.hash === null) {
this.setState({
hash: reloadRequest.content.reloadHash,
packages: reloadRequest.content.packages,
});
return;
}
if (reloadRequest.content.reloadHash !== this.state.hash) {
if (
reloadRequest.content.hard ||
reloadRequest.content.packages.length !==
this.state.packages.length ||
!R.all(
R.map(
x => R.contains(x, this.state.packages),
reloadRequest.content.packages
)
)
) {
// 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();
}
R.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
window.top.location.reload();
} else {
// Since it's only a css reload,
// we just change the hash.
this.setState({
hash: reloadRequest.content.reloadHash,
});
}
} else {
// Soft reload
window.clearInterval(this.state.intervalId);
dispatch({type: 'RELOAD'});
}
}
} else if (reloadRequest.status === 500) {
if (this._retry > this.state.max_retry) {
window.clearInterval(this.state.intervalId);
// Integrate with dev tools ui?!
window.alert(
`
Reloader failed after ${this._retry} times.
Please check your application for errors.
`
);
}
this._retry++;
}
}
componentDidMount() {
const {dispatch} = this.props;
const {disabled, interval} = this.state;
if (!disabled && !this.state.intervalId) {
const intervalId = setInterval(() => {
dispatch(getReloadHash());
}, interval);
this.setState({intervalId});
}
}
componentWillUnmount() {
if (!this.state.disabled && this.state.intervalId) {
window.clearInterval(this.state.intervalId);
}
}
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);