Comments (13)
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.
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.
@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.
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.
@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.
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.
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.
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.
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.
@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.
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.
@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.
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)
- VS Code: The Svelte server crashed 5 times. Cannot find module @rollup/rollup-win32-x64-msvc HOT 8
- ReactiveMap and ReactiveSet has no value in log HOT 1
- SVGs with animateTransform lag for an extremely long time when embedded HOT 7
- Transitions are present before delay is reached HOT 1
- Writing an import statement inside the markup as document content makes Svelte process it HOT 4
- missing type of HTMLAttribute HOT 4
- Svelte 5: Add warning to compiler when using `bind:value` incorrectly HOT 2
- Svelte 5 migrate function throws if there's no semicolon at the end of a reactive statement
- Svelte 5: Chrome: svelte-select list is not opened on the first click (same code of Svelte 4) HOT 21
- Option to extends components HOT 2
- `remove_input_attr_defaults` function is slow during hydration HOT 5
- Svelte 5: restrict which characters can be used in component props HOT 1
- Nested `:global` selectors HOT 6
- Svelte 5. relative attribute selector, inside a :global(...) selector, is being scoped
- Allow the default value for a $property to be a $state() HOT 2
- Unnecessary recalculation of derived value when element added/removed from DOM HOT 2
- svelte(a11y-label-has-associated-control) shows incorrectly when using render snippet HOT 4
- Svelte 5: setting input value to an object property, updating a store, and using the store value in the template causes input to be reset HOT 4
- Svelte 5: CSS Comments between selector and brackets {} causing compilation error
- Svelte 5: Bug with data exposed to a slot in a slot
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte.