Skip to content

Commit 7ec00e7

Browse files
committed
feat: auto pull/backup outlives session
close #68
1 parent 0ab55d3 commit 7ec00e7

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

main.ts

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ interface ObsidianGitSettings {
2222
disablePopups: boolean;
2323
listChangedFilesInMessageBody: boolean;
2424
showStatusBar: boolean;
25+
lastAutoBackUp: string;
26+
lastAutoPull: string;
2527
}
2628
const DEFAULT_SETTINGS: ObsidianGitSettings = {
2729
commitMessage: "vault backup: {{date}}",
@@ -33,16 +35,18 @@ const DEFAULT_SETTINGS: ObsidianGitSettings = {
3335
pullBeforePush: true,
3436
disablePopups: false,
3537
listChangedFilesInMessageBody: false,
36-
showStatusBar: true
38+
showStatusBar: true,
39+
lastAutoBackUp: "",
40+
lastAutoPull: ""
3741
};
3842

3943
export default class ObsidianGit extends Plugin {
4044
git: SimpleGit;
4145
settings: ObsidianGitSettings;
4246
statusBar: StatusBar;
4347
state: PluginState;
44-
intervalIDBackup: number;
45-
intervalIDPull: number;
48+
timeoutIDBackup: number;
49+
timeoutIDPull: number;
4650
lastUpdate: number;
4751
gitReady = false;
4852
promiseQueue: PromiseQueue = new PromiseQueue();
@@ -98,6 +102,8 @@ export default class ObsidianGit extends Plugin {
98102
}
99103

100104
async onunload() {
105+
window.clearTimeout(this.timeoutIDBackup);
106+
window.clearTimeout(this.timeoutIDPull);
101107
console.log('unloading ' + this.manifest.name + " plugin");
102108
}
103109
async loadSettings() {
@@ -131,10 +137,18 @@ export default class ObsidianGit extends Plugin {
131137
}
132138

133139
if (this.settings.autoSaveInterval > 0) {
134-
this.enableAutoBackup();
140+
const now = new Date();
141+
const last = new Date(this.settings.lastAutoBackUp);
142+
143+
const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - last.getTime()) / 1000) / 60));
144+
this.startAutoBackup(diff <= 0 ? 0 : diff);
135145
}
136146
if (this.settings.autoPullInterval > 0) {
137-
this.enableAutoPull();
147+
const now = new Date();
148+
const last = new Date(this.settings.lastAutoPull);
149+
150+
const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - last.getTime()) / 1000) / 60));
151+
this.startAutoPull(diff <= 0 ? 0 : diff);
138152
}
139153
}
140154

@@ -309,35 +323,41 @@ export default class ObsidianGit extends Plugin {
309323

310324
// endregion: main methods
311325

312-
enableAutoBackup() {
313-
const minutes = this.settings.autoSaveInterval;
314-
this.intervalIDBackup = window.setInterval(
315-
() => this.promiseQueue.addTask(() => this.createBackup(true)),
316-
minutes * 60000
326+
startAutoBackup(minutes?: number) {
327+
this.timeoutIDBackup = window.setTimeout(
328+
() => {
329+
this.promiseQueue.addTask(() => this.createBackup(true));
330+
this.settings.lastAutoBackUp = new Date().toString();
331+
this.saveSettings();
332+
this.startAutoBackup();
333+
},
334+
(minutes ?? this.settings.autoSaveInterval) * 60000
317335
);
318-
this.registerInterval(this.intervalIDBackup);
319336
}
320337

321-
enableAutoPull() {
322-
const minutes = this.settings.autoPullInterval;
323-
this.intervalIDPull = window.setInterval(
324-
() => this.promiseQueue.addTask(() => this.pullChangesFromRemote()),
325-
minutes * 60000
338+
startAutoPull(minutes?: number) {
339+
this.timeoutIDPull = window.setTimeout(
340+
() => {
341+
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
342+
this.settings.lastAutoPull = new Date().toString();
343+
this.saveSettings();
344+
this.startAutoPull();
345+
},
346+
(minutes ?? this.settings.autoPullInterval) * 60000
326347
);
327-
this.registerInterval(this.intervalIDPull);
328348
}
329349

330-
disableAutoBackup(): boolean {
331-
if (this.intervalIDBackup) {
332-
clearInterval(this.intervalIDBackup);
350+
clearAutoBackup(): boolean {
351+
if (this.timeoutIDBackup) {
352+
window.clearTimeout(this.timeoutIDBackup);
333353
return true;
334354
}
335355
return false;
336356
}
337357

338-
disableAutoPull(): boolean {
339-
if (this.intervalIDPull) {
340-
clearInterval(this.intervalIDPull);
358+
clearAutoPull(): boolean {
359+
if (this.timeoutIDPull) {
360+
window.clearTimeout(this.timeoutIDPull);
341361
return true;
342362
}
343363
return false;
@@ -453,16 +473,16 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
453473
plugin.saveSettings();
454474

455475
if (plugin.settings.autoSaveInterval > 0) {
456-
plugin.disableAutoBackup();
457-
plugin.enableAutoBackup();
476+
plugin.clearAutoBackup();
477+
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
458478
new Notice(
459479
`Automatic backup enabled! Every ${plugin.settings.autoSaveInterval} minutes.`
460480
);
461481
} else if (
462482
plugin.settings.autoSaveInterval <= 0 &&
463-
plugin.intervalIDBackup
483+
plugin.timeoutIDBackup
464484
) {
465-
plugin.disableAutoBackup() &&
485+
plugin.clearAutoBackup() &&
466486
new Notice("Automatic backup disabled!");
467487
}
468488
} else {
@@ -484,16 +504,16 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
484504
plugin.saveSettings();
485505

486506
if (plugin.settings.autoPullInterval > 0) {
487-
plugin.disableAutoPull();
488-
plugin.enableAutoPull();
507+
plugin.clearAutoPull();
508+
plugin.startAutoPull(plugin.settings.autoSaveInterval);
489509
new Notice(
490510
`Automatic pull enabled! Every ${plugin.settings.autoPullInterval} minutes.`
491511
);
492512
} else if (
493513
plugin.settings.autoPullInterval <= 0 &&
494-
plugin.intervalIDPull
514+
plugin.timeoutIDPull
495515
) {
496-
plugin.disableAutoPull() &&
516+
plugin.clearAutoPull() &&
497517
new Notice("Automatic pull disabled!");
498518
}
499519
} else {

0 commit comments

Comments
 (0)