Notes/src/services/spaced_update.ts

78 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-03 22:53:41 +03:00
type Updater = () => void;
2022-09-17 23:06:17 +02:00
class SpacedUpdate {
2024-04-03 22:53:41 +03:00
private updater: Updater;
private lastUpdated: number;
private changed: boolean;
private updateInterval: number;
private changeForbidden: boolean;
constructor(updater: Updater, updateInterval = 1000) {
2022-09-17 23:06:17 +02:00
this.updater = updater;
this.lastUpdated = Date.now();
this.changed = false;
this.updateInterval = updateInterval;
2024-04-03 22:53:41 +03:00
this.changeForbidden = false;
2022-09-17 23:06:17 +02:00
}
scheduleUpdate() {
if (!this.changeForbidden) {
this.changed = true;
setTimeout(() => this.triggerUpdate());
}
}
async updateNowIfNecessary() {
if (this.changed) {
this.changed = false; // optimistic...
try {
await this.updater();
}
catch (e) {
this.changed = true;
throw e;
}
}
}
isAllSavedAndTriggerUpdate() {
const allSaved = !this.changed;
this.updateNowIfNecessary();
return allSaved;
}
triggerUpdate() {
if (!this.changed) {
return;
}
if (Date.now() - this.lastUpdated > this.updateInterval) {
this.updater();
this.lastUpdated = Date.now();
this.changed = false;
}
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
2022-09-17 23:06:17 +02:00
this.scheduleUpdate();
}
}
2024-04-03 22:53:41 +03:00
async allowUpdateWithoutChange(callback: () => void) {
2022-09-17 23:06:17 +02:00
this.changeForbidden = true;
try {
await callback();
}
finally {
this.changeForbidden = false;
}
}
}
2024-04-03 22:53:41 +03:00
export = SpacedUpdate;