generated from ZEISS/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreload.js
More file actions
46 lines (46 loc) · 1.11 KB
/
reload.js
File metadata and controls
46 lines (46 loc) · 1.11 KB
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
(() => {
// reload.ts
var lastUuid = "";
var timeout;
var resetBackoff = () => {
timeout = 1e3;
};
var backOff = () => {
if (timeout > 10 * 1e3) {
return;
}
timeout = timeout * 2;
};
var hotReloadUrl = () => {
const hostAndPort = location.hostname + (location.port ? ":" + location.port : "");
if (location.protocol === "https:") {
return "wss://" + hostAndPort + "/ws/reload";
}
return "ws://" + hostAndPort + "/ws/reload";
};
function connectHotReload() {
const socket = new WebSocket(hotReloadUrl());
socket.onmessage = (event) => {
if (lastUuid === "") {
lastUuid = event.data;
}
if (lastUuid !== event.data) {
console.log("[Hot Reloader] Server Changed, reloading");
location.reload();
}
};
socket.onopen = () => {
resetBackoff();
socket.send("Hello");
};
socket.onclose = () => {
const timeoutId = setTimeout(function() {
clearTimeout(timeoutId);
backOff();
connectHotReload();
}, timeout);
};
}
resetBackoff();
connectHotReload();
})();