69 lines
2.5 KiB
JavaScript
69 lines
2.5 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
class SyncScheduler {
|
|
constructor(task, debounceMs) {
|
|
this.task = task;
|
|
this.debounceMs = debounceMs;
|
|
this.pending = new Map();
|
|
this.timers = new Map();
|
|
this.flushing = false;
|
|
}
|
|
schedule(dirPath) {
|
|
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, firstRun) {
|
|
const existing = this.timers.get(dirPath);
|
|
if (existing) {
|
|
clearTimeout(existing);
|
|
this.timers.delete(dirPath);
|
|
}
|
|
this.pending.set(dirPath, !!firstRun);
|
|
void this.flush();
|
|
}
|
|
flush() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
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 {
|
|
yield this.task(dirPath, firstRun);
|
|
}
|
|
catch (error) {
|
|
console.log("ERROR:", error.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
finally {
|
|
this.flushing = false;
|
|
if (this.pending.size > 0) {
|
|
void this.flush();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
exports.default = SyncScheduler;
|