Yomguithereal / baobab

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

Force monkey recalculation #515

Closed savolkov closed 2 years ago

savolkov commented 2 years ago

Hey!

It so happened that I need to make monkey to recalculate value on data change outside the tree.

Example:

fromJohn: monkey(
  ['data', 'messages'],
  function(messages) {
    const externalValue = ExternalService.getValue()
     return messages.filter(function(m) {
      return m.from === 'John' || externalValue;
});

In my case it is possible to fire events in case of outer data change, however I can't find the proper way to trigger monkey recalculation.

What is the proper way to do it?

Yomguithereal commented 2 years ago

There is a hidden method _refreshMonkeys defined here that you can try. It's possible to call it without arguments to force all of the tree to refresh if I remember correctly. Or you can read the code and see how to refresh one particular monkey.

savolkov commented 2 years ago

@Yomguithereal Thanks a lot! Didn't manage to find it on my own.

I'll write back with solution if I'll find it

savolkov commented 2 years ago

Didn't work for me, but I've managed to get to monkey's update() method and it worked. Thanks!