changed: saving strategy. Use an 5-sec interval worker

This commit is contained in:
quenousimporte 2023-01-22 21:17:23 +01:00
parent d03ded0762
commit 1f9a2d303c
1 changed files with 20 additions and 32 deletions

52
main.js
View File

@ -1,6 +1,6 @@
var defaultsettings = var defaultsettings =
{ {
savedelay: 1000, savedelay: 5000,
bgcolor: "white", bgcolor: "white",
fontfamily: "'Inconsolata', 'Consolas', monospace", fontfamily: "'Inconsolata', 'Consolas', monospace",
fontsize: "90%", fontsize: "90%",
@ -21,7 +21,7 @@ var codelanguages = ["xml", "js", "sql"];
// globals // globals
var currentnote = null; var currentnote = null;
var fileindex = 0; var fileindex = 0;
var timeoutid = null; var workerid = null;
var folds = []; var folds = [];
var backup = ""; var backup = "";
var localdata = null; var localdata = null;
@ -549,15 +549,6 @@ function downloadnote()
download(currentnote.title + ".md", getnotecontent()); download(currentnote.title + ".md", getnotecontent());
} }
function delay()
{
return new Promise(function(apply)
{
clearTimeout(timeoutid);
timeoutid = setTimeout(apply, settings.savedelay);
});
}
function save() function save()
{ {
var content = getnotecontent(); var content = getnotecontent();
@ -696,6 +687,17 @@ function initvault()
currentvault = window.localStorage.getItem("vault") || "local"; currentvault = window.localStorage.getItem("vault") || "local";
} }
function startworker()
{
workerid = setInterval(function()
{
if (!saved)
{
save();
}
}, settings.savedelay);
}
function init() function init()
{ {
loadsettings(); loadsettings();
@ -713,15 +715,16 @@ function init()
queryremote({action: "fetch"}) queryremote({action: "fetch"})
.then(data => .then(data =>
{ {
marksaved();
localdata = data; localdata = data;
loadlast(); loadlast();
startworker();
}) })
.catch(remotecallfailed); .catch(remotecallfailed);
} }
else else
{ {
loadstorage(); loadstorage();
startworker();
} }
if (issplit()) if (issplit())
@ -749,16 +752,11 @@ function togglepassword()
function pushtoremote() function pushtoremote()
{ {
if (!isremote())
{
console.log("local vault, no push");
return;
}
console.log("sending data to php server"); console.log("sending data to php server");
markunsaved("⇅");
if (localdata) if (localdata)
{ {
clearTimeout(timeoutid);
var content = getnotecontent(); var content = getnotecontent();
queryremote({action: "push", data: JSON.stringify(localdata)}) queryremote({action: "push", data: JSON.stringify(localdata)})
.then(() => { .then(() => {
@ -770,7 +768,7 @@ function pushtoremote()
else else
{ {
console.warn("Content changed in the meantime, keep as unsaved"); console.warn("Content changed in the meantime, keep as unsaved");
save(); markunsaved("*");
} }
}) })
.catch(remotecallfailed); .catch(remotecallfailed);
@ -1278,7 +1276,7 @@ function resize()
{ {
if (md.clientHeight > md.scrollHeight) return; if (md.clientHeight > md.scrollHeight) return;
console.log("resize"); //console.log("resize");
md.style.height = md.scrollHeight + 'px'; md.style.height = md.scrollHeight + 'px';
/*md.rows = (md.value.match(/\n/g) || []).length + 1; /*md.rows = (md.value.match(/\n/g) || []).length + 1;
@ -1300,16 +1298,7 @@ function putontop()
function notecontentchanged() function notecontentchanged()
{ {
resize(); resize();
markunsaved("⇅"); markunsaved("*");
if (isremote())
{
delay().then(save);
}
else
{
save();
}
} }
function loadtodo() function loadtodo()
@ -1417,8 +1406,7 @@ function rename(newname)
currentnote.title = newname; currentnote.title = newname;
markunsaved("⇅"); markunsaved("*");
save();
return ""; return "";
} }