Code Monkey home page Code Monkey logo

Comments (13)

brunnerh avatar brunnerh commented on June 18, 2024 2

Code using frameworks like this should not rely on how often and when getters are called.
If the code is not pure it should neither be a getter, nor be called directly in the template.
(Having side effects in getters is just a bad idea in general, regardless of framework, though.)

from svelte.

brunnerh avatar brunnerh commented on June 18, 2024

This happens due to render effect batching.

$.render_effect(() => $.set_text(text_2, `class ${$.stringify(component.two())}`));
$.render_effect(() => {
	$.set_text(text, `clicks: ${$.stringify($.get(count))}`);
	$.set_text(text_1, `class ${$.stringify(component.one)}`);
});

Why does this matter to you?

from svelte.

anatolzak avatar anatolzak commented on June 18, 2024

@brunnerh I am not sure I have an actual issue with the getter rerunning. But it seems to go against what svelte is about--only re-rendering the parts that are dependent on state that has changed. I don't see why the getter should rerender when it doesn't depend on any state whatsoever.

from svelte.

brunnerh avatar brunnerh commented on June 18, 2024

Svelte 5 uses runtime reactivity; in general there is no way for the compiler to know whether anything in the property is reactive or not. Here there could potentially be some optimization via static analysis since the class is defined in the component but I wonder if the effort would be worth it.

(Related issue: #9768)

from svelte.

anatolzak avatar anatolzak commented on June 18, 2024

@brunnerh I am not an expert with svelte internals but how does svelte know to re-run a class method only if a state that it depends on changed? Why does this not apply for a class getter?

from svelte.

brunnerh avatar brunnerh commented on June 18, 2024

Effects re-run if a signal that is used in it gets triggered.
In the generated code I posted above you can see that there are two effects.
The class method has its own; I am also not an expert so I do not know why that is; maybe function calls are excluded from batching. Surely one of the maintainers could enlighten us.

from svelte.

anatolzak avatar anatolzak commented on June 18, 2024

Why does this matter to you?

@brunnerh I am relying on a getter inside an effect that unexpectedly re-runs even though the state that the getter depends on didn't change. I am not using derived because derived requires the properties it relies on to be defined on the prototype, but I set the properties in the constructor. Basically I would expect getters to be treated the same way as class methods in term of only re-running when they access an reactive state inside that has changed.

from svelte.

trueadm avatar trueadm commented on June 18, 2024

This is not a bug and is working as intended.

We treat call expressions as possibly containing side-effects. However we treat object member expressions as being pure and as such we batch them all together in a single effect because they're meant to be pure, whether or not they are reactive or not. Doing console.log is clearly a global side-effect, so that's why it's occurring in this case. Maybe we should add a warning for using something like console.log inside a template where we feel it should be pure.

If we broke out every member expression into its own effect then this would have a significant memory and performance impact.

from svelte.

anatolzak avatar anatolzak commented on June 18, 2024

We treat call expressions as possibly containing side-effects

@trueadm by this logic, why doing console.log inside a class method does not rerun the method?

from svelte.

trueadm avatar trueadm commented on June 18, 2024

@anatolzak Because there's nothing causing it to change. Member expressions are coupled together. If one of them becomes reactive, then all of them will be invoked each time as they're all meant to be pure.

from svelte.

anatolzak avatar anatolzak commented on June 18, 2024

If we broke out every member expression into its own effect then this would have a significant memory and performance impact.

@trueadm I don't know how svelte works internally, but I wouldn't expect the behavior you described. The behavior I expect is that any place that accesses a reactive state should be rerun when that reactive state changes. I wouldn't expect code to rerun otherwise.

from svelte.

trueadm avatar trueadm commented on June 18, 2024

@anatolzak The code is expected to re-run as it's meant to be pure. It's much more efficient to run pure logic over and over again than to create many effects for each bit of logic in case it might be side-effectful.

from svelte.

dummdidumm avatar dummdidumm commented on June 18, 2024

Closing because this works as designed - as mentioned by Dominic this is a deliberate heuristic that gives the best performance in the most number of cases.

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.