1let socket = null;
2let timeout = null;
3
4function connect() {
5 socket = new WebSocket(`ws://${window.location.host}/lustre-dev-tools`);
6
7 if (timeout) {
8 clearTimeout(timeout);
9 timeout = null;
10 }
11
12 socket.onmessage = (event) => {
13 if (event.data === "reload") {
14 window.location.reload();
15 }
16 };
17
18 // If the dev server goes down we'll continue to try to reconnect
19 // every 5 seconds. If the user needs to kill the server for some
20 // reason, this means the page will restore live reload without a
21 // refresh.
22 socket.onclose = () => {
23 socket = null;
24
25 if (timeout) clearTimeout(timeout);
26 if (!socket) timeout = setTimeout(() => connect(), 5000);
27 };
28
29 socket.onerror = () => {
30 socket = null;
31
32 if (timeout) clearTimeout(timeout);
33 if (!socket) timeout = setTimeout(() => connect(), 5000);
34 };
35}
36
37connect();