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
createTransformer leaks memory when run outside a reactive context #116
Comments
Any memoization mechanism suffers from a similar faith semantics. While using computed outside reactive context is in principle harmless, createTransformer is not. I think the proper solution to this problem is too simply throw (PR is welcome!). cleanup could work as well, but it means that users might totally not get any benefits from use createTransformer at all, and is probably not what he intended. |
Thanks @mweststrate. Perhaps whether MobX throws or calls |
By the way, is it possible for something outside core MobX (because |
@mweststrate Should this be implemented by |
I'm using createTransformer for a method that takes a small range of possible arguments. In some cases the result is observed, but not in all cases. The leak is bounded by the input range. So we are getting the effect we intended, an exception would be bad for us, and cleanup would be fine. |
Does computedFn suffer from this? |
@vivainio, no it shouldn't: as:
Also, it will print a warning if used outside a reactive context and not memoize at all (unless the |
The docs say:
But it's worse than that. A transformer used outside a reactive context will leak memory each time it is called with a new value. The object created by
createView
will be stored inviews
for each invocation, but becauseonBecomeUnobserved
will never be called, that view will never be removed. For example, this code causes well over a gigabyte of memory to be allocated:As the documentation says, using a transformer in this way sort of defeats its purpose. But the same could be said of
computed
. If your property is meant to be called outside a reactive context, why use a computed observable at all? Just use a regular property. But there are, of course, situations where it's convenient to have a single property that is used both inside and outside reactive contexts, and it's a useful feature of mobx that computed observables, when called outside a reactive context, or no worse than regular property getters. In my opinion, the same should be true ofcreateTransformer
.I'd further argue that the transformer's
cleanup
function should be called immediately when the function is invoked in a non-reactive context.The text was updated successfully, but these errors were encountered: