-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f937f6
breaking: state mutations inside the template are no longer allowed
trueadm 551b006
fix test
trueadm 862270f
fix test
trueadm c768825
lint
trueadm 908f0b1
update error message
trueadm c83578b
fix test
trueadm 48ee7bd
Merge branch 'main' into template-mutations
trueadm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
breaking: state mutations inside the template are no longer allowed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/svelte/tests/runtime-runes/samples/side-effect-template/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
compileOptions: { | ||
dev: true | ||
}, | ||
|
||
test({ assert, target }) { | ||
const button = target.querySelector('button'); | ||
|
||
assert.throws(() => { | ||
button?.click(); | ||
flushSync(); | ||
}, /state_unsafe_mutation/); | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/svelte/tests/runtime-runes/samples/side-effect-template/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
let items = $state([]); | ||
</script> | ||
|
||
<button onclick={() => items.push(3, 2, 1)}>Add</button> | ||
{JSON.stringify(items.sort())} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 dodestroy_block_effect_children
instead ofdestroy_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 thisUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an oversight, we should always be destroying them. #13693 fixes that so they're now consistent.
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.