Skip to content

Commit

Permalink
Merge pull request #22 from leotaku/rework-frontend
Browse files Browse the repository at this point in the history
Improve client side code somewhat
  • Loading branch information
leotaku authored Jan 6, 2025
2 parents 0a3b2d8 + 4ce6822 commit 98ac675
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
29 changes: 0 additions & 29 deletions assets/polling.html

This file was deleted.

33 changes: 33 additions & 0 deletions assets/polling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(() => {
const inputs = document.currentScript.dataset;

const retry = (url) => {
const controller = new AbortController();
setTimeout(() => controller.abort(), 500);
fetch(url, { cache: "no-store", signal: controller.signal })
.then((resp) => {
if (!resp.ok) return Promise.reject();
console.log("[tower-livereload] reload...");
window.location.reload();
})
.catch(() => setTimeout(() => retry(url), inputs.reloadInterval));
};

addEventListener("pageshow", () => {
const controller = new AbortController();
var unloaded = false;
addEventListener("beforeunload", () => {
unloaded = true;
controller.abort();
});

console.log("[tower-livereload] connected...");
fetch(inputs.longPoll, { cache: "no-store", signal: controller.signal })
.then((rsp) => rsp.text())
.catch(() => null)
.then(() => {
console.log("[tower-livereload] disconnected...");
if (!unloaded) retry(inputs.backUp);
});
});
})();
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ impl<S, ReqPred, ResPred> LiveReload<S, ReqPred, ResPred> {
let inject = InjectService::new(
service,
format!(
include_str!("../assets/polling.html"),
r#"<script data-long-poll="{long_poll}" data-back-up="{back_up}" data-reload-interval="{reload_interval}">{code}</script>"#,
code = include_str!("../assets/polling.js"),
long_poll = long_poll_path,
back_up = back_up_path,
reload_interval = reload_interval.as_millis(),
Expand Down

0 comments on commit 98ac675

Please sign in to comment.