Skip to content

Instantly share code, notes, and snippets.

@NikitaObukhov
Created January 12, 2016 18:33
Show Gist options
  • Save NikitaObukhov/cd2655464d9bdf37ac45 to your computer and use it in GitHub Desktop.
Save NikitaObukhov/cd2655464d9bdf37ac45 to your computer and use it in GitHub Desktop.
var prev = 'prev' in found ? found.prev : _.clone(found);
var threeWayMerge = function(a, b, c) {
var d = a || {};
_.forEach(b, function(value, key) {
if (_.isArray(value) || _.isObject(value)) {
d[key] = threeWayMerge(a[key], b[key], c[key]);
}
else {
if (a && c && a[key] == c[key]) {
d[key] = b[key];
}
else if (c && b[key] == c[key]) {
d[key] = a[key];
}
else {
// conflict: prefer online version (b)
d[key] = b[key];
}
}
});
return d;
};
var orig = _.clone(found);
// found - визит в оффлайне, visit - визит из онлайне, prev - прошлая версия визита
found = threeWayMerge(found, visit, prev);
found.prev = orig;
promises.push(db.update('visits', {'data': JSON.stringify(found)}, {
'id': found.id
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment