-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Inconsistencies between observablemap and observableset #2336
Comments
Interesting observation. It should be probably fixed as part of the upcoming V6 because this change could easily break existing apps that assume such a "faulty" behavior. If you want to try a PR, it's probably best to do it against |
This is currently working as designed, the rationale being as follows: If Sets would maintain an atom per 'value', that would allocate an atom for every This is potentially true for maps as well, but since those are typically used as lookup maps, there are less usage scenerios where many elements are considered, but few are part of the map (this is an assumption of course). Similarly, keys used in observable map are typically primitives such as strings, so the risk of accidentally preventing the garbage collection of values is not present, like with Sets. This is a deliberate choice, and not the most optimal one in all possible scenarios, but at least you can optimize for the case you describe by using an observable map instead, where you use the values as keys, and |
I can if I know about this behavior, but i'm speculating many users of mobx do not (as it is not documented anywhere and even if it was the heuristics here are confusing ). So they might reach for a Set to replace a Can we consider having similar behaviors for both Another alternative (for both example: const set = observable(new Set());
autorun(() => {
if (set.has("foo")) {
console.log("set: its there!");
} else {
console.log("set: nope");
}
});
set.add("bar"); // will run because "foo" is not in the set and we added a new element and since we don't keep track of elements that are not in the collection we need to re-run
set.delete("bar"); // won't run because we're deleting an element that was in the collection. Elements outside of the collection can not be affected
set.add("baz"); // will run because "foo" is not in the set and we added a new element
set.add("foo"); // will run
set.add("bar"); // won't run because 'foo' is now in the set and part of the atom map
set.delete("foo"); // will run with |
Closing for now as there doesn't seem to be a big audience needing this and there is a work around. |
Intended outcome: observable set and observable map should have the similar behaviour in terms of when reactions are triggered.
Actual outcome: observable map has an internal map of observable values which are updated and changed as needed, therefore if a single value is retrieved from a map within a reaction, that reaction will only care about that single value, any unrelated mutation to the map will not re-trigger the reaction. Whereas in an observable set there's just one atom that manages all changes, so if a reaction only cares about whether or not a single value is in a set any unrelated mutation to that set will still trigger the reaction.
How to reproduce the issue::
https://codesandbox.io/s/mobx-mapset-behavior-t8bjg
Versions All versions that have set/map.
Note: can contribute PR.
The text was updated successfully, but these errors were encountered: