Initial aishare service
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user