-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotReload.js
More file actions
44 lines (33 loc) · 860 Bytes
/
Copy pathhotReload.js
File metadata and controls
44 lines (33 loc) · 860 Bytes
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
const SandHotReload = (() => {
let lastHash = null;
let root = null;
let app = null;
function hash(str) {
let h = 0;
for (let i = 0; i < str.length; i++) {
h = (h << 5) - h + str.charCodeAt(i);
h |= 0;
}
return h;
}
function start(_app, _root, interval = 500) {
app = _app;
root = _root;
setInterval(() => {
const code = document.querySelector("script[data-app]")?.innerHTML;
if (!code) return;
const currentHash = hash(code);
if (lastHash === null) {
lastHash = currentHash;
}
if (currentHash !== lastHash) {
console.log("🔥 SandJS Hot Reload triggered");
lastHash = currentHash;
root.innerHTML = "";
Sand.render(app, root);
}
}, interval);
}
return { start };
})();
window.SandHotReload = SandHotReload;