feat(webui): concurrent downloading

This commit is contained in:
tretrauit 2024-03-22 22:12:17 +07:00
parent 51834cc7e6
commit c2203943e2

View File

@ -73,18 +73,20 @@ getLessonButton.addEventListener('click', async () => {
<br>`; <br>`;
} }
let videoDlResult = null; let videoDlResult = null;
const promises = [];
const downloadSelectedButton = document.createElement("button"); const downloadSelectedButton = document.createElement("button");
downloadSelectedButton.innerHTML = "Download Selected"; downloadSelectedButton.innerHTML = "Download Selected";
downloadSelectedButton.addEventListener('click', async () => { downloadSelectedButton.addEventListener('click', async () => {
for (const [id, video] of Object.entries(ids)) { for (const [id, video] of Object.entries(ids)) {
promises.push(async () => {
const checkbox = document.querySelector(`#${id}`); const checkbox = document.querySelector(`#${id}`);
if (!checkbox.checked) { if (!checkbox.checked) {
continue; return;
} }
const videoRsp = await fetch(`/api/v1/videos/${video.id}`); const videoRsp = await fetch(`/api/v1/videos/${video.id}`);
if (videoRsp.status !== 200) { if (videoRsp.status !== 200) {
console.error(`Failed to get video ${video.name}`); console.error(`Failed to get video ${video.name}`);
continue; return;
} }
const videoData = await videoRsp.json(); const videoData = await videoRsp.json();
if (videoDlResult === null) { if (videoDlResult === null) {
@ -115,7 +117,9 @@ getLessonButton.addEventListener('click', async () => {
if (rsp.status !== 200) { if (rsp.status !== 200) {
console.error(`Failed to download ${video.name}`); console.error(`Failed to download ${video.name}`);
} }
});
} }
await Promise.all(promises.map(p => p()));
}); });
result.appendChild(downloadSelectedButton); result.appendChild(downloadSelectedButton);
// result.innerHTML += "<button>Download All</button>"; // result.innerHTML += "<button>Download All</button>";