changed: save strategy! again!

This commit is contained in:
quenousimporte 2023-01-26 00:32:00 +01:00
parent 88444f40fc
commit f75c7b6bd2
1 changed files with 56 additions and 68 deletions

124
main.js
View File

@ -1348,84 +1348,72 @@ function postpone()
}); });
} }
function waitpending()
{
return new Promise(function(resolve)
{
if (!pending)
{
resolve();
}
else
{
console.log("query already pending, waiting...");
var id = setInterval(() =>
{
if (!pending)
{
console.log("...clear");
clearInterval(id);
resolve();
}
}, 100);
}
});
}
function save() function save()
{ {
clearTimeout(workerid); clearTimeout(workerid);
waitpending()
.then(() => if (!localdata)
{ {
var content = getnotecontent(); showtemporaryinfo("cannot push empty data");
return;
}
if ((content == "" && backup != "") || content == "null" || content == "undefined") if (pending)
{ {
showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved"); console.log("pending query: save cancelled");
return; return;
} }
currentnote.pos = md.selectionStart;
currentnote.content = content;
window.localStorage.setItem(currentvault, JSON.stringify(localdata)); var content = getnotecontent();
if (currentnote.title == "settings.json") if ((content == "" && backup != "") || content == "null" || content == "undefined")
{ {
settings = JSON.parse(content); showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
savesettings(); return;
} }
console.log("data serialized in local storage")
if (isremote()) currentnote.pos = md.selectionStart;
{ currentnote.content = content;
console.log("sending data to php server...");
if (localdata) window.localStorage.setItem(currentvault, JSON.stringify(localdata));
{
pending = true; if (currentnote.title == "settings.json")
queryremote({action: "push", data: JSON.stringify(localdata)}) {
.then(() => settings = JSON.parse(content);
{ savesettings();
console.log("data saved on server."); }
saved = true; console.log("data serialized in local storage")
})
.catch(remotecallfailed) if (isremote())
.finally(() => {
{ console.log("sending data to php server...");
pending = false;
}); pending = true;
} queryremote({action: "push", data: JSON.stringify(localdata)})
else .then(() =>
{
showtemporaryinfo("Cannot push empty data");
}
}
else
{ {
console.log("...data saved on server");
saved = true; saved = true;
} })
}); .catch(remotecallfailed)
.finally(() =>
{
pending = false;
if (content != getnotecontent())
{
console.log("but content changed: will save again");
datachanged();
}
else if (!saved)
{
console.log("save failed. Data unsaved on server, manual action required.");
}
});
}
else
{
saved = true;
}
} }
function datachanged() function datachanged()