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