Is there a pattern for dynamic creation/disposal of $effect
s?
#16692
Unanswered
colecrouter
asked this question in
Q&A
Replies: 1 comment 1 reply
-
You could do something like this where |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Playground example
I recently come across this issue when trying to build nested proxy data classes. I refactored my classes using getters/setters to use
$state
and$effect
. This was a huge win in all regards (DX, performance, etc.), but now I cannot create these outside of a component's initialization lifecycle. This is a well documented limitation, and can be resolved by wrapping the$effect
declaration (or it's calling code) inside of an$effect.root
.At a top level, this isn't a massive issue, but it quickly gets out of hand with nested classes.
Things I've considered:
WeakMap
to keep track of class instances & call their clean-up functions.$effect
s hold onto references to their instances, so (I think) the instance won't ever get GC'd.$effect.root
clean-up.SvelteMap
,SvelteSet
, etc.[Symbol.dispose]
—I also understand that I might have tied my business logic too tightly to Svelte's reactivity system.
I would love to hear others comments on this matter, as well as any possible solutions.
Beta Was this translation helpful? Give feedback.
All reactions