From f75c7b6bd2b3fe9f2ce77cf26d643bc7de516986 Mon Sep 17 00:00:00 2001 From: quenousimporte <76260127+quenousimporte@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:32:00 +0100 Subject: [PATCH] changed: save strategy! again! --- main.js | 124 +++++++++++++++++++++++++------------------------------- 1 file changed, 56 insertions(+), 68 deletions(-) diff --git a/main.js b/main.js index 875328a..602a82a 100644 --- a/main.js +++ b/main.js @@ -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() { clearTimeout(workerid); - waitpending() - .then(() => + + if (!localdata) { - var content = getnotecontent(); + showtemporaryinfo("cannot push empty data"); + return; + } - if ((content == "" && backup != "") || content == "null" || content == "undefined") - { - showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved"); - return; - } - - currentnote.pos = md.selectionStart; - currentnote.content = content; + if (pending) + { + console.log("pending query: save cancelled"); + return; + } - window.localStorage.setItem(currentvault, JSON.stringify(localdata)); - if (currentnote.title == "settings.json") - { - settings = JSON.parse(content); - savesettings(); - } - console.log("data serialized in local storage") + var content = getnotecontent(); + if ((content == "" && backup != "") || content == "null" || content == "undefined") + { + showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved"); + return; + } - if (isremote()) - { - console.log("sending data to php server..."); + currentnote.pos = md.selectionStart; + currentnote.content = content; - if (localdata) - { - pending = true; - queryremote({action: "push", data: JSON.stringify(localdata)}) - .then(() => - { - console.log("data saved on server."); - saved = true; - }) - .catch(remotecallfailed) - .finally(() => - { - pending = false; - }); - } - else - { - showtemporaryinfo("Cannot push empty data"); - } - } - else + window.localStorage.setItem(currentvault, JSON.stringify(localdata)); + + if (currentnote.title == "settings.json") + { + settings = JSON.parse(content); + savesettings(); + } + console.log("data serialized in local storage") + + if (isremote()) + { + console.log("sending data to php server..."); + + pending = true; + queryremote({action: "push", data: JSON.stringify(localdata)}) + .then(() => { + console.log("...data saved on server"); 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()