Only save modified files. Much faster saves.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3640 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-01-13 02:40:46 +00:00
parent 85c5b4c4d6
commit cf195cdd44

View File

@ -5,6 +5,7 @@ var gCredentials;
var gCurrentFile; var gCurrentFile;
var gFiles = {}; var gFiles = {};
var gApp = {files: {}};
var gEditor; var gEditor;
var kErrorColor = "#dc322f"; var kErrorColor = "#dc322f";
@ -160,6 +161,10 @@ function load() {
gFiles[name] = {}; gFiles[name] = {};
loadFile(name, json['files'][name]); loadFile(name, json['files'][name]);
}); });
if (Object.keys(json['files']).length == 0) {
document.getElementById("editPane").style.display = 'flex';
}
gApp = JSON.parse(text);
} }
} catch { } catch {
} }
@ -212,20 +217,23 @@ function save() {
var appFinished = function(success) { var appFinished = function(success) {
document.getElementById("save").disabled = false; document.getElementById("save").disabled = false;
Object.values(gFiles).forEach(function(file) { Object.values(gFiles).forEach(function(file) {
file.doc.markClean(); file.generation = file.doc.changeGeneration();
}); });
updateFiles(); updateFiles();
} }
var always = function() { var always = function() {
var anyUnfinished = Object.values(gFiles).some(x => x.request); var anyUnfinished = Object.values(gFiles).some(x => x.request);
var anyUnsaved = Object.values(gFiles).some(x => !x.id); var anyUnsaved = Object.values(gFiles).some(x => !x.doc.isClean(x.generation) && !x.id);
if (!anyUnfinished && !anyUnsaved) { if (!anyUnfinished && !anyUnsaved) {
var app = { var app = {
type: "tildefriends-app", type: "tildefriends-app",
files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id])), files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id || gApp.files[x]])),
}; };
console.log(app);
Object.values(gFiles).forEach(function(file) { delete file.id; }); Object.values(gFiles).forEach(function(file) { delete file.id; });
gApp = JSON.parse(JSON.stringify(app));
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.addEventListener("error", function() { request.addEventListener("error", function() {
@ -274,7 +282,14 @@ function save() {
} }
}; };
var anySkipped = false;
Object.values(gFiles).forEach(function(file) { Object.values(gFiles).forEach(function(file) {
if (file.doc.isClean(file.generation)) {
console.log("Not saving clean file.");
anySkipped = true;
return;
}
delete file.id; delete file.id;
file.request = new XMLHttpRequest(); file.request = new XMLHttpRequest();
file.request.addEventListener("error", function() { file.request.addEventListener("error", function() {
@ -305,10 +320,15 @@ function save() {
always(); always();
}); });
console.log("Saving file");
file.request.open("POST", "/save", true); file.request.open("POST", "/save", true);
file.request.setRequestHeader("Content-Type", "text/plain"); file.request.setRequestHeader("Content-Type", "text/plain");
file.request.send(file.doc.getValue()); file.request.send(file.doc.getValue());
}); });
if (anySkipped) {
always();
}
} }
function url() { function url() {
@ -591,7 +611,7 @@ function updateFiles() {
if (file == gCurrentFile) { if (file == gCurrentFile) {
li.classList.add("current"); li.classList.add("current");
} }
if (!gFiles[file].doc.isClean()) { if (!gFiles[file].doc.isClean(gFiles[file].generation)) {
li.classList.add("dirty"); li.classList.add("dirty");
} }
node.appendChild(li); node.appendChild(li);
@ -602,7 +622,8 @@ function updateFiles() {
function makeNewFile(name) { function makeNewFile(name) {
gFiles[name] = { gFiles[name] = {
doc: new CodeMirror.Doc("", guessMode(name)) doc: new CodeMirror.Doc("", guessMode(name)),
generation: -1,
}; };
openFile(name); openFile(name);
} }