tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-10-10 16:07:38 +03:00
parent c310b230ac
commit d72c2a2e15

61
diff.js
View File

@ -1506,6 +1506,9 @@ module.Types = {
// XXX this will produce a flat result out of the box... // XXX this will produce a flat result out of the box...
// XXX this eliminates the need for .flatten(..) // XXX this eliminates the need for .flatten(..)
// XXX do we need context??? // XXX do we need context???
// XXX Q: will this blow up on recursive objects???
// I think no... (needs testing)
// ...do we need a tree/recursive format to support object recursion???
_diff: function(A, B, options, context){ _diff: function(A, B, options, context){
options = options || {} options = options || {}
var that = this var that = this
@ -1528,13 +1531,42 @@ module.Types = {
options.cmp = cmp options.cmp = cmp
// make change path updater...
//
// returns a function:
// - create a copy of the change
// - concatenate change.path to base
var updatePath = function(base){
return function(e){
return Object.assign({},
e,
{ path: base.concat(e.path) }) } }
return walk( return walk(
function(diff, node, next, stop){ function(diff, node, next, stop){
var path = node[0] var path = node[0]
var A = node[1] var A = node[1]
var B = node[2] var B = node[2]
var cache = this.cache = this.cache || new Map() // cache format:
// Map([
// [<obj_a>, Map([
// // <obj_a> and <obj_b0> match...
// [<obj_b0>, true],
// // <obj_a> and <obj_b1> do not match...
// [<obj_b1>, [
// // relative changes...
// // NOTE: change.path is relative to obj_a
// // and may need to be updated to
// // reflect the actual change in A/B...
// <change>,
// ...
// ]],
// ...
// ])],
// ...
// ])
var cache = this.cache = this.cache || context.cache || new Map()
var cache_l2 = cache.get(A) || new Map() var cache_l2 = cache.get(A) || new Map()
// uncached compare... // uncached compare...
@ -1543,11 +1575,11 @@ module.Types = {
if(!cache_l2.has(B)){ if(!cache_l2.has(B)){
// we have a match -> no changes, just cache... // we have a match -> no changes, just cache...
if(cmp(A, B)){ if(cmp(A, B)){
cache.set(A, cache_l2.set(B, false)) cache.set(A, cache_l2.set(B, undefined))
return return
} }
// get the handler... // handler...
var handler = that.get( var handler = that.get(
(that.DIFF_TYPES.has(A) || that.DIFF_TYPES.has(B)) ? (that.DIFF_TYPES.has(A) || that.DIFF_TYPES.has(B)) ?
'Basic' 'Basic'
@ -1566,16 +1598,21 @@ module.Types = {
throw new TypeError('Diff: can\'t handle: ' + type) throw new TypeError('Diff: can\'t handle: ' + type)
} }
cache.set(A, cache_l2.set(B, true))
// call the handler... // call the handler...
return diff var res = handler.call(that, A, B, next, options)
.concat(handler.call(that, A, B, next, options))
// update paths... cache.set(A, cache_l2.set(B, res))
.map(function(e){
e.path = path.concat(e.path) return diff.concat(res
return e .map(updatePath(path)))
})
// return the cached values...
} else {
var res = cache_l2.get(B)
return res == null ?
res
: diff.concat(res
.map(updatePath(path)))
} }
}, },
// diff... // diff...