mirror of
https://github.com/directus/directus.git
synced 2026-01-28 10:58:01 -05:00
32 lines
462 B
TypeScript
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;
|
|
}
|
|
}
|