Yomguithereal / baobab

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unify output from tree events and cursor events

Gby56 opened this issue · comments

Hi !
I was playing around with the library and my use-case is using baobab on this stucture :

const test = {
    "model": {
        "name": "foo",
        "list1": [{
                "prop1": "hey"
            },
            {
                "prop1": "ho"
            }
        ],
        "list2": [{
                "prop2": "bye"
            },
            {
                "prop2": "seeya"
            }
        ]
    }
}

Let's create the tree like so
const tree = new Baobab(test);
If I attach my watcher on tree like so :

	tree.on('update', (e) => {
		const eventData = e.data;
	});

In eventData I get previous, currentData, AND paths, and transactions !

Is it possible to retrieve such info from a cursor set directly on a nested value ?, such as :

	const cursor = tree.select('model', 'list1');

In the first case, the transaction gives the actual path of the event, giving the index of the targeted object of an array
In the second case, we only get the path of the cursor, not the affected element in the cursor's array

Thanks ! :)

Indeed, the cursor event does not have the same precision but I think it was because the use case did not exist yet. What do you want to do with it at cursor level? Also I suspect this is thusly made because of performance reasons but I need to dig deep into the code to make sure of this.

I would have liked to handle updates differently for each of these big arrays, to prevent from having a "firehose situation" when reading directly updates from the tree.
That way I don't have to write logic for dispatching each event to each handler, it would be just a matter of attaching to different levels of the tree (kind of a subtree in the end !) :)