Yomguithereal / baobab

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

Cursor events and many of monkey's #428

Open eugeny-dementev opened 8 years ago

eugeny-dementev commented 8 years ago

Hello @Yomguithereal! I get some silence from cursors when make some crazy monkey tree.

var Baobab = require('baobab');
var _ = require('lodash');

var tree = new Baobab({
  branch: {
    current: {
      name: 'hello'
    },
    changed: {},
    computed: { 
      name: Baobab.monkey(
        ['branch', 'current', 'name'],
        ['branch', 'changed', 'name'],
        (current, changed) => _.isUndefined(changed) ? current : changed
      )
    },
    diff: {
      name: Baobab.monkey(
        ['branch', 'changed', 'name'],
        (name) => {
          if (!_.isUndefined(name)) return name;
        }
      )
    },
    isHasDiff: Baobab.monkey(
      ['branch','diff'],
      (diff) => !_.chain(diff)
        .omit(_.isUndefined)
        .isEmpty()
        .value()
    )
  },
  diff: {
    branch: Baobab.monkey(
      ['branch', 'diff'],
      (diff) => _.omit(diff, _.isUndefined)
    )
  },
  isHasDiff: Baobab.monkey(
    ['branch', 'isHasDiff'],
    (isHasDiff) => isHasDiff
  ),
  controls: {
    save: {
      isEnabled: Baobab.monkey(
        ['isHasDiff'],
        (isHasDiff) => isHasDiff
      )
    }
  }
});

var isEnabled = tree.select(['controls', 'save', 'isEnabled']);
var diff = tree.select(['diff', 'branch', 'name']);
var isHasDiff = tree.select(['isHasDiff']);

isEnabled.on('update', () => console.log('isEnabled update')); // not emitted 
isHasDiff.on('update', () => console.log('isHasDiff update')); // not emitted
diff.on('update', () => console.log('diff update')); // not emitted

console.log('isEnabled', isEnabled.get()); // false
console.log('isHasDiff', isHasDiff.get()); // false
console.log('diff', diff.get()); // undefined

tree.set(['branch', 'changed', 'name'], 'someValue');

console.log('isEnabled', isEnabled.get()); // true
console.log('isHasDiff', isHasDiff.get()); // true
console.log('diff', diff.get()); // someValue

All field have been calculated, but no any event was emitted.

Yomguithereal commented 8 years ago

Hello @eugeny-dementev. I will need some time to test this and see where this went bad.

markuplab commented 8 years ago

Any updates on this?

Yomguithereal commented 8 years ago

Ok. The problem here is that your monkeys listen to paths which are the parent of the monkeys and not the paths of the monkeys or below that. There might be a solution but I am not sure yet this would be wise for performance.

markuplab commented 8 years ago

Any progress on this?

Rendalf commented 8 years ago

It works correctly when lazyMonkeys is enabled