|
|
|
@ -6,15 +6,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function gatherSyncData() {
|
|
|
|
|
if (getStorage("syncstateversion") == null) {
|
|
|
|
|
setStorage("syncstateversion", 0);
|
|
|
|
|
}
|
|
|
|
|
var data = {
|
|
|
|
|
localStorage: {},
|
|
|
|
|
changed: getStorage("lastchange") == null ? 0 : getStorage("lastchange"),
|
|
|
|
|
stateversion: getStorage("syncstateversion") == null ? 0 : getStorage("syncstateversion")
|
|
|
|
|
};
|
|
|
|
|
if (!inStorage("lastsync")) {
|
|
|
|
|
// first time syncing to the server, let's make sure
|
|
|
|
|
// the server settings take precedence
|
|
|
|
|
data.changed = 1;
|
|
|
|
|
}
|
|
|
|
|
var allitems = getAllStorage();
|
|
|
|
|
for (var i = 0; i < allitems.length; i++) {
|
|
|
|
|
var key = allitems[i].key;
|
|
|
|
@ -27,33 +25,30 @@ function gatherSyncData() {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncDataToLocalStorage(data) {
|
|
|
|
|
function syncDataToLocalStorage(data, stateversion) {
|
|
|
|
|
for (var key in data.localStorage) {
|
|
|
|
|
if (data.localStorage.hasOwnProperty(key)) {
|
|
|
|
|
setStorage(key, data.localStorage[key], true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setStorage("syncstateversion", stateversion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveSync(remotedata) {
|
|
|
|
|
var localchangetime = getStorage("lastchange");
|
|
|
|
|
if (remotedata.changed == null) {
|
|
|
|
|
// The server has nothing, this is the first sync
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (localchangetime == null) {
|
|
|
|
|
// No local setting changes but since we've gotten this far,
|
|
|
|
|
// the server has stuff for us
|
|
|
|
|
syncDataToLocalStorage(remotedata);
|
|
|
|
|
function resolveSync(remotedata, remotestateversion) {
|
|
|
|
|
var localstateversion = getStorage("syncstateversion");
|
|
|
|
|
console.log("Resolving sync: remote state: " + remotestateversion, "local state: " + localstateversion);
|
|
|
|
|
if (localstateversion == remotestateversion) {
|
|
|
|
|
// Server and client both have same version
|
|
|
|
|
console.log("Sync: server is same as client");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (localchangetime < remotedata.changed) {
|
|
|
|
|
// The server has newer stuff for us
|
|
|
|
|
syncDataToLocalStorage(remotedata);
|
|
|
|
|
} else if (Number(localstateversion) < Number(remotestateversion)) {
|
|
|
|
|
// Server has newer version
|
|
|
|
|
console.log("Sync: server is newer than client");
|
|
|
|
|
syncDataToLocalStorage(remotedata, remotestateversion);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (localchangetime >= remotedata.changed) {
|
|
|
|
|
// Our local data is newer or the same as the server copy
|
|
|
|
|
} else {
|
|
|
|
|
// We have the newer version
|
|
|
|
|
console.log("Sync: client is newer than server.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
@ -70,10 +65,11 @@ function syncNow(callback) {
|
|
|
|
|
$.post(SETTINGS.syncapi, {
|
|
|
|
|
username: username,
|
|
|
|
|
password: password,
|
|
|
|
|
data: JSON.stringify(data)
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
stateversion: data.stateversion
|
|
|
|
|
}, function (resp) {
|
|
|
|
|
if (resp.status == "OK") {
|
|
|
|
|
resolveSync(resp.data);
|
|
|
|
|
resolveSync(resp.data, resp.stateversion);
|
|
|
|
|
setStorage("lastsync", Date.now() / 1000);
|
|
|
|
|
if (typeof callback == "function") {
|
|
|
|
|
callback();
|
|
|
|
|