clean: use window.sleep in place of setTimeout
Docker Image CI / build-and-push-image (push) Has been cancelled
Maintain Release Merge PR / update-release-pr (push) Has been cancelled
release-please / release-please (push) Has been cancelled
test / test (20.x) (push) Has been cancelled
test / test (22.x) (push) Has been cancelled
test / backend (node env, api-test) (22.x) (push) Has been cancelled
test / puterjs (browser env, playwright) (22.x) (push) Has been cancelled
test / puterjs (node env, vitest) (22.x) (push) Has been cancelled

Also another change to make duration handling consistent for the two
blocks of code that have the "ensure 2 seconds" logic.
This commit is contained in:
KernelDeimos
2025-10-30 19:11:09 -04:00
committed by Eric Dubé
parent e37e009305
commit 08e9ab399a
+12 -17
View File
@@ -990,21 +990,15 @@ window.initgui = async function(options){
contentType: "application/json",
data: JSON.stringify(requestData),
success: async function (data){
// 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 (timeRemaining > 0) {
/*eslint-disable*/
const turnstile_duration = Date.now() - window.turnstile_success_ts;
if (turnstile_duration < 2000) {
// Sleep until 2 seconds have passed
await new Promise(rslv => setTimeout(rslv, timeRemaining));
await window.sleep(2000 - turnstile_duration);
}
/*eslint-disable*/
const $captchaModal = $('.captcha-modal');
if ( $captchaModal.length > 0 ) await new Promise(resolve => {
if ( $captchaModal.length > 0 ) await new Promise(async 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
@@ -1015,16 +1009,17 @@ window.initgui = async function(options){
});
// Just in case anything fails, also resolve after 500ms
setTimeout(() => resolve(), 500);
await window.sleep(500);
resolve();
});
// 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 => {
if(window.embedded_in_popup) await new Promise(async resolve => {
let spinner_duration = (Date.now() - spinner_init_ts);
setTimeout(() => {
puter.ui.hideSpinner();
resolve();
}, spinner_duration > 2000 ? 10 : 2000 - spinner_duration);
if (spinner_duration < 2000) {
await window.sleep(2000 - spinner_duration);
}
puter.ui.hideSpinner();
});
/*eslint-enable*/