From bf0ebc78f26fa9f6c2c2999f9f7b6b5db3f954da Mon Sep 17 00:00:00 2001 From: Gaspard Jankowiak Date: Fri, 5 Jun 2026 13:30:21 +0200 Subject: [PATCH] batch retrieve the applicants' details in group of 20 --- content.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) 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...")