Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 117 forks source link

All non-monkey keys are lost during merge when monkey present #430

Closed abalmos closed 8 years ago

abalmos commented 8 years ago

All non-monkey keys that have the same parent path as a monkey are lost during a merge.

Here is a simple example:

var Baobab = require('baobab');
var Monkey = Baobab.monkey;

var baobab = new Baobab({
  cart: {
    test: {},
    items: [{price: 10}, {price: 20}],
    total: Monkey(['.', 'items'], items => items.reduce((total, item) => total+item.price, 0))
  },
  cart2: {
    items: [{price: 10}, {price: 20}]
  }
});

console.log(baobab.get(['cart', 'total'])); // 30
baobab.merge([], {});
console.log(baobab.get());  // { cart: { total: [Getter] }, cart2: { items: [ [Object], [Object] ] } }
                            // Note ['cart', 'items'], and ['cart', 'test'] are lost
console.log(baobab.get(['cart', 'total'])); // Error because of missing ['cart', 'items']
Yomguithereal commented 8 years ago

Hello @abalmos. This is because you are using merge rather than deepMerge. The merge command is shallow.

Yomguithereal commented 8 years ago

Ok, let me check this more thoroughly.

Yomguithereal commented 8 years ago

@Zache @abalmos this issue should be fixed now. The solution is not completely optimal perf wise but it shouldn't have an impact anyway (quite the same solution as the one in #430).

tnrich commented 8 years ago

+1 thanks everyone for getting this one solved!