Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Merge and update event: excessive event handler's calls

ruscoder opened this issue · comments

I found the odd behavior of update handler and merge.

Example:

const tree = new Baobab();
tree.set('test', {});
tree.select('test', 'value').set(1);

// Add update event handler
tree.select('test', 'value').on('update', (e) => console.log('update:', e.data.currentData, e.data.previousData)); 


tree.select('test', 'value').set(2)
// Update handler's output: update: 2 1

tree.select('test', 'value').set(2)
// Update handler wasn't called

tree.select('test', 'value').set(3)
// Update handler's output: update: 3 2

tree.select('test', 'anothervalue').set(1);
// Update handler wasn't called because it is another cursor

Before, everything was OK. Update handler was called only when data was changed.

tree.select('test').merge({ 'anothervalue': 1});
// Update handler's output: update: 3 3

tree.select('test').merge({ 'anothervalue': 1});
// Update handler's output: update: 3 3

I think it is bad behavior because data wasn't changed and cursor ['test', 'value'] always had the same value.

What do you think about this?

I am not sure to understand your case but what you need to know is that Baobab never perform data diff for performance reasons but instead compare paths. In the merge case, you edit the parent path, so I cannot infer that the underlying values have change or not, hence the event.

But I might change some handler strategies in the future.