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

130
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; var content = getnotecontent();
currentnote.content = content; if ((content == "" && backup != "") || content == "null" || content == "undefined")
{
window.localStorage.setItem(currentvault, JSON.stringify(localdata)); showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
if (currentnote.title == "settings.json") return;
{ }
settings = JSON.parse(content);
savesettings(); currentnote.pos = md.selectionStart;
} currentnote.content = content;
console.log("data serialized in local storage")
window.localStorage.setItem(currentvault, JSON.stringify(localdata));
if (isremote())
{ if (currentnote.title == "settings.json")
console.log("sending data to php server..."); {
settings = JSON.parse(content);
if (localdata) savesettings();
{ }
pending = true; console.log("data serialized in local storage")
queryremote({action: "push", data: JSON.stringify(localdata)})
.then(() => if (isremote())
{ {
console.log("data saved on server."); console.log("sending data to php server...");
saved = true;
}) pending = true;
.catch(remotecallfailed) queryremote({action: "push", data: JSON.stringify(localdata)})
.finally(() => .then(() =>
{
pending = false;
});
}
else
{
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()