Skip to content

Commit

Permalink
fix: lazily connect derievds (in deriveds) to their parent (#15129)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Jan 28, 2025
1 parent 3f8ce21 commit fc4dd2d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
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

0 comments on commit fc4dd2d

Please sign in to comment.