mobxjs / mobx-angular

The MobX connector for Angular.
MIT License
483 stars 59 forks source link

@computed function called twice? #18

Closed SDSLeon closed 7 years ago

SDSLeon commented 7 years ago

I am try the Todo example from this project and see that computed functions in mobx store is called twice every time when one 1 observable change.

http://prntscr.com/e9omwg

Is this a normal?

Also i see this in my own project.

If i remove *mobxAutorun it will be called only once. Example and my own app still works. http://prntscr.com/e9oois But now function is called every time i type in input. http://prntscr.com/e9opxs

SDSLeon commented 7 years ago

And if i write autorun function where i will also use this computed. All things back to normal and computed will be called only once.

http://prntscr.com/e9uutf with use co,puted values in autorun http://prntscr.com/e9uv6o without

SDSLeon commented 7 years ago

The problem exist if use *mobxAutorun with autorunAsync.

With *mobxAutorunSync and autorun computed called only once on update, but still called twice on Init.

SDSLeon commented 7 years ago

this.autoDetect(this.view); Need to be moved to constructor for autoruns directives. But stay in Init method for reaction.

This will fix the double computed runs on init

adamkleingit commented 7 years ago

I moved createEmbeddedView and autoDetect to ngOnInit, and not using autorunAsync anymore.

Seems like it's solving the problem, so I'll also deprecate autorunSync directive.

Fixed in 1.2.8 and published - can you verify?

SDSLeon commented 7 years ago

Thanks! Fixed twice run on change observable value.

Still runs twice on first run after F5. http://prntscr.com/ec8utz

I think autoDetect should be called before ngOnInit to be applied for first run.

adamkleingit commented 7 years ago

When I tried it it ran only once

Can you share your code? Maybe you're calling the computed value outside the autorun? Or you're changing an observable?

SDSLeon commented 7 years ago

yeah! you are right, i recheck the code and find the observable value that changed.

So, its fully fixed. =) Thanks.

adamkleingit commented 7 years ago

awesome