@@ -22,6 +22,8 @@ interface ObsidianGitSettings {
22
22
disablePopups : boolean ;
23
23
listChangedFilesInMessageBody : boolean ;
24
24
showStatusBar : boolean ;
25
+ lastAutoBackUp : string ;
26
+ lastAutoPull : string ;
25
27
}
26
28
const DEFAULT_SETTINGS : ObsidianGitSettings = {
27
29
commitMessage : "vault backup: {{date}}" ,
@@ -33,16 +35,18 @@ const DEFAULT_SETTINGS: ObsidianGitSettings = {
33
35
pullBeforePush : true ,
34
36
disablePopups : false ,
35
37
listChangedFilesInMessageBody : false ,
36
- showStatusBar : true
38
+ showStatusBar : true ,
39
+ lastAutoBackUp : "" ,
40
+ lastAutoPull : ""
37
41
} ;
38
42
39
43
export default class ObsidianGit extends Plugin {
40
44
git : SimpleGit ;
41
45
settings : ObsidianGitSettings ;
42
46
statusBar : StatusBar ;
43
47
state : PluginState ;
44
- intervalIDBackup : number ;
45
- intervalIDPull : number ;
48
+ timeoutIDBackup : number ;
49
+ timeoutIDPull : number ;
46
50
lastUpdate : number ;
47
51
gitReady = false ;
48
52
promiseQueue : PromiseQueue = new PromiseQueue ( ) ;
@@ -98,6 +102,8 @@ export default class ObsidianGit extends Plugin {
98
102
}
99
103
100
104
async onunload ( ) {
105
+ window . clearTimeout ( this . timeoutIDBackup ) ;
106
+ window . clearTimeout ( this . timeoutIDPull ) ;
101
107
console . log ( 'unloading ' + this . manifest . name + " plugin" ) ;
102
108
}
103
109
async loadSettings ( ) {
@@ -131,10 +137,18 @@ export default class ObsidianGit extends Plugin {
131
137
}
132
138
133
139
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 ) ;
135
145
}
136
146
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 ) ;
138
152
}
139
153
}
140
154
@@ -309,35 +323,41 @@ export default class ObsidianGit extends Plugin {
309
323
310
324
// endregion: main methods
311
325
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
317
335
) ;
318
- this . registerInterval ( this . intervalIDBackup ) ;
319
336
}
320
337
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
326
347
) ;
327
- this . registerInterval ( this . intervalIDPull ) ;
328
348
}
329
349
330
- disableAutoBackup ( ) : boolean {
331
- if ( this . intervalIDBackup ) {
332
- clearInterval ( this . intervalIDBackup ) ;
350
+ clearAutoBackup ( ) : boolean {
351
+ if ( this . timeoutIDBackup ) {
352
+ window . clearTimeout ( this . timeoutIDBackup ) ;
333
353
return true ;
334
354
}
335
355
return false ;
336
356
}
337
357
338
- disableAutoPull ( ) : boolean {
339
- if ( this . intervalIDPull ) {
340
- clearInterval ( this . intervalIDPull ) ;
358
+ clearAutoPull ( ) : boolean {
359
+ if ( this . timeoutIDPull ) {
360
+ window . clearTimeout ( this . timeoutIDPull ) ;
341
361
return true ;
342
362
}
343
363
return false ;
@@ -453,16 +473,16 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
453
473
plugin . saveSettings ( ) ;
454
474
455
475
if ( plugin . settings . autoSaveInterval > 0 ) {
456
- plugin . disableAutoBackup ( ) ;
457
- plugin . enableAutoBackup ( ) ;
476
+ plugin . clearAutoBackup ( ) ;
477
+ plugin . startAutoBackup ( plugin . settings . autoSaveInterval ) ;
458
478
new Notice (
459
479
`Automatic backup enabled! Every ${ plugin . settings . autoSaveInterval } minutes.`
460
480
) ;
461
481
} else if (
462
482
plugin . settings . autoSaveInterval <= 0 &&
463
- plugin . intervalIDBackup
483
+ plugin . timeoutIDBackup
464
484
) {
465
- plugin . disableAutoBackup ( ) &&
485
+ plugin . clearAutoBackup ( ) &&
466
486
new Notice ( "Automatic backup disabled!" ) ;
467
487
}
468
488
} else {
@@ -484,16 +504,16 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
484
504
plugin . saveSettings ( ) ;
485
505
486
506
if ( plugin . settings . autoPullInterval > 0 ) {
487
- plugin . disableAutoPull ( ) ;
488
- plugin . enableAutoPull ( ) ;
507
+ plugin . clearAutoPull ( ) ;
508
+ plugin . startAutoPull ( plugin . settings . autoSaveInterval ) ;
489
509
new Notice (
490
510
`Automatic pull enabled! Every ${ plugin . settings . autoPullInterval } minutes.`
491
511
) ;
492
512
} else if (
493
513
plugin . settings . autoPullInterval <= 0 &&
494
- plugin . intervalIDPull
514
+ plugin . timeoutIDPull
495
515
) {
496
- plugin . disableAutoPull ( ) &&
516
+ plugin . clearAutoPull ( ) &&
497
517
new Notice ( "Automatic pull disabled!" ) ;
498
518
}
499
519
} else {
0 commit comments