37 lines
849 B
TypeScript
37 lines
849 B
TypeScript
import { store } from "./db.js";
|
|
import { runCapture } from "./capture.js";
|
|
|
|
class CaptureQueue {
|
|
private queue: string[] = [];
|
|
private running = false;
|
|
|
|
enqueue(id: string): void {
|
|
if (!this.queue.includes(id)) this.queue.push(id);
|
|
void this.drain();
|
|
}
|
|
|
|
restorePending(): void {
|
|
for (const row of store.queuedCaptures()) {
|
|
store.updateStatus(row.id, "queued");
|
|
this.enqueue(row.id);
|
|
}
|
|
}
|
|
|
|
private async drain(): Promise<void> {
|
|
if (this.running) return;
|
|
this.running = true;
|
|
try {
|
|
while (this.queue.length) {
|
|
const id = this.queue.shift()!;
|
|
const row = store.getCapture(id);
|
|
if (!row || row.status === "ready") continue;
|
|
await runCapture(row);
|
|
}
|
|
} finally {
|
|
this.running = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
export const captureQueue = new CaptureQueue();
|