Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Granular observation in Map/Array #49

Open
kevinresol opened this issue Oct 20, 2020 · 2 comments
Open

Granular observation in Map/Array #49

kevinresol opened this issue Oct 20, 2020 · 2 comments
Milestone

Comments

@kevinresol
Copy link
Member

kevinresol commented Oct 20, 2020

As reference, MobX is able to do the following:

const {observable, autorun} = require("mobx");

const map1 = observable.map();

autorun(() => {
	console.log('compute');
    console.log(map1.has('foo'))
})

console.log(0);
map1.set('foo', '1'); // computes
console.log(1);
map1.set('bar', '2'); // no compute because 'bar' key is not involved
console.log(2);
map1.set('foo', '3'); // no compute because existence of 'foo' unchanged
console.log(3);
map1.delete('foo'); // computes
console.log(4);

console.log('==================')

const map2 = observable.map();

autorun(() => {
	console.log('compute');
    console.log(map2.get('foo'));
})

console.log(0);
map2.set('foo', '1'); // computes
console.log(1);
map2.set('bar', '2'); // no compute because 'bar' key is not involved
console.log(2);
map2.set('foo', '3'); // computes
console.log(3);
map2.set('foo', '3'); // no compute because value unchanged
console.log(4);

I think it is done by internally caching the request: https://github.com/mobxjs/mobx/blob/6daafc4f7930f807dd047bae1be55938e95017e5/src/types/observablemap.ts#L115-L131

kevinresol added a commit to kevinresol/exp-ecs that referenced this issue Oct 20, 2020
@back2dos
Copy link
Member

For map this is quite easy to do. It could indeed be implemented as a Map<Key, State<Value>>.

I wonder if we should have different data structures for different use cases.

@kevinresol
Copy link
Member Author

Ya, a separate one is good, and we may want to profile them and let user know when to choose which.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants