-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure deep mutation ownership widening
Previously, ownership widening/removal was only done if the immediate object that was encountered was state. This isn't always the case. It also didn't take into account classes with state on it (which turn into getters). This change takes both these cases into account and now always traverses the given object deeply. fixes #11060
- Loading branch information
1 parent
b1a8038
commit d849558
Showing
5 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 | ||
--- | ||
|
||
fix: ensure deep mutation ownership widening |
This file contains 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
35 changes: 35 additions & 0 deletions
35
packages/svelte/tests/runtime-runes/samples/non-local-mutation-inherited-owner-6/_config.js
This file contains 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,35 @@ | ||
import { tick } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
/** @type {typeof console.warn} */ | ||
let warn; | ||
|
||
/** @type {any[]} */ | ||
let warnings = []; | ||
|
||
export default test({ | ||
compileOptions: { | ||
dev: true | ||
}, | ||
|
||
before_test: () => { | ||
warn = console.warn; | ||
|
||
console.warn = (...args) => { | ||
warnings.push(...args); | ||
}; | ||
}, | ||
|
||
after_test: () => { | ||
console.warn = warn; | ||
warnings = []; | ||
}, | ||
|
||
async test({ assert, target }) { | ||
const btn = target.querySelector('button'); | ||
|
||
await btn?.click(); | ||
await tick(); | ||
assert.deepEqual(warnings.length, 0); | ||
} | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/svelte/tests/runtime-runes/samples/non-local-mutation-inherited-owner-6/main.svelte
This file contains 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,25 @@ | ||
<script lang="ts"> | ||
import { setContext } from 'svelte'; | ||
import Sub from './sub.svelte'; | ||
class Person1 { | ||
value = $state({ person: 'John', age: 33 }) | ||
} | ||
const class_nested_state = $state(new Person1()); | ||
class Person2 { | ||
person = $state('John'); | ||
age = $state(33); | ||
} | ||
const state_nested_class = $state({ value: new Person2() }); | ||
const nested_state = $state({ person: 'John', age: 33 }); | ||
setContext('foo', { | ||
nested_state, | ||
get class_nested_state() { return class_nested_state }, | ||
get state_nested_class() { return state_nested_class } | ||
}) | ||
</script> | ||
|
||
<Sub {class_nested_state} {state_nested_class} {nested_state} /> |
11 changes: 11 additions & 0 deletions
11
packages/svelte/tests/runtime-runes/samples/non-local-mutation-inherited-owner-6/sub.svelte
This file contains 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,11 @@ | ||
<script> | ||
import { getContext } from "svelte"; | ||
const foo = getContext('foo') | ||
</script> | ||
|
||
<button onclick={() => { | ||
foo.class_nested_state.value.age++; | ||
foo.state_nested_class.value.age++; | ||
foo.nested_state.age++; | ||
}}>mutate</button> |