Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make XHR requests async
  • Loading branch information
regalijan committed Mar 26, 2024
1 parent d5fa259 commit 2f335cf
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions app/routes/report.tsx
Expand Up @@ -33,6 +33,10 @@ export function meta() {
{
title: "Report an Exploiter - Car Crushers",
},
{
name: "description",
content: "Use this page to report a cheater",
},
];
}

Expand Down Expand Up @@ -170,33 +174,37 @@ export default function () {
};

for (let i = 0; i < upload_urls.length; i++) {
const xhr = new XMLHttpRequest();

xhr.open("PUT", upload_urls[i], false);
xhr.setRequestHeader(
"content-type",
(files[i].name.split(".").at(-1) as string).toLowerCase() === "mov"
? "video/mp4"
: files[i].type ||
fileTypes[files[i].name.split(".").at(-1) as string],
);

xhr.upload.addEventListener("progress", increaseProgress);
xhr.upload.onabort = () => {
shouldRecall = true;
setUploading(false);
setFileProgress(0);
};
xhr.upload.onerror = () => {
shouldRecall = true;
setUploading(false);
setFileProgress(0);
};
xhr.upload.onloadend = () => {
if (i === upload_urls.length - 1) setUploading(false);
};

xhr.send(files[i]);
await new Promise((resolve) => {
const xhr = new XMLHttpRequest();

xhr.open("PUT", upload_urls[i], true);
xhr.setRequestHeader(
"content-type",
(files[i].name.split(".").at(-1) as string).toLowerCase() === "mov"
? "video/mp4"
: files[i].type ||
fileTypes[files[i].name.split(".").at(-1) as string],
);

xhr.upload.addEventListener("progress", increaseProgress);
xhr.upload.onabort = () => {
shouldRecall = true;
setUploading(false);
setFileProgress(0);
};
xhr.upload.onerror = () => {
shouldRecall = true;
setUploading(false);
setFileProgress(0);
};
xhr.upload.onloadend = () => {
if (i === upload_urls.length - 1) setUploading(false);

resolve(null);
};

xhr.send(files[i]);
});
}

if (shouldRecall) {
Expand Down

0 comments on commit 2f335cf

Please sign in to comment.