-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: use state
instead of source
in reactive classes
#16239
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
Open
paoloricciuti
wants to merge
3
commits into
main
Choose a base branch
from
source-to-state-in-classes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
−50
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
106 changes: 90 additions & 16 deletions
106
packages/svelte/tests/runtime-runes/samples/side-effect-derived-map/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 |
---|---|---|
@@ -1,27 +1,101 @@ | ||
<script> | ||
import { SvelteMap } from 'svelte/reactivity'; | ||
|
||
let visibleExternal = $state(false); | ||
let external = new SvelteMap(); | ||
const throws = $derived.by(() => { | ||
external.set(1, 1); | ||
return external; | ||
let outside_basic = $state(false); | ||
let outside_basic_map = new SvelteMap(); | ||
const throw_basic = $derived.by(() => { | ||
outside_basic_map.set(1, 1); | ||
return outside_basic_map; | ||
}); | ||
|
||
let visibleInternal = $state(false); | ||
const works = $derived.by(() => { | ||
let internal = new SvelteMap(); | ||
internal.set(1, 1); | ||
return internal; | ||
let inside_basic = $state(false); | ||
const works_basic = $derived.by(() => { | ||
let inside = new SvelteMap(); | ||
inside.set(1, 1); | ||
return inside; | ||
}); | ||
|
||
let outside_has = $state(false); | ||
let outside_has_map = new SvelteMap([[1, 1]]); | ||
const throw_has = $derived.by(() => { | ||
outside_has_map.has(1); | ||
outside_has_map.set(1, 2); | ||
return outside_has_map; | ||
}); | ||
|
||
let inside_has = $state(false); | ||
const works_has = $derived.by(() => { | ||
let inside = new SvelteMap([[1, 1]]); | ||
inside.has(1); | ||
inside.set(1, 1); | ||
return inside; | ||
}); | ||
|
||
let outside_get = $state(false); | ||
let outside_get_map = new SvelteMap([[1, 1]]); | ||
const throw_get = $derived.by(() => { | ||
outside_get_map.get(1); | ||
outside_get_map.set(1, 2); | ||
return outside_get_map; | ||
}); | ||
|
||
let inside_get = $state(false); | ||
const works_get = $derived.by(() => { | ||
let inside = new SvelteMap([[1, 1]]); | ||
inside.get(1); | ||
inside.set(1, 1); | ||
return inside; | ||
}); | ||
|
||
let outside_values = $state(false); | ||
let outside_values_map = new SvelteMap([[1, 1]]); | ||
const throw_values = $derived.by(() => { | ||
outside_values_map.values(1); | ||
outside_values_map.set(1, 2); | ||
return outside_values_map; | ||
}); | ||
|
||
let inside_values = $state(false); | ||
const works_values = $derived.by(() => { | ||
let inside = new SvelteMap([[1, 1]]); | ||
inside.values(); | ||
inside.set(1, 1); | ||
return inside; | ||
}); | ||
</script> | ||
|
||
<button onclick={() => (visibleExternal = true)}>external</button> | ||
{#if visibleExternal} | ||
{throws} | ||
<button onclick={() => (outside_basic = true)}>external</button> | ||
{#if outside_basic} | ||
{throw_basic} | ||
{/if} | ||
<button onclick={() => (inside_basic = true)}>internal</button> | ||
{#if inside_basic} | ||
{works_basic} | ||
{/if} | ||
|
||
<button onclick={() => (outside_has = true)}>external</button> | ||
{#if outside_has} | ||
{throw_has} | ||
{/if} | ||
<button onclick={() => (visibleInternal = true)}>internal</button> | ||
{#if visibleInternal} | ||
{works} | ||
<button onclick={() => (inside_has = true)}>internal</button> | ||
{#if inside_has} | ||
{works_has} | ||
{/if} | ||
|
||
<button onclick={() => (outside_get = true)}>external</button> | ||
{#if outside_get} | ||
{throw_get} | ||
{/if} | ||
<button onclick={() => (inside_get = true)}>internal</button> | ||
{#if inside_get} | ||
{works_get} | ||
{/if} | ||
|
||
<button onclick={() => (outside_values = true)}>external</button> | ||
{#if outside_values} | ||
{throw_values} | ||
{/if} | ||
<button onclick={() => (inside_values = true)}>internal</button> | ||
{#if inside_values} | ||
{works_values} | ||
{/if} |
Oops, something went wrong.
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.
Wondering if there are cases when map still lives, while the reaction could have been GCed and this would prevent it?
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.
Yeah I was wondering about that too...technically it shouldn't be the case because either there's no active reaction (it's outside of a derived/effect) or it's inside it but this means that when the derived/effect is no longer used the function should be GCd and everything inside it too...but I need to do a better check
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.
Maybe this can become
initial_reaction = WeakSet(active_reaction)
and we can check withthis.initial_reaction.has(active_reaction)
?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.
Just for the sake of history, I've also discovered there is a WeakRef struct that could be alternative here. In case of any future issues might be a possible alternative implementation