diff --git a/content.js b/content.js index cb52ebd..852600a 100644 --- a/content.js +++ b/content.js @@ -67,6 +67,37 @@ async function getApplicant(applicant, token, onProgress = () => { }) { return downloadApplicantFilesWithProgress(applicantDetails, token, onProgress) } +async function runWithConcurrencyLimit(items, limit, worker) { + const results = new Array(items.length) + let nextIndex = 0 + + async function runNext() { + const currentIndex = nextIndex + nextIndex += 1 + + if (currentIndex >= items.length) { + return + } + + try { + results[currentIndex] = { + status: "fulfilled", + value: await worker(items[currentIndex], currentIndex) + } + } catch (error) { + results[currentIndex] = { + status: "rejected", + reason: error + } + } + + await runNext() + } + + await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => runNext())) + return results +} + async function getApplicants() { const aid = getApplicationId() const token = await getXSRFToken() @@ -415,7 +446,7 @@ function rip(event) { progressDialog.initializeApplicant(applicant) }) - const applicantDetailsResults = await Promise.allSettled(applicants.map(async (applicant) => { + const applicantDetailsResults = await runWithConcurrencyLimit(applicants, 20, async (applicant) => { progressDialog.setStatus("Downloading applicant files (will take some time)...") try { const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => { @@ -426,7 +457,7 @@ function rip(event) { progressDialog.markApplicantFailed(applicant) throw error } - })) + }) console.log("Preparing zip archive...") progressDialog.setStatus("Preparing zip archive...")