Skip to content

breaking: state mutations inside the template are no longer allowed #13660

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

Merged
merged 7 commits into from
Oct 19, 2024

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Oct 18, 2024

A logical extension of #13625. Given we don't allow mutations in template logic block expressions, it also makes sense to no longer allow the inside template expressions either.

Closes #13633
Fixes #13677

Copy link

changeset-bot bot commented Oct 18, 2024

🦋 Changeset detected

Latest commit: 48ee7bd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning should probably be updated to not mention logic blocks but instead something like "during template rendering".

Also it should probably only apply in runes mode, else it's quite the breaking change.

@@ -325,7 +325,7 @@ export function template_effect(fn) {
value: '{expression}'
});
}
return render_effect(fn);
return block(fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, can we just change the type of effect? I would've expected this has unintended side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's pretty much the same thing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what ways are they not? If template_effect which is pretty much used for all things that are output into the html isn't using a render effect, then what do we even need it for?

Also did you see my comment about only doing the validation in runes mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation was always runes mode anyway, we're re-using the same validation as before. Blocks differ in two ways:

  • They don't clear destroy branch effects on updates
  • They disallow mutations as do deriveds

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asked differently: what's stopping us from keep using the render effect, if we would extract the validation into a common method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately $effect.pre and a bunch of other stuff already using render effect with mutations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this change affect or be affected in some by way the work done in #13600 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they're connected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @dummdidumm on this — treating every template expression as a block effect seems like it could have undesirable consequences, if not now then in the future. For example it means that in update_effect we do destroy_block_effect_children instead of destroy_effect_children, which means we don't destroy child deriveds, which feels like it could be bad.

Shouldn't we just have a new flag that means 'state mutations not allowed' and apply that to deriveds and blocks and template expressions?

Then in set we'd do something like this

-(active_reaction.f & (DERIVED | BLOCK_EFFECT)) !== 0 &&
+(active_reaction.f & (MUTATIONS_DISALLOWED)) !== 0 &&

Copy link
Contributor Author

@trueadm trueadm Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which means we don't destroy child deriveds, which feels like it could be bad.

That's an oversight, we should always be destroying them. #13693 fixes that so they're now consistent.

Shouldn't we just have a new flag that means 'state mutations not allowed' and apply that to deriveds and blocks and template expressions?

I'm hesitant adding more flag conditions, we already have a ton and each one we add removes from our total allowed, which removes future additions for features around async etc. However maybe we can do that – I just felt it was unnecessary to introduce a flag when the two common concepts – deriveds and block effects have the same unified heuristic.

@dummdidumm
Copy link
Member

#13684 makes me worried to go ahead with this so close to release - feels like there's hidden bugs and the cure could be worse than the disease

@trueadm
Copy link
Contributor Author

trueadm commented Oct 19, 2024

#13684 makes me worried to go ahead with this so close to release - feels like there's hidden bugs and the cure could be worse than the disease

That was unrelated and has since been fixed. :)

@trueadm trueadm merged commit ae10f4d into main Oct 19, 2024
9 checks passed
@trueadm trueadm deleted the template-mutations branch October 19, 2024 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants