batch retrieve the applicants' details in group of 20
This commit is contained in:
parent
b203e9bef3
commit
bf0ebc78f2
1 changed files with 33 additions and 2 deletions
35
content.js
35
content.js
|
|
@ -67,6 +67,37 @@ async function getApplicant(applicant, token, onProgress = () => { }) {
|
||||||
return downloadApplicantFilesWithProgress(applicantDetails, 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() {
|
async function getApplicants() {
|
||||||
const aid = getApplicationId()
|
const aid = getApplicationId()
|
||||||
const token = await getXSRFToken()
|
const token = await getXSRFToken()
|
||||||
|
|
@ -415,7 +446,7 @@ function rip(event) {
|
||||||
progressDialog.initializeApplicant(applicant)
|
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)...")
|
progressDialog.setStatus("Downloading applicant files (will take some time)...")
|
||||||
try {
|
try {
|
||||||
const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => {
|
const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => {
|
||||||
|
|
@ -426,7 +457,7 @@ function rip(event) {
|
||||||
progressDialog.markApplicantFailed(applicant)
|
progressDialog.markApplicantFailed(applicant)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}))
|
})
|
||||||
|
|
||||||
console.log("Preparing zip archive...")
|
console.log("Preparing zip archive...")
|
||||||
progressDialog.setStatus("Preparing zip archive...")
|
progressDialog.setStatus("Preparing zip archive...")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue