That is quite a cool idea actually, maybe MobX should offer something standardized for that :-D. @mweststrate yeah, I tried something like. In practice, this is very unlikely. If our computed property is created with that option set, MobX will cache the computed value even when it is accessed outside of a reactive context.

But first, a little background.

Perhaps it could be done if JavaScript had something like Java’s WeakReference.

react mobx, component does not render new observable, React Hooks (useState) and Mobx [No mobx-react-lite]. That’s clearly a memory leak. For a computed value evaluated in an autorun to cause a memory leak, the computed value would have to access some long-lived observable. What's the verdicts on hub-less circle bicycle wheels? When one of the observables does change, MobX magically knows to invalidate the cache and re-run the derivation. The main difference between the two is that computed properties create an explicit caching point, but later they work the same and any other computed value or MobX based reaction like @observer components can react to them. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. Thanks for all the hard work! MobX computed properties are just like normal JavaScript properties, with some additional perks.

If the this.userName property is an See the volatile state section. milliseconds. In computed properties you can basically do anything except modifying state so you should be fine there. Understanding when to use @computed will provide additional tools for you to write a highly performant app. First, let’s review a couple of points about @computed below: An example from the docs shows this simply. And beyond that by many individual backers and through one time contributions. In that scenario, the autorun would indeed cause a memory leak, keeping an object from being garbage collected that is inaccessible to the application.

// combine part1 and part2 into a result somehow... You signed in with another tab or window. If we access the property outside of an autorun, observer, or similar, then the property acts just like a normal property. JavaScript computed - 4 examples found. MobX uses the same (i.e.

How to make reusable React/MobX components? In this scenario, there is no extension of lifetime.

Spoiler alert: The lifetime of a computed property with keepAlive: true will be extended to the longest lifetime of any of the observables it accesses. It there any way to cache the result of computation with different inputs?

When an async function is called, only the first part (up to the first await) executes immediately, and so that's the only part that MobX will be able to track.

promisedComputed will update also, as soon as possible. Actions and Computed Property in Mobx. If the this.userName property is an observable and is modified, the We can build an object distills the relevant filter information into an object that can be used in the REST call for obtaining child filter data.

The remaining parts execute later on, when MobX has stopped listening. It will initially return When none of the observables that the derivation depends on have changed, repeatedly accessing the computed property just returns the same, previously-computed value.

Ah hah, there’s the keepAlive: true option!

@mweststrate Yeah, I thought about to create computed property on nested object levels, but there are few reason that I want to do it: Fair enough, in that case I would simple introduce methods like, From there on you can instantiate selectors by invoking the factory, and get the result of the selector by using .get(). least 500 milliseconds. It also has built-in type definitions: no MobX computed expressions that . A computed in MobX is defined by a function, which consumes other observable values and is automatically re-evaluated, like a spreadsheet cell containing a calculation. If you are like me, you stay away from these parts, unsure what strange things might occur if you use a tool the wrong way. Embrace mutability. New issue Have a question about this project?

Maybe we grab all the input observables, squirrel those away too, and then next time the property is accessed we compare to the previous observable values to decide whether to re-run the derivation? Unlike the normal computed feature, promisedComputed can't work as a decorator on a property getter. This is getting complicated, and isn’t MobX supposed to deal with this sort of thing automatically?

[off topic] I'm really like MobX in general by the way (I'm converting from redux). What could cause SQL Server to deny execution of a SP at first, but allow it later with no privileges change?

By clicking “Sign up for GitHub”, you agree to our terms of service and Learn more, We use analytics cookies to understand how you use our websites so we can make them better, e.g.

correct) approach as ko.pureComputed from the ground up, and the Atom class makes it easy to detect when your data transitions they're used to log you in. Views come in two flavors: views with arguments and views without arguments. When in doubt, move all your gathering of observable values to the start of the async function. So where in Redux you would have a selector (factory), in MobX people either introduce a computed on an instance field, or as field in a React component.