refactor: promisize save

This commit is contained in:
quenousimporte 2024-01-08 10:55:12 +01:00
parent b800debd2f
commit 3ffacd309d
1 changed files with 87 additions and 84 deletions

57
main.js
View File

@ -34,7 +34,6 @@ var pending = false;
var settings = null;
var tags = null;
var titlemap = {};
var clip = "";
var stat =
{
@ -1199,7 +1198,7 @@ function loadstorage()
var title = params.get("n");
var line = params.get("l");
var tags = params.get("t");
clip = params.get("c");
var clip = params.get("c");
if (clip)
{
@ -1224,9 +1223,8 @@ function loadstorage()
notepage.appendChild(msg);
saved = false;
save();
return;
return save()
.then(window.close);
}
if (currentnote)
@ -1858,14 +1856,12 @@ function setsaved()
unsavedmark.hidden = true;
saved = true;
lastsaved = timestamp();
if (clip)
{
close();
}
}
function save()
{
return new Promise(function(resolve, reject)
{
clearTimeout(workerid);
if (currentnote.title == "settings.json")
@ -1874,40 +1870,39 @@ function save()
savesettings();
loadsettings();
setsaved();
return;
resolve();
}
else if (currentnote.title == "pgpkeys")
{
localStorage.setItem("pgpkeys", md.value);
setsaved();
return;
resolve();
}
if (!localdata)
else if (!localdata)
{
showtemporaryinfo("cannot push empty data");
return;
reject();
}
if (pending)
else if (pending)
{
console.log("pending query: save cancelled");
return;
reject();
}
if (saved)
else if (saved)
{
console.log("nothing to save");
return;
reject();
}
else
{
var content = md.value;
if ((content == "" && backup != "") || content == "null" || content == "undefined")
{
showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
return;
reject();
}
else
{
currentnote.pos = md.selectionStart;
currentnote.content = content;
putontop();
@ -1917,7 +1912,7 @@ function save()
if (settings.sync)
{
var datatosend = JSON.stringify(localdata);
encryptstring(datatosend)
return encryptstring(datatosend)
.then(encrypted =>
{
console.log("sending data to php server...");
@ -1936,19 +1931,27 @@ function save()
if (content != md.value)
{
console.log("but content changed: will save again");
datachanged();
return datachanged();
}
else if (!saved)
{
console.log("save failed. Data unsaved on server. Will retry.");
datachanged();
return datachanged();
}
else
{
resolve();
}
});
}
else
{
setsaved();
resolve();
}
}
}
});
}
function escapeHtml(unsafe) {
@ -2220,7 +2223,7 @@ function datachanged()
saved = false;
unsavedmark.hidden = !settings.sync;
postpone()
return postpone()
.then(save);
}