batch retrieve the applicants' details in group of 20

This commit is contained in:
Gaspard Jankowiak 2026-06-05 13:30:21 +02:00 committed by gapato
commit bf0ebc78f2

View file

@ -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...")