🐛 Fix task manager
This commit is contained in:
parent
a7532a92af
commit
822b7d3b2e
1 changed files with 14 additions and 10 deletions
|
@ -4,17 +4,21 @@ export default function taskManager(tasks: Array<() => Promise<void>>, maxConcur
|
||||||
let currentlyRunningTasks = 0;
|
let currentlyRunningTasks = 0;
|
||||||
|
|
||||||
const startNewTask = () => {
|
const startNewTask = () => {
|
||||||
if (remainingTasks.length === 0 && currentlyRunningTasks === 0) {
|
//Exit if we completed all tasks
|
||||||
return resolve();
|
if (remainingTasks.length === 0) {
|
||||||
|
if (currentlyRunningTasks === 0) {
|
||||||
|
return resolve();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//If there is at least one task left, complete it
|
||||||
|
const curTask = remainingTasks.pop() as () => Promise<void>;
|
||||||
|
const curPromise = curTask();
|
||||||
|
currentlyRunningTasks += 1;
|
||||||
|
curPromise.then(() => {
|
||||||
|
currentlyRunningTasks -= 1;
|
||||||
|
return startNewTask();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const curTask = remainingTasks.pop() as () => Promise<void>;
|
|
||||||
const curPromise = curTask();
|
|
||||||
currentlyRunningTasks += 1;
|
|
||||||
curPromise.then(() => {
|
|
||||||
currentlyRunningTasks -= 1;
|
|
||||||
return startNewTask();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (let i = 0; i < maxConcurrentTasks; i += 1) {
|
for (let i = 0; i < maxConcurrentTasks; i += 1) {
|
||||||
|
|
Loading…
Reference in a new issue