Skip to content
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

Make pureComputed the default computed and make computed impureComputed or even autorun #2167

Closed
futpib opened this issue Dec 8, 2016 · 3 comments

Comments

@futpib
Copy link

futpib commented Dec 8, 2016

I find that, when choosing between pureComputed and computed, pureComputed is almost always the right one due to laziness and performance benefits.

You really want computed only when you want immediate and predictable evaluation for side-effects.

Also you almost certainly want to break your computed in two parts with ignoreDependencies to make sure evaluation happens only when you want:

ko.computed(() => {
    // register dependencies
    ko.ignoreDependencies(() => {
        // produce side-effects
    });
});

This is why I propose that computed is renamed to a longer (and scarier) impureComputed or autorun (like in mobx), possibly changing it's signature dividing "register dependencies" and "produce side-effects" parts. And pureComputed is renamed to computed to make it the go-to option for less experienced knockout.js developers. Of course this would break compatibility, so this is probably only feasible for the next major release.

If we could make documentation advise towards pureComputed and against computed when in doubt, that would be great.

If it were possible to ban any non-local mutation in pureComputed's read and write functions (except writing other observables in write), I'd also root for that.

@brianmhunt
Copy link
Member

Thanks @futpib – I'm closing this but opening the same issue at tko.computed (referenced above), since that's where it stands a better chance of being accepted.

@brianmhunt
Copy link
Member

For more info on tko, see: github.com/knockout/tko 🍻

@IanYates
Copy link

I'd also add that in our codebase we ban the use of ko.computed. When we actually want that behaviour I've aliased ko.sideEffects to be the same as ko.computed.
In our KO components we always have a dispose method which calls ko.disposeComputeds(this) - disposeComputeds is a simple function that shallow-enumerates the given object's properties and, if any are a computed, those computeds are disposed.
That seems to have handled all memory leaks, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants