Skip to content

fix: support $state.snapshot as part of variable declarations #11235

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 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-numbers-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: support `$state.snapshot` as part of variable declarations
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ export const javascript_visitors_runes = {
for (const declarator of node.declarations) {
const init = declarator.init;
const rune = get_rune(init, state.scope);
if (!rune || rune === '$effect.active' || rune === '$effect.root' || rune === '$inspect') {
if (
!rune ||
rune === '$effect.active' ||
rune === '$effect.root' ||
rune === '$inspect' ||
rune === '$state.snapshot'
) {
if (init != null && is_hoistable_function(init)) {
const hoistable_function = visit(init);
state.hoisted.push(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { test } from '../../test';

export default test({
html: `<button>[{"a":0}]</button>`,
html: `[{"a":0}] <button>[{"a":0}]</button>`,

async test({ assert, target }) {
const btn = target.querySelector('button');

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>[{"a":0},{"a":1}]</button>`);
assert.htmlEqual(target.innerHTML, `[{"a":0}] <button>[{"a":0},{"a":1}]</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script>
let items = $state([{a: 0}]);
let start = $state.snapshot(items);
</script>

{JSON.stringify(start)}
<button on:click={() => items.push({a: items.length})}>{JSON.stringify(structuredClone($state.snapshot(items)))}</button>