@@ -43,6 +43,7 @@ export default class ObsidianGit extends Plugin {
43
43
intervalIDPull : number ;
44
44
lastUpdate : number ;
45
45
gitReady = false ;
46
+ promiseQueue : PromiseQueue = new PromiseQueue ( ) ;
46
47
conflictOutputFile = "conflict-files-obsidian-git.md" ;
47
48
48
49
setState ( state : PluginState ) {
@@ -59,13 +60,13 @@ export default class ObsidianGit extends Plugin {
59
60
this . addCommand ( {
60
61
id : "pull" ,
61
62
name : "Pull from remote repository" ,
62
- callback : ( ) => this . pullChangesFromRemote ( ) ,
63
+ callback : ( ) => this . promiseQueue . addTask ( ( ) => this . pullChangesFromRemote ( ) ) ,
63
64
} ) ;
64
65
65
66
this . addCommand ( {
66
67
id : "push" ,
67
68
name : "Commit *all* changes and push to remote repository" ,
68
- callback : ( ) => this . createBackup ( false )
69
+ callback : ( ) => this . promiseQueue . addTask ( ( ) => this . createBackup ( false ) )
69
70
} ) ;
70
71
71
72
this . addCommand ( {
@@ -122,7 +123,7 @@ export default class ObsidianGit extends Plugin {
122
123
this . setState ( PluginState . idle ) ;
123
124
124
125
if ( this . settings . autoPullOnBoot ) {
125
- this . pullChangesFromRemote ( ) ;
126
+ this . promiseQueue . addTask ( ( ) => this . pullChangesFromRemote ( ) ) ;
126
127
}
127
128
128
129
if ( this . settings . autoSaveInterval > 0 ) {
@@ -307,7 +308,7 @@ export default class ObsidianGit extends Plugin {
307
308
enableAutoBackup ( ) {
308
309
const minutes = this . settings . autoSaveInterval ;
309
310
this . intervalIDBackup = window . setInterval (
310
- async ( ) => await this . createBackup ( true ) ,
311
+ ( ) => this . promiseQueue . addTask ( ( ) => this . createBackup ( true ) ) ,
311
312
minutes * 60000
312
313
) ;
313
314
this . registerInterval ( this . intervalIDBackup ) ;
@@ -316,7 +317,7 @@ export default class ObsidianGit extends Plugin {
316
317
enableAutoPull ( ) {
317
318
const minutes = this . settings . autoPullInterval ;
318
319
this . intervalIDPull = window . setInterval (
319
- async ( ) => await this . pullChangesFromRemote ( ) ,
320
+ ( ) => this . promiseQueue . addTask ( ( ) => this . pullChangesFromRemote ( ) ) ,
320
321
minutes * 60000
321
322
) ;
322
323
this . registerInterval ( this . intervalIDPull ) ;
@@ -731,7 +732,7 @@ class CustomMessageModal extends SuggestModal<string> {
731
732
}
732
733
733
734
onChooseSuggestion ( item : string , _ : MouseEvent | KeyboardEvent ) : void {
734
- this . plugin . createBackup ( false , item ) ;
735
+ this . plugin . promiseQueue . addTask ( ( ) => this . plugin . createBackup ( false , item ) ) ;
735
736
}
736
737
737
738
}
@@ -772,3 +773,22 @@ class ChangedFilesModal extends FuzzySuggestModal<FileStatusResult> {
772
773
}
773
774
}
774
775
}
776
+
777
+ class PromiseQueue {
778
+ tasks : ( ( ) => Promise < any > ) [ ] = [ ] ;
779
+
780
+ addTask ( task : ( ) => Promise < any > ) {
781
+ this . tasks . push ( task ) ;
782
+ if ( this . tasks . length === 1 ) {
783
+ this . handleTask ( ) ;
784
+ }
785
+ }
786
+ async handleTask ( ) {
787
+ if ( this . tasks . length > 0 ) {
788
+ this . tasks [ 0 ] ( ) . finally ( ( ) => {
789
+ this . tasks . shift ( ) ;
790
+ this . handleTask ( ) ;
791
+ } ) ;
792
+ }
793
+ }
794
+ }
0 commit comments