How "legal" is an effect inside a derived? #18305
-
|
tldr: let value = $derived.by(() => {
const deps = readDeps();
$effect(() => {
const cleanup = sideEffect(deps);
return () => cleanup();
});
return deps;
});currently this works in svelte 5. but it's not documented in the docs that you're allowed to define an effect inside a derivation. is that feature guaranteed to work in the future? or its an undocumented internal behavior and we shouldn't rely on it cause it can break with any version? or its undocumented solely because its heavily discouraged but its an intended to work? the way we expect it to work is - when a dirty derived is read, it runs the effect. when derived is updated or unsubscribed from - it tears down the effect. thats not really how it seems to work if I read the derived in non-reactive context while there're no "reactive" subscribers" - the effect doesn't run on the non-reactive dirty read but on a subsequent update of derived deps, and cleanup of that run runs on the next read. for context why the need: After some investigation it seems like originally asked on discord: https://discord.com/channels/457912077277855764/1509162376920825966 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It's valid, as long as any updated state is fully "owned" by the In the Discord thread you pointed to a test that was added in relation to a fix of an issue I had reported: Complex derived state that uses effects internally, e.g. for data fetching, is not that unusual. |
Beta Was this translation helpful? Give feedback.
It's valid, as long as any updated state is fully "owned" by the
$derived.In the Discord thread you pointed to a test that was added in relation to a fix of an issue I had reported:
Complex derived state that uses effects internally, e.g. for data fetching, is not that unusual.