58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
/**
|
||
* util/queue.js
|
||
* Verarbeitet Update-Checks als geordnete Warteschlange –
|
||
* verhindert parallele API-Anfragen und Rate-Limits.
|
||
*/
|
||
|
||
export class UpdateQueue {
|
||
constructor() {
|
||
this._queue = [];
|
||
this._running = false;
|
||
this.processed = 0; // Gesamt-Jobs seit Start
|
||
this.nextRunAt = null; // Zeitstempel des nächsten geplanten Runs
|
||
}
|
||
|
||
/** Anzahl der wartenden Jobs */
|
||
get size() {
|
||
return this._queue.length;
|
||
}
|
||
|
||
/** Ist die Queue gerade aktiv? */
|
||
get isRunning() {
|
||
return this._running;
|
||
}
|
||
|
||
/**
|
||
* Fügt einen asynchronen Job zur Queue hinzu.
|
||
* @param {() => Promise<void>} job
|
||
*/
|
||
enqueue(job) {
|
||
this._queue.push(job);
|
||
this._run();
|
||
}
|
||
|
||
/** Interne Abarbeitungsschleife – läuft bis Queue leer ist */
|
||
async _run() {
|
||
if (this._running) return;
|
||
this._running = true;
|
||
|
||
while (this._queue.length > 0) {
|
||
const job = this._queue.shift();
|
||
try {
|
||
await job();
|
||
} catch (e) {
|
||
console.error("[Queue] Job-Fehler:", e.message);
|
||
}
|
||
this.processed++;
|
||
|
||
// 500ms Pause zwischen Jobs
|
||
if (this._queue.length > 0) {
|
||
await new Promise((r) => setTimeout(r, 500));
|
||
}
|
||
}
|
||
|
||
this._running = false;
|
||
}
|
||
}
|
||
|
||
export default new UpdateQueue(); |