Skip to content

Commit

Permalink
chore: improve derived ownership model (#13623)
Browse files Browse the repository at this point in the history
* chore: improve derived ownership model

* remove redundant assertion
  • Loading branch information
trueadm authored Oct 16, 2024
1 parent 974e823 commit 6a38bbe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-vans-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: improve derived ownership model
5 changes: 1 addition & 4 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { inspect_effects, set_inspect_effects } from './sources.js';
*/
/*#__NO_SIDE_EFFECTS__*/
export function derived(fn) {
let flags = DERIVED | DIRTY;
var flags = DERIVED | DIRTY;

if (active_effect === null) {
flags |= UNOWNED;
Expand All @@ -58,9 +58,6 @@ export function derived(fn) {
var derived = /** @type {Derived} */ (active_reaction);
(derived.children ??= []).push(signal);
}
if (active_effect !== null) {
(active_effect.deriveds ??= []).push(signal);
}

return signal;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,17 @@ export function get(signal) {
set_signal_status(active_effect, DIRTY);
schedule_effect(active_effect);
}
} else if (is_derived) {
var derived = /** @type {Derived} */ (signal);
var parent = derived.parent;

if (parent !== null && !parent.deriveds?.includes(derived)) {
(parent.deriveds ??= []).push(derived);
}
}

if (is_derived) {
var derived = /** @type {Derived} */ (signal);
derived = /** @type {Derived} */ (signal);

if (check_dirtiness(derived)) {
update_derived(derived);
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ describe('signals', () => {
assert.equal(a?.deps?.length, 1);
assert.equal(s?.reactions?.length, 1);
destroy();
assert.equal(a?.deps, null);
assert.equal(s?.reactions, null);
};
});
Expand Down

0 comments on commit 6a38bbe

Please sign in to comment.