Web: reload window 60 minutes after user activity

Reload the webapp 60 minutes after keyboard/mouse activity.

Previously this was a hard 60 minutes after the webapp was loaded, which
means that it could happen while someone was actively working in the web
interface. This should now be prevented.
This commit is contained in:
Sybren A. Stüvel 2022-07-07 16:08:07 +02:00
parent f8c49981f6
commit 7bceaf5b10
2 changed files with 40 additions and 5 deletions

View File

@ -0,0 +1,36 @@
import { DateTime } from "luxon";
// Do a full refresh once per hour. This is just to make sure that long-lived
// displays (like the TV in the hallway at Blender HQ) pick up on HTML/JS/CSS
// changes eventually.
const reloadAfter = {minute: 60};
function getReloadDeadline() {
return DateTime.now().plus(reloadAfter);
}
let reloadAt = getReloadDeadline();
// Every activity (mouse move, keyboard, etc.) defers the reload.
function deferReload() {
reloadAt = getReloadDeadline();
}
function maybeReload() {
const now = DateTime.now();
if (now < reloadAt) return;
window.location.reload();
}
export default function autoreload() {
// Check whether reloading is needed every minute.
window.setInterval(maybeReload, 60 * 1000);
window.addEventListener("resize", deferReload);
window.addEventListener("mousedown", deferReload);
window.addEventListener("mouseup", deferReload);
window.addEventListener("mousemove", deferReload);
window.addEventListener("keydown", deferReload);
window.addEventListener("keyup", deferReload);
}

View File

@ -14,11 +14,6 @@ window.plain = (x) => JSON.parse(JSON.stringify(x));
// objectEmpty returns whether the object is empty or not. // objectEmpty returns whether the object is empty or not.
window.objectEmpty = (o) => !o || Object.entries(o).length == 0; window.objectEmpty = (o) => !o || Object.entries(o).length == 0;
// Do a full refresh once per hour. This is just to make sure that long-lived
// displays (like the TV in the hallway at Blender HQ) pick up on HTML/JS/CSS
// changes eventually.
window.setTimeout(() => {window.location.reload(); }, 3600 * 1000);
const app = createApp(App) const app = createApp(App)
const pinia = createPinia() const pinia = createPinia()
@ -35,3 +30,7 @@ window.jobs = useJobs();
window.notifs = useNotifs(); window.notifs = useNotifs();
window.taskLog = useTaskLog(); window.taskLog = useTaskLog();
window.API = API; window.API = API;
// Automatically reload the window after a period of inactivity from the user.
import autoreload from '@/autoreloader'
autoreload();