Files
directus/api/src/utils/job-queue.ts
2022-06-08 10:27:00 -04:00

32 lines
462 B
TypeScript

type Job = () => Promise<void> | void;
export class JobQueue {
private running: boolean;
private jobs: Job[];
constructor() {
this.running = false;
this.jobs = [];
}
public enqueue(job: Job): void {
this.jobs.push(job);
if (!this.running) {
this.run();
}
}
private async run(): Promise<void> {
this.running = true;
while (this.jobs.length > 0) {
const job = this.jobs.shift()!;
await job();
}
this.running = false;
}
}