deepObserve callback getting called multiple times within a single runInAction function
See original GitHub issueI have a simple case that looks something like this:
const myArray = observable([]);
deepObserve(myArray, () => {
console.log("array update");
});
runInAction(() => {
myArray.length = 1;
myArray.length = 2;
});
The log in this case runs twice, once for each length modification, immediately after each.
Is this normal? I would expect that because those length updates are in a runInAction
, the observer only gets updated once, after the action is fully executed.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top Results From Across the Web
deepObserve callback getting called multiple times within a single ...
I have a simple case that looks something like this: const myArray = observable([]); deepObserve(myArray, () => { console.log("array update"); }); ...
Read more >Issues · mobxjs/mobx-utils - GitHub
Utility functions and common patterns for MobX. ... deepObserve callback getting called multiple times within a single runInAction function.
Read more >When 'action'/'runInAction' is really needed in mobx react
Mobx action is doing the batching, similarly to how ReactJS batches multiple changes. When you use reactjs click handler react is ...
Read more >mobxjs/mobx - Gitter
oh … I was looking for something that still inside my observable object :( base on the above example, when filters is updated...
Read more >Writing async actions · Mobx Doc - iiunknown
This means that if you have a setTimeout , promise .then or async construction, and in that callback some more state is changed,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m not sure what heavy looks like, but calling heavy n times versus n+1 times is the same problem from a cost perspective. Either the function is unbearably heavy and locking up the app, or it isn’t. But the +1 isn’t going to make the fundamental difference and I rather look into optimizing heavy itself then. For example you might memoize the filter result for existing entries, and only apply filtering to your newly incoming data instead of the whole set, etc. But so far it doesn’t sound your 1 run less is going to make the difference, and there is probably a bigger underlying problem; heavy being heavy.
Note that autorun does support a custom scheduler, in case you want to debounce it still, even after respecting transactions.
On Mon, Mar 15, 2021 at 9:58 AM Andrew @.***> wrote:
Ran across this issue today. The reasoning makes sense but it still threw me for a bit of a loop. For any future googlers, I added a simple debounce to my deepObserve callback and that worked for my usecase.