checks tab focus before doing XHR background stuff, replaces connectivity check endpoint for a lighter response

This commit is contained in:
tim h 2023-01-07 14:06:37 -06:00
parent dbcd588e29
commit 548eefe769

View file

@ -143,6 +143,7 @@ var stableDiffusionData = {
var host = ""; var host = "";
var url = "/sdapi/v1/"; var url = "/sdapi/v1/";
const basePixelCount = 64; //64 px - ALWAYS 64 PX const basePixelCount = 64; //64 px - ALWAYS 64 PX
var focused = true;
function startup() { function startup() {
testHostConfiguration(); testHostConfiguration();
@ -168,6 +169,7 @@ function startup() {
changeHiResSquare(); changeHiResSquare();
changeRestoreFaces(); changeRestoreFaces();
changeSyncCursorSize(); changeSyncCursorSize();
checkFocus();
} }
function setFixedHost(h, changePromptMessage) { function setFixedHost(h, changePromptMessage) {
@ -335,7 +337,23 @@ async function testHostConnection() {
let checkInProgress = false; let checkInProgress = false;
const checkConnection = async (notify = false) => { const checkConnection = async (
notify = false,
simpleProgressStatus = false
) => {
const apiIssueResult = () => {
setConnectionStatus("apiissue");
const message = `The host is online, but the API seems to be disabled.\nHave you run the webui with the flag '--api', or is the flag '--gradio-debug' currently active?`;
console.error(message);
if (notify) alert(message);
};
const offlineResult = () => {
setConnectionStatus("offline");
const message = `The connection with the host returned an error: ${response.status} - ${response.statusText}`;
console.error(message);
if (notify) alert(message);
};
if (checkInProgress) if (checkInProgress)
throw new CheckInProgressError( throw new CheckInProgressError(
"Check is currently in progress, please try again" "Check is currently in progress, please try again"
@ -344,6 +362,25 @@ async function testHostConnection() {
var url = document.getElementById("host").value + "/startup-events"; var url = document.getElementById("host").value + "/startup-events";
// Attempt normal request // Attempt normal request
try { try {
if (simpleProgressStatus) {
const response = await fetch(
document.getElementById("host").value + "/sdapi/v1/progress" // seems to be the "lightest" endpoint?
);
const responseData = await response.json();
switch (response.status) {
case 200: {
setConnectionStatus("online");
break;
}
case 404: {
apiIssueResult();
break;
}
default: {
offlineResult();
}
}
} else {
// Check if API is available // Check if API is available
const response = await fetch( const response = await fetch(
document.getElementById("host").value + "/sdapi/v1/options" document.getElementById("host").value + "/sdapi/v1/options"
@ -377,17 +414,12 @@ async function testHostConnection() {
break; break;
} }
case 404: { case 404: {
setConnectionStatus("apiissue"); apiIssueResult();
const message = `The host is online, but the API seems to be disabled.\nHave you run the webui with the flag '--api', or is the flag '--gradio-debug' currently active?`;
console.error(message);
if (notify) alert(message);
break; break;
} }
default: { default: {
setConnectionStatus("offline"); offlineResult();
const message = `The connection with the host returned an error: ${response.status} - ${response.statusText}`; }
console.error(message);
if (notify) alert(message);
} }
} }
} catch (e) { } catch (e) {
@ -413,7 +445,9 @@ async function testHostConnection() {
return status; return status;
}; };
if (focused) {
await checkConnection(!urlParams.has("noprompt")); await checkConnection(!urlParams.has("noprompt"));
}
// On click, attempt to refresh // On click, attempt to refresh
connectionIndicator.onclick = async () => { connectionIndicator.onclick = async () => {
@ -425,18 +459,32 @@ async function testHostConnection() {
} }
}; };
// Checks every 5 seconds if offline, 30 seconds if online // Checks every 5 seconds if offline, 60 seconds if online
const checkAgain = () => { const checkAgain = () => {
if (focused) {
setTimeout( setTimeout(
async () => { async () => {
await checkConnection(); let simple = !firstTimeOnline;
await checkConnection(false, simple);
checkFocus();
checkAgain(); checkAgain();
}, },
connectionStatus ? 30000 : 5000 connectionStatus ? 60000 : 5000
//connectionStatus ? 5000 : 5000 //TODO REMOVE DEBUG REPLACE TO 60000 : 5000
); );
} else {
setTimeout(() => {
console.debug("unfocused, zzz");
checkFocus();
checkAgain();
}, 60000);
//}, 5000); //TODO REMOVE DEBUG REPLACE TO 60000
}
}; };
if (focused) {
checkAgain(); checkAgain();
}
return () => { return () => {
checkConnection().catch(() => {}); checkConnection().catch(() => {});
@ -1242,3 +1290,16 @@ function resetToDefaults() {
localStorage.clear(); localStorage.clear();
} }
} }
document.addEventListener("visibilitychange", () => {
checkFocus();
});
function checkFocus() {
if (!document.hidden) {
focused = true;
} else {
focused = false;
}
console.debug("FOCUSED: " + focused); //TODO comment out or something
}