Yomguithereal / baobab

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

Lazy monkies arnt so lazy #400

Closed tonypee closed 9 years ago

tonypee commented 9 years ago

It seems that the monkies are a little too fit.

const tree2 = new Baobab({
  test: monkey(
      ['items'],
      function(items) {
        console.log('monkey eval');
      }
  ),
  items: []
});

var cursor = tree2.select('items');
cursor.push(1);
cursor.push(2);
cursor.push(3);

// output:  
// state.js:34 monkey eval 
// state.js:34 monkey eval
// state.js:34 monkey eval

So, the monkey is evaluated 3 times. It should only be on access no?

SaphuA commented 9 years ago

The docs on the front page say:

Which makes it seem the behavior you get is as expected. But the docs also say:

Which seems to be a bit conflicting.

Perhaps @Yomguithereal can elaborate on what the expected behavior should be and update the docs accordingly?

markuplab commented 9 years ago

"By access" is very bad way, because you can have many ui sources that use monkey fields. But every mutation recalculation is also bad, i think it's can be recalculated on end of transactions (nextTick), or on sync read, if it's not cached.

SaphuA commented 9 years ago

@markuplab I'm pretty sure there is a system in place that prevents it from needlessly recalculating.

Yomguithereal commented 9 years ago

Okay, so let me explain what is the expected behavior of the monkeys:

If this is not what you observe, and @tonypee's example seems to pinpoint this exactly, then there is a bug I must fix.

SaphuA commented 9 years ago

@Yomguithereal it seems that your last point...

...is not the case in @tonypee's example. It seems to be recalculated every time a push to the watched path is done.

@tonypee can you provide a repro repo?

Yomguithereal commented 9 years ago

If the test case presented by @tonypee is full, no need for a repo. If this is all there is to such example then nothing should be logged to the console. So there indeed seems to be a bug.

Yomguithereal commented 9 years ago

Ok. I've pinpointed the source of the bug I think. Patch incoming.

tonypee commented 9 years ago

perfect - thanks!

Yomguithereal commented 9 years ago

Will need to fix some other things but a 2.2.0 is definitely coming soon.

Yomguithereal commented 9 years ago

Closing this then.