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

171
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,97 +1856,102 @@ function setsaved()
unsavedmark.hidden = true; unsavedmark.hidden = true;
saved = true; saved = true;
lastsaved = timestamp(); lastsaved = timestamp();
if (clip)
{
close();
}
} }
function save() function save()
{ {
clearTimeout(workerid); return new Promise(function(resolve, reject)
if (currentnote.title == "settings.json")
{ {
settings = JSON.parse(md.value); clearTimeout(workerid);
savesettings();
loadsettings();
setsaved();
return;
}
else if (currentnote.title == "pgpkeys")
{
localStorage.setItem("pgpkeys", md.value);
setsaved();
return;
}
if (!localdata) if (currentnote.title == "settings.json")
{
showtemporaryinfo("cannot push empty data");
return;
}
if (pending)
{
console.log("pending query: save cancelled");
return;
}
if (saved)
{
console.log("nothing to save");
return;
}
var content = md.value;
if ((content == "" && backup != "") || content == "null" || content == "undefined")
{
showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
return;
}
currentnote.pos = md.selectionStart;
currentnote.content = content;
putontop();
window.localStorage.setItem("data", JSON.stringify(localdata));
if (settings.sync)
{
var datatosend = JSON.stringify(localdata);
encryptstring(datatosend)
.then(encrypted =>
{ {
console.log("sending data to php server..."); settings = JSON.parse(md.value);
pending = true; savesettings();
return queryremote({action: "push", data: encrypted}) loadsettings();
})
.then(() =>
{
console.log("...data saved on server");
setsaved(); setsaved();
}) resolve();
.catch(remotecallfailed) }
.finally(() => else if (currentnote.title == "pgpkeys")
{ {
pending = false; localStorage.setItem("pgpkeys", md.value);
if (content != md.value) setsaved();
resolve();
}
else if (!localdata)
{
showtemporaryinfo("cannot push empty data");
reject();
}
else if (pending)
{
console.log("pending query: save cancelled");
reject();
}
else if (saved)
{
console.log("nothing to save");
reject();
}
else
{
var content = md.value;
if ((content == "" && backup != "") || content == "null" || content == "undefined")
{ {
console.log("but content changed: will save again"); showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
datachanged(); reject();
} }
else if (!saved) else
{ {
console.log("save failed. Data unsaved on server. Will retry."); currentnote.pos = md.selectionStart;
datachanged(); currentnote.content = content;
putontop();
window.localStorage.setItem("data", JSON.stringify(localdata));
if (settings.sync)
{
var datatosend = JSON.stringify(localdata);
return encryptstring(datatosend)
.then(encrypted =>
{
console.log("sending data to php server...");
pending = true;
return queryremote({action: "push", data: encrypted})
})
.then(() =>
{
console.log("...data saved on server");
setsaved();
})
.catch(remotecallfailed)
.finally(() =>
{
pending = false;
if (content != md.value)
{
console.log("but content changed: will save again");
return datachanged();
}
else if (!saved)
{
console.log("save failed. Data unsaved on server. Will retry.");
return datachanged();
}
else
{
resolve();
}
});
}
else
{
setsaved();
resolve();
}
} }
}); }
} });
else
{
setsaved();
}
} }
function escapeHtml(unsafe) { function escapeHtml(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);
} }