diff options
author | Sebastian <sebasjm@gmail.com> | 2022-12-06 09:21:17 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-12-06 09:21:17 -0300 |
commit | e382b022030db96b8282337b304ec5e599a5f405 (patch) | |
tree | 6ba9afb72a1b3e5ce35f37a987d74286b0e45c92 /packages/web-util/src/live-reload.ts | |
parent | 219e48f35196e54361dc17d6ca7de6e095c46eeb (diff) | |
download | wallet-core-e382b022030db96b8282337b304ec5e599a5f405.tar.xz |
web-util: utils for developing webapps
Diffstat (limited to 'packages/web-util/src/live-reload.ts')
-rw-r--r-- | packages/web-util/src/live-reload.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/packages/web-util/src/live-reload.ts b/packages/web-util/src/live-reload.ts new file mode 100644 index 000000000..bae0a5b84 --- /dev/null +++ b/packages/web-util/src/live-reload.ts @@ -0,0 +1,52 @@ +/* eslint-disable no-undef */ + +function setupLiveReload(): void { + const ws = new WebSocket("wss://localhost:8080/ws"); + + ws.addEventListener("message", (message) => { + try { + const event = JSON.parse(message.data); + if (event.type === "file-updated-start") { + showReloadOverlay(); + return; + } + if (event.type === "file-updated-done") { + window.location.reload(); + return; + } + } catch (e) { + return + } + console.log("unsupported", event); + }); + + ws.addEventListener("error", (error) => { + console.error(error); + }); + ws.addEventListener("close", (message) => { + setTimeout(setupLiveReload, 1500); + }); +} +setupLiveReload(); + + +function showReloadOverlay(): void { + const d = document.createElement("div"); + d.style.position = "absolute"; + d.style.width = "100%"; + d.style.height = "100%"; + d.style.color = "white"; + d.style.backgroundColor = "rgba(0,0,0,0.5)"; + d.style.display = "flex"; + d.style.justifyContent = "center"; + const h = document.createElement("h1"); + h.style.margin = "auto"; + h.innerHTML = "reloading..."; + d.appendChild(h); + if (document.body.firstChild) { + document.body.insertBefore(d, document.body.firstChild); + } else { + document.body.appendChild(d); + } +} + |