Add better offline sync support
parent
3050862c7f
commit
022ff4d113
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* The code in this file is by StackOverflow user Juan Mendes.
|
||||||
|
* License: Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0).
|
||||||
|
* Source: https://stackoverflow.com/a/14966749
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a map out of an array be choosing what property to key by
|
||||||
|
* @param {object[]} array Array that will be converted into a map
|
||||||
|
* @param {string} prop Name of property to key by
|
||||||
|
* @return {object} The mapped array. Example:
|
||||||
|
* mapFromArray([{a:1,b:2}, {a:3,b:4}], 'a')
|
||||||
|
* returns {1: {a:1,b:2}, 3: {a:3,b:4}}
|
||||||
|
*/
|
||||||
|
function mapFromArray(array, prop) {
|
||||||
|
var map = {};
|
||||||
|
for (var i = 0; i < array.length; i++) {
|
||||||
|
map[ array[i][prop] ] = array[i];
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object[]} o old array of notes (local copy)
|
||||||
|
* @param {object[]} n new array of notes (remote copy)
|
||||||
|
* @param {object} An object with changes
|
||||||
|
*/
|
||||||
|
function getDelta(o, n) {
|
||||||
|
var delta = {
|
||||||
|
addedRemote: [],
|
||||||
|
addedLocal: [],
|
||||||
|
deletedRemote: [],
|
||||||
|
deletedLocal: [],
|
||||||
|
changedRemote: [],
|
||||||
|
changedLocal: [],
|
||||||
|
noChange: []
|
||||||
|
};
|
||||||
|
oSane = [];
|
||||||
|
for (var i = 0; i < o.length; i++) {
|
||||||
|
if (o[i].noteid == null) { // Note has no real `noteid`
|
||||||
|
delta.addedLocal.push(o[i]);
|
||||||
|
} else {
|
||||||
|
oSane.push(o[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var local = mapFromArray(oSane, 'noteid');
|
||||||
|
var remote = mapFromArray(n, 'noteid');
|
||||||
|
|
||||||
|
for (var id in local) {
|
||||||
|
if (!remote.hasOwnProperty(id)) { // Notes that are only present locally
|
||||||
|
delta.addedLocal.push(local[id]);
|
||||||
|
// TODO: Figure out which notes were actually added locally and which were deleted on the server
|
||||||
|
/*if (local[id].norealid) { // Note hasn't been synced to the remote yet
|
||||||
|
delta.addedLocal.push(local[id]);
|
||||||
|
} else { // Note has been synced to remote but isn't there anymore
|
||||||
|
delta.deletedRemote.push(local[id]);
|
||||||
|
}*/
|
||||||
|
} else { // Notes that are present on both
|
||||||
|
if (local[id].modified > remote[id].modified) { // Local copy is newer
|
||||||
|
delta.changedLocal.push(local[id]);
|
||||||
|
} else if (local[id].modified < remote[id].modified) { // Remote copy is newer
|
||||||
|
delta.changedRemote.push(remote[id]);
|
||||||
|
} else { // Modified date is same, let's check content
|
||||||
|
if (local[id].content == remote[id].content) {
|
||||||
|
delta.noChange.push(local[id]);
|
||||||
|
} else if (local[id].content.length > remote[id].content.length) {
|
||||||
|
delta.changedLocal.push(local[id]);
|
||||||
|
} else {
|
||||||
|
delta.changedRemote.push(remote[id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add notes that are only on the remote
|
||||||
|
for (var id in remote) {
|
||||||
|
if (!local.hasOwnProperty(id)) {
|
||||||
|
delta.addedRemote.push(remote[id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return delta;
|
||||||
|
}
|
Loading…
Reference in New Issue