1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/static/js/poll.js
catborise 7359fbb147 fix(accounts): a page whose session ended says so; a form sent then is not lost in a 405
A session that ended while a page was open went unnoticed: the VM list,
the VM page and the job panel kept asking, were sent to the sign-in
page, and showed what they had as if it were current. A button pressed
then sent the user to sign in and, after it, to the form's own address
by GET, where its view answered an empty 405 and the action was lost.

The panel's LoginRequiredMiddleware (webvirtcloud.middleware) answers a
page's script 401, upon which session_end.js says the user was signed
out, with a link to sign in and come back, and the page stops asking
and reloading. A form sent after the end (or with a token a sign-in
elsewhere replaced) goes back, after the sign-in, to the page it was on,
and the sign-in page says it was not done. accounts.E002 asks for this
middleware; Django's own is a warning (W002), so an update keeps a
panel whose local settings name it running.
2026-11-21 23:48:34 +03:00

63 lines
2.5 KiB
JavaScript

// Repeats request() while the page is visible and passes each answer to
// onData. request() returns a jQuery promise (e.g. $.getJSON(url)); onData is
// called after the poller's own bookkeeping, so an error in it does not stop
// the polling. One request at a time: the next one starts
// interval ms after the previous one has finished, so a slow host does not
// pile up requests, each holding a web worker. A hidden page asks nothing;
// when it is shown again it asks at once. stop() pauses, start() resumes
// at once (for example while a tab of the page is shown). The first request
// goes out after firstDelay ms, the interval if it is not given.
function pollWhileVisible(interval, request, onData, firstDelay) {
var timer = null;
var running = false;
var stopped = false;
function run() {
timer = null;
if (stopped || document.hidden || window.sessionEnded) {
return; // a page whose session ended asks nothing more (session_end.js)
}
running = true;
request()
.always(function () {
running = false;
if (!stopped && !document.hidden) {
timer = setTimeout(run, interval);
}
})
.done(onData);
}
function resume() {
if (!stopped && !document.hidden && !running && timer === null) {
run();
}
}
document.addEventListener("visibilitychange", resume);
timer = setTimeout(run, firstDelay === undefined ? interval : firstDelay);
return {
stop: function () {
stopped = true;
clearTimeout(timer);
timer = null;
},
start: function () {
stopped = false;
resume();
},
};
}
// The user is in the page: a dialog open, a field in use, an XML edit not
// saved or VMs chosen on the VM list. A poll that would reload the page waits for its next answer.
function pageInUse() {
var active = document.activeElement;
return !!document.querySelector(".modal.show")
|| !!(active && active.matches && active.matches("input, select, textarea"))
|| !!(window.xmlEditUnsaved && window.xmlEditUnsaved())
|| (typeof bulkPowerChosen === "function" && bulkPowerChosen()) // the VM list's bulk form in use (bulk_power.js)
|| !!document.querySelector("button[data-busy]") // a form being sent (submit_busy.js)
|| !!window.sessionEnded; // signed out: the page keeps its notice (session_end.js)
}