From e37e009305c65fdbb7ca855577d517c8995be4d5 Mon Sep 17 00:00:00 2001 From: KernelDeimos <7225168+KernelDeimos@users.noreply.github.com> Date: Thu, 30 Oct 2025 18:56:17 -0400 Subject: [PATCH] refactor: reduce indentation and arithmetic This commit reduces the indentation level and complex arithmetic inside the createTempUser function in initgui. It is suspected that this change should greatly reduce measures of the "cognitive complexity" metric. --- src/gui/src/initgui.js | 70 ++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index 28f3f1d1b..343d70da7 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -990,42 +990,46 @@ window.initgui = async function(options){ contentType: "application/json", data: JSON.stringify(requestData), success: async function (data){ - setTimeout(async () => { - const $captchaModal = $('.captcha-modal'); - if ( $captchaModal.length > 0 ) { - await new Promise(resolve => { - // The callback operand for fadeOut could be called - // more than once if there are multiple `.captcha-modal` - // elements, but only the first call to `resolve()` will - // have any effect. - $captchaModal.fadeOut(200, function () { - $(this).remove(); - resolve(); - }); - - // Just in case anything fails, also resolve after 500ms - setTimeout(() => { - resolve(); - }, 500); - }); - } + // We want to show the spinner for at least 2 seconds so it + // doesn't look like a flicker. + let timeRemaining = 2000; + // Subtract time taken for turnstile to succeed + timeRemaining -= Date.now() - window.turnstile_success_ts; - // if this is a popup, hide the spinner, make sure it was visible for at least 2 seconds - if(window.embedded_in_popup){ - let spinner_duration = (Date.now() - spinner_init_ts); - setTimeout(() => { - window.update_auth_data(data.token, data.user); - document.dispatchEvent(new Event("login", { bubbles: true})); - puter.ui.hideSpinner(); - }, spinner_duration > 2000 ? 10 : 2000 - spinner_duration); + if (timeRemaining > 0) { + // Sleep until 2 seconds have passed + await new Promise(rslv => setTimeout(rslv, timeRemaining)); + } - return; - }else{ - window.update_auth_data(data.token, data.user); - document.dispatchEvent(new Event("login", { bubbles: true})); - } - }, (Date.now() - window.turnstile_success_ts) > 2000 ? 10 : 2000 - (Date.now() - window.turnstile_success_ts)); + /*eslint-disable*/ + const $captchaModal = $('.captcha-modal'); + if ( $captchaModal.length > 0 ) await new Promise(resolve => { + // The callback operand for fadeOut could be called + // more than once if there are multiple `.captcha-modal` + // elements, but only the first call to `resolve()` will + // have any effect. + $captchaModal.fadeOut(200, function () { + $(this).remove(); + resolve(); + }); + + // Just in case anything fails, also resolve after 500ms + setTimeout(() => resolve(), 500); + }); + + // if this is a popup, hide the spinner, make sure it was visible for at least 2 seconds + if(window.embedded_in_popup) await new Promise(resolve => { + let spinner_duration = (Date.now() - spinner_init_ts); + setTimeout(() => { + puter.ui.hideSpinner(); + resolve(); + }, spinner_duration > 2000 ? 10 : 2000 - spinner_duration); + }); + /*eslint-enable*/ + + window.update_auth_data(data.token, data.user); + document.dispatchEvent(new Event("login", { bubbles: true })); }, error: async (err) => { UIAlert({