Изолировать callbacks отменённой загрузки (#9)

This commit is contained in:
Panfilov Konstantin committed 2026-10-20 18:33:12 +03:00
1 parent d70d396670
commit 5afa71ec84
1 file changed
+7 -4
+7 -4
View File
@@ -223,6 +223,7 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
xhr.withCredentials = true;
xhr.setRequestHeader('Accept', 'application/json');
xhr.onload = async () => {
if (attempt !== uploadAttempt || leavingUploadPage) return;
if (xhr.status >= 200 && xhr.status < 300) {
try { const result = JSON.parse(xhr.responseText); if (!result?.id) throw new Error(); await recovery.accepted({id: result.id}); redirectToTour(record); }
catch (_) { showUploadError(genericErrorMsg); }
@@ -230,9 +231,9 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
}
showUploadError(genericErrorMsg);
};
xhr.onerror = () => showUploadError(genericErrorMsg, true);
xhr.onerror = () => { if (attempt === uploadAttempt && !leavingUploadPage) showUploadError(genericErrorMsg, true); };
xhr.send(formData);
} catch (_) { showUploadError(genericErrorMsg); }
} catch (_) { if (attempt === uploadAttempt && !leavingUploadPage) showUploadError(genericErrorMsg); }
}
function uploadFile(file, saved = null) {
@@ -244,9 +245,10 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
const record = saved || await recovery.remember(new File([bytes], file.name, {type: file.type, lastModified: file.lastModified}), metadata());
if (attempt === uploadAttempt && !leavingUploadPage) uploadPreparedFile(file, bytes, record);
})
.catch(() => showUploadError(genericErrorMsg));
.catch(() => { if (attempt === uploadAttempt && !leavingUploadPage) showUploadError(genericErrorMsg); });
}
function uploadPreparedFile(file, bytes, record) {
const attempt = uploadAttempt;
const xhr = new XMLHttpRequest();
const formData = new FormData();
// Ключ загрузки: по нему сайт передаёт её в сессию посетителя (/api/images/public/claim, годен 30 мин).
@@ -268,6 +270,7 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
};
xhr.onload = async () => {
if (attempt !== uploadAttempt || leavingUploadPage) return;
if (xhr.status >= 200 && xhr.status < 300) {
let response;
try {
@@ -297,7 +300,7 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
};
xhr.onerror = () => {
showUploadError(genericErrorMsg, true);
if (attempt === uploadAttempt && !leavingUploadPage) showUploadError(genericErrorMsg, true);
};
xhr.send(formData);