Skip to content

Commit

Permalink
Ensure that project saving is atomic, and ignore any fileWatcher call…
Browse files Browse the repository at this point in the history
…backs while we're in the middle of saving
  • Loading branch information
joethephish authored and smwhr committed May 14, 2024
1 parent 7aaf1bf commit ced5c61
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/renderer/inkProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function InkProject(mainInkFilePath) {
// Wait for all project files to be found before starting first compilation
this.ready = false;

// Make sure a project save is atomic
this.saveActive = false;

this.startFileWatching();
}

Expand Down Expand Up @@ -226,6 +229,7 @@ InkProject.prototype.startFileWatching = function() {
}

this.fileWatcher.on("add", newlyFoundAbsFilePath => {
if( this.saveActive ) return; // ignore file watching while atomic save is active
if( tryUpdateSettingsFile(newlyFoundAbsFilePath) ) return;
if (!isInkFile(newlyFoundAbsFilePath)) { return; }

Expand All @@ -248,6 +252,7 @@ InkProject.prototype.startFileWatching = function() {
});

this.fileWatcher.on("change", updatedAbsFilePath => {
if( this.saveActive ) return; // ignore file watching while atomic save is active
if( tryUpdateSettingsFile(updatedAbsFilePath) ) return;
if (!isInkFile(updatedAbsFilePath)) { return; }

Expand All @@ -268,6 +273,7 @@ InkProject.prototype.startFileWatching = function() {
}
});
this.fileWatcher.on("unlink", removedAbsFilePath => {
if( this.saveActive ) return; // ignore file watching while atomic save is active
if( tryUpdateSettingsFile(removedAbsFilePath) ) return;
if (!isInkFile(removedAbsFilePath)) { return; }

Expand Down Expand Up @@ -304,6 +310,10 @@ InkProject.prototype.showInkFile = function(inkFile) {

InkProject.prototype.save = function() {

// Make saving atomic, don't save again if we're already saving
if( this.saveActive ) return;
this.saveActive = true;

var wasUnsaved = !this.mainInk.projectDir;

var filesRemaining = this.files.length;
Expand All @@ -321,6 +331,8 @@ InkProject.prototype.save = function() {

if( allSuccess )
InkProject.events.didSave();

this.saveActive = false;
}
}

Expand All @@ -336,6 +348,11 @@ InkProject.prototype.save = function() {
if( wasUnsaved ) this.startFileWatching();

includeFiles.forEach(f => f.save(success => singleFileSaveComplete(f, success)));
}

// Cancel the save process because main ink file save failed
else {
this.saveActive = false;
}
});
}
Expand Down

0 comments on commit ced5c61

Please sign in to comment.