@@ -28,6 +28,11 @@ export default class SyncManager {
2828 private eventsListener : EventsListener ;
2929 private syncIntervalId : number | null = null ;
3030
31+ // Use to track if syncing is in progress, this ideally
32+ // prevents multiple syncs at the same time and creation
33+ // of messy conflicts.
34+ private syncing : boolean = false ;
35+
3136 constructor (
3237 private vault : Vault ,
3338 private settings : GitHubSyncSettings ,
@@ -70,6 +75,21 @@ export default class SyncManager {
7075 * This fails if neither remote nor local folders are empty.
7176 */
7277 async firstSync ( ) {
78+ if ( this . syncing ) {
79+ this . logger . info ( "First sync already in progress" ) ;
80+ // We're already syncing, nothing to do
81+ return ;
82+ }
83+
84+ this . syncing = true ;
85+ try {
86+ this . firstSyncImpl ( ) ;
87+ } finally {
88+ this . syncing = false ;
89+ }
90+ }
91+
92+ private async firstSyncImpl ( ) {
7393 await this . logger . info ( "Starting first sync" ) ;
7494 let repositoryIsBare = false ;
7595 let res : RepoContent ;
@@ -83,6 +103,7 @@ export default class SyncManager {
83103 treeSha = res . sha ;
84104 } catch ( err ) {
85105 if ( err . status !== 409 ) {
106+ this . syncing = false ;
86107 throw err ;
87108 }
88109 // The repository is bare, meaning it has no tree, no commits and no branches
@@ -137,7 +158,7 @@ export default class SyncManager {
137158 * @param files All files in the remote repository, including those not in its content dir.
138159 * @param treeSha The SHA of the tree in the remote repository.
139160 */
140- async firstSyncFromRemote (
161+ private async firstSyncFromRemote (
141162 files : { [ key : string ] : GetTreeResponseItem } ,
142163 treeSha : string ,
143164 ) {
@@ -200,7 +221,7 @@ export default class SyncManager {
200221 * @param files All files in the remote repository
201222 * @param treeSha The SHA of the tree in the remote repository.
202223 */
203- async firstSyncFromLocal (
224+ private async firstSyncFromLocal (
204225 files : { [ key : string ] : GetTreeResponseItem } ,
205226 treeSha : string ,
206227 ) {
@@ -241,6 +262,21 @@ export default class SyncManager {
241262 * @returns
242263 */
243264 async sync ( ) {
265+ if ( this . syncing ) {
266+ this . logger . info ( "Sync already in progress" ) ;
267+ // We're already syncing, nothing to do
268+ return ;
269+ }
270+
271+ this . syncing = true ;
272+ try {
273+ await this . syncImpl ( ) ;
274+ } finally {
275+ this . syncing = false ;
276+ }
277+ }
278+
279+ private async syncImpl ( ) {
244280 await this . logger . info ( "Starting sync" ) ;
245281 const { files, sha : treeSha } = await this . client . getRepoContent ( ) ;
246282 const manifest = files [ `${ this . vault . configDir } /${ MANIFEST_FILE_NAME } ` ] ;
0 commit comments