diff --git a/main.js b/main.js index d8462d6..d576e91 100644 --- a/main.js +++ b/main.js @@ -1291,6 +1291,15 @@ function pushall() return Promise.all(list); } +function fetchandserialize(guid) +{ + return queryremote({action: "fetch", name: guid}) + .then(content => + { + localStorage.setItem(guid, content); + }); +} + function fetch() { return new Promise(function(resolve) @@ -1311,26 +1320,42 @@ function fetch() { if (!filecontent) { - return pushall(); + pushall().then(resolve); } else { - // compare and fetch modified and serialize all that - } + var localindex = JSON.parse(localStorage.getItem("index")); + var remoteindex = JSON.parse(filecontent); - }).catch(err => + var list = []; + Object.keys(remoteindex).forEach(guid => + { + if (!localindex[guid] || localindex[guid].lastchanged < remoteindex[guid].lastchanged) + { + list.push(fetchandserialize(guid)); + } + }); + + Promise.all(list).then( () => + { + localStorage.setItem("index", JSON.stringify(remoteindex)); + resolve(); + }); + } + }) + .catch(err => + { + if (err == "error: authent") { - if (err == "error: authent") - { - settings.password = prompt("Password: ", settings.password); - savesettings(); - init(); - } - else - { - showtemporaryinfo(err); - } - }); + settings.password = prompt("Password: ", settings.password); + savesettings(); + init(); + } + else + { + showtemporaryinfo(err); + } + }); } } else