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

60
main.js
View File

@ -1348,38 +1348,23 @@ 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(() =>
{
var content = getnotecontent();
if (!localdata)
{
showtemporaryinfo("cannot push empty data");
return;
}
if (pending)
{
console.log("pending query: save cancelled");
return;
}
var content = getnotecontent();
if ((content == "" && backup != "") || content == "null" || content == "undefined") if ((content == "" && backup != "") || content == "null" || content == "undefined")
{ {
showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved"); showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
@ -1390,6 +1375,7 @@ function save()
currentnote.content = content; currentnote.content = content;
window.localStorage.setItem(currentvault, JSON.stringify(localdata)); window.localStorage.setItem(currentvault, JSON.stringify(localdata));
if (currentnote.title == "settings.json") if (currentnote.title == "settings.json")
{ {
settings = JSON.parse(content); settings = JSON.parse(content);
@ -1401,31 +1387,33 @@ function save()
{ {
console.log("sending data to php server..."); console.log("sending data to php server...");
if (localdata)
{
pending = true; pending = true;
queryremote({action: "push", data: JSON.stringify(localdata)}) queryremote({action: "push", data: JSON.stringify(localdata)})
.then(() => .then(() =>
{ {
console.log("data saved on server."); console.log("...data saved on server");
saved = true; saved = true;
}) })
.catch(remotecallfailed) .catch(remotecallfailed)
.finally(() => .finally(() =>
{ {
pending = false; pending = false;
}); if (content != getnotecontent())
}
else
{ {
showtemporaryinfo("Cannot push empty data"); console.log("but content changed: will save again");
datachanged();
} }
else if (!saved)
{
console.log("save failed. Data unsaved on server, manual action required.");
}
});
} }
else else
{ {
saved = true; saved = true;
} }
});
} }
function datachanged() function datachanged()