|
|
|
|
@ -10,7 +10,8 @@ function fetchRemote() {return authHeaders().then(function(h){return fetch(BU+"/
@@ -10,7 +10,8 @@ function fetchRemote() {return authHeaders().then(function(h){return fetch(BU+"/
|
|
|
|
|
function putRemote(s) {return authHeaders().then(function(h){return fetch(BU+"/"+"memory/state",{method:"PUT",headers:h,body:JSON.stringify(s)})}).then(function(r){if(!r.ok)throw new Error("PUT "+r.status);return r.json()}).then(function(j){return j&&j.ok})}; |
|
|
|
|
|
|
|
|
|
function newer(a,b) {return (a&&a.updatedAt?a.updatedAt:"") >= (b&&b.updatedAt?b.updatedAt:"")}; |
|
|
|
|
function sync() {return Promise.all([loadLocal(),fetchRemote().catch(function(){return null})]).then(function(arr){var loc=arr[0],rem=arr[1];if(!rem)return loc;if(newer(loc,rem)){putRemote(loc).catch(function(){});return loc};return saveLocal(rem).then(function(){return rem})})}; |
|
|
|
|
function isEmpty(s) {return !s || ((s.memories ? Object.keys(s.memories).length : 0) + (s.skills ? s.skills.length : 0) === 0)}; |
|
|
|
|
function sync() {return Promise.all([loadLocal(),fetchRemote().catch(function(){return null})]).then(function(arr){var loc=arr[0],rem=arr[1];if(!rem)return loc;var le=isEmpty(loc),re=isEmpty(rem);if(le!==re){if(le){return saveLocal(rem).then(function(){return rem})}putRemote(loc).catch(function(){});return loc};if(newer(loc,rem)){putRemote(loc).catch(function(){});return loc};return saveLocal(rem).then(function(){return rem})})}; |
|
|
|
|
|
|
|
|
|
function getState() {return sync()}; |
|
|
|
|
function putState(s) {var v={version:1,updatedAt:new Date().toISOString(),memories:s.memories||{},skills:s.skills||[]};return saveLocal(v).then(function(){return putRemote(v).catch(function(){return false})}).then(function(){return v})}; |
|
|
|
|
|