Skip to content

Commit

Permalink
Added runAfterEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Dec 3, 2024
1 parent 3b6d074 commit 4ab124a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/models/TaskManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Queue from "./Queue";

const sleep = (n) => new Promise((r) => setTimeout(r, n));

export default class TaskManager {

public rateLimit = 10;
Expand Down Expand Up @@ -54,4 +56,27 @@ export default class TaskManager {
}
}

/**
* You can queue this function that will be
* executed after all pending fetch tasks
* are finished
* @param fx any function
*/
public runAfterEnd(fx: () => any) {
(async () => {
for(;;) {
await sleep(100);
if (this.running.size > 0) {
continue;
}
await sleep(10);
if (this.running.size > 0) {
continue;
}
break;
}
fx();
})().catch(console.error);
}

}

0 comments on commit 4ab124a

Please sign in to comment.