Created
January 12, 2016 18:33
-
-
Save NikitaObukhov/cd2655464d9bdf37ac45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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