2024-12-19 22:06:42 +02:00
|
|
|
type Callback = () => Promise<void> | void;
|
2024-07-25 19:22:44 +03:00
|
|
|
|
2020-01-19 21:40:23 +01:00
|
|
|
export default class SpacedUpdate {
|
2024-07-25 19:22:44 +03:00
|
|
|
private updater: Callback;
|
|
|
|
|
private lastUpdated: number;
|
|
|
|
|
private changed: boolean;
|
|
|
|
|
private updateInterval: number;
|
|
|
|
|
private changeForbidden?: boolean;
|
|
|
|
|
|
|
|
|
|
constructor(updater: Callback, updateInterval = 1000) {
|
2020-01-19 21:40:23 +01:00
|
|
|
this.updater = updater;
|
|
|
|
|
this.lastUpdated = Date.now();
|
|
|
|
|
this.changed = false;
|
|
|
|
|
this.updateInterval = updateInterval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scheduleUpdate() {
|
2020-02-02 10:10:37 +01:00
|
|
|
if (!this.changeForbidden) {
|
2020-01-24 22:30:17 +01:00
|
|
|
this.changed = true;
|
|
|
|
|
setTimeout(() => this.triggerUpdate());
|
|
|
|
|
}
|
2020-01-19 21:40:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateNowIfNecessary() {
|
|
|
|
|
if (this.changed) {
|
2021-02-28 19:46:04 +01:00
|
|
|
this.changed = false; // optimistic...
|
2021-02-27 23:39:02 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.updater();
|
2025-01-09 18:07:02 +02:00
|
|
|
} catch (e) {
|
2021-02-27 23:39:02 +01:00
|
|
|
this.changed = true;
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-01-19 21:40:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-27 23:39:02 +01:00
|
|
|
isAllSavedAndTriggerUpdate() {
|
|
|
|
|
const allSaved = !this.changed;
|
|
|
|
|
|
|
|
|
|
this.updateNowIfNecessary();
|
|
|
|
|
|
|
|
|
|
return allSaved;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 00:14:31 +02:00
|
|
|
/**
|
|
|
|
|
* Normally {@link scheduleUpdate()} would actually trigger the update only once per {@link updateInterval}. If the method is called 200 times within 20s, it will execute only 20 times.
|
|
|
|
|
* Sometimes, if the updates are continuous this would cause a performance impact. Resetting the time ensures that the calls to {@link triggerUpdate} have stopped before actually triggering an update.
|
|
|
|
|
*/
|
|
|
|
|
resetUpdateTimer() {
|
|
|
|
|
this.lastUpdated = Date.now();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:28:15 +03:00
|
|
|
/**
|
|
|
|
|
* Sets the update interval for the spaced update.
|
|
|
|
|
* @param interval The update interval in milliseconds.
|
|
|
|
|
*/
|
|
|
|
|
setUpdateInterval(interval: number) {
|
|
|
|
|
this.updateInterval = interval;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-19 21:40:23 +01:00
|
|
|
triggerUpdate() {
|
|
|
|
|
if (!this.changed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Date.now() - this.lastUpdated > this.updateInterval) {
|
|
|
|
|
this.updater();
|
|
|
|
|
this.lastUpdated = Date.now();
|
|
|
|
|
this.changed = false;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2023-06-30 11:18:34 +02:00
|
|
|
// update isn't triggered but changes are still pending, so we need to schedule another check
|
2020-01-19 21:40:23 +01:00
|
|
|
this.scheduleUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 22:30:17 +01:00
|
|
|
|
2024-07-25 19:22:44 +03:00
|
|
|
async allowUpdateWithoutChange(callback: Callback) {
|
2020-01-24 22:30:17 +01:00
|
|
|
this.changeForbidden = true;
|
|
|
|
|
|
|
|
|
|
try {
|
2020-02-08 21:23:42 +01:00
|
|
|
await callback();
|
2025-01-09 18:07:02 +02:00
|
|
|
} finally {
|
2020-01-24 22:30:17 +01:00
|
|
|
this.changeForbidden = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-19 21:40:23 +01:00
|
|
|
}
|