Bugfix: fix race conditions

This commit is contained in:
2026-08-10 11:07:39 +01:00
parent 44c5c19050
commit 9eae95e391
14 changed files with 326 additions and 175 deletions
+63
View File
@@ -0,0 +1,63 @@
type SyncTask = (dirPath: string, firstRun?: boolean) => Promise<void>;
export default class SyncScheduler {
private pending = new Map<string, boolean>();
private timers = new Map<string, NodeJS.Timeout>();
private flushing = false;
constructor(
private readonly task: SyncTask,
private readonly debounceMs: number
) {}
schedule(dirPath: string) {
const existing = this.timers.get(dirPath);
if (existing) clearTimeout(existing);
const timer = setTimeout(() => {
this.timers.delete(dirPath);
this.pending.set(dirPath, false);
void this.flush();
}, this.debounceMs);
this.timers.set(dirPath, timer);
}
enqueue(dirPath: string, firstRun?: boolean) {
const existing = this.timers.get(dirPath);
if (existing) {
clearTimeout(existing);
this.timers.delete(dirPath);
}
this.pending.set(dirPath, !!firstRun);
void this.flush();
}
private async flush() {
if (this.flushing) return;
this.flushing = true;
try {
while (this.pending.size > 0) {
const batch = [...this.pending.entries()];
this.pending.clear();
for (const [dirPath, firstRun] of batch) {
try {
await this.task(dirPath, firstRun);
} catch (error: any) {
console.log("ERROR:", error.message);
}
}
}
} finally {
this.flushing = false;
if (this.pending.size > 0) {
void this.flush();
}
}
}
}