Skip to content
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

fix: lazily connect derievds (in deriveds) to their parent #15129

Merged
merged 1 commit into from
Jan 28, 2025
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/few-beans-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: lazily connect derievds (in deriveds) to their parent
4 changes: 0 additions & 4 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ export function derived(fn) {
signal.created = get_stack('CreatedAt');
}

if (parent_derived !== null) {
(parent_derived.children ??= []).push(signal);
}

return signal;
}

Expand Down
24 changes: 14 additions & 10 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,26 +957,30 @@ export function get(signal) {
new_deps.push(signal);
}
}
} else if (is_derived && /** @type {Derived} */ (signal).deps === null) {
}

if (
is_derived &&
/** @type {Derived} */ (signal).deps === null &&
(active_reaction === null || untracking || (active_reaction.f & DERIVED) !== 0)
) {
var derived = /** @type {Derived} */ (signal);
var parent = derived.parent;
var target = derived;

while (parent !== null) {
// Attach the derived to the nearest parent effect, if there are deriveds
// in between then we also need to attach them too
if (parent !== null) {
// Attach the derived to the nearest parent effect or derived
if ((parent.f & DERIVED) !== 0) {
var parent_derived = /** @type {Derived} */ (parent);

target = parent_derived;
parent = parent_derived.parent;
if (!parent_derived.children?.includes(derived)) {
(parent_derived.children ??= []).push(derived);
}
} else {
var parent_effect = /** @type {Effect} */ (parent);

if (!parent_effect.deriveds?.includes(target)) {
(parent_effect.deriveds ??= []).push(target);
if (!parent_effect.deriveds?.includes(derived)) {
(parent_effect.deriveds ??= []).push(derived);
}
break;
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,33 @@ describe('signals', () => {
};
});

test('nested deriveds do not connect inside parent deriveds if unused', () => {
return () => {
let a = render_effect(() => {});
let b: Derived<void> | undefined;

const destroy = effect_root(() => {
a = render_effect(() => {
$.untrack(() => {
b = derived(() => {
derived(() => {});
derived(() => {});
derived(() => {});
});
$.get(b);
});
});
});

assert.deepEqual(a.deriveds?.length, 1);
assert.deepEqual(b?.children, null);

destroy();

assert.deepEqual(a.deriveds, null);
};
});

test('deriveds containing effects work correctly when used with untrack', () => {
return () => {
let a = render_effect(() => {});
Expand Down
Loading