-
-
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
suggestion: log warning when computed property is accessed outside reaction/observer #1197
Comments
What about accessing computed inside "writing" context? @computed get sum() {
return this.a + this.b
}
@action doSomething() { // not necessarily action/tx/batch?
this.a = 5;
console.log(this.sum) // do something with sum
} |
@urugator you can always extract the body of the computed property to a method, and use that method in other context. This way the expectation is clear, i.e. the computed is expected to be memorized and the extracted method is expected to re-compute every time, just like a normal method is supposed to do. |
Being used in writing context doesn't mean it needs to recompute every time the context is invoked. It needs to be recomputed only if the dependencies were changed. That may happen conditionaly or not at all. |
Before we discuss further, I hope we can agree that a good API should have the following properties
If we cannot agree on these, I don't think we can be productive in the thread and might as well stop here. So, back to the computed property, first, it clearly does not meet point 2, because it memoizes in reaction/observer but does not in other context, which makes bugs very hard to discover and developers have to constantly remind themselves of that. Refactoring can easily cause the bug. second, it is neutral to point 3 and third it does nothing for point 4. I am not proposing for computed property to hit all these 4 points. but at lease it should warn developers that they might have done something wrong. Won't you want that when you use other people's API? |
Throwing/warning outside of specific context is context depedent behavior as well.
But is using computed inside non-tracking context wrong? To me it seem that the ability to switch between these two modes based on the context is actually considered a feature (I may be wrong of course). |
I suggest to log warining about perfomance when unobserved computed used outside of reaction or observer. |
I want to extend the request for accessing any observable value, not only computed. React.render(<Timer timerData={timerData.secondsPassed} />, document.body) The developer will see a warning about that. |
MobX 4 will introduce |
@mweststrate Couldn't Also not fan of |
@urugator yes that sounds good! I was thinking on a computed level flag as well indeed, but couldn't figure any name that would fit in 80 chars linelength. |
I was also thinking about |
One of my component got great performance downgrade after refactoring. It turns out that the computed property is accessed in a hot code path that is not in reaction/observer, therefore the computed property got recomputed over and over again.
The is one of those errors that is really hard to track down. It would be nice that mobx would log warning in the debug mode when the computed property is used this way. After all, computed properties are primarily supposed to be used in reaction/observer, which enables its memorization capability. However, because of the fact that you can still use computed property in non reaction/observer context, it makes really hard to track down the performance problem caused by it.
Please consider it, or provide other way to warn developer when they use computed properties in wrong context.
The text was updated successfully, but these errors were encountered: