Skip to content

Commit bd9a2d2

Browse files
authored
fix: ensure unowned deriveds correctly update (#12747)
1 parent 98ae05b commit bd9a2d2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.changeset/olive-forks-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure unowned deriveds correctly update

packages/svelte/src/internal/client/runtime.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ export function check_dirtiness(reaction) {
162162

163163
if ((flags & MAYBE_DIRTY) !== 0) {
164164
var dependencies = reaction.deps;
165+
var is_unowned = (flags & UNOWNED) !== 0;
165166

166167
if (dependencies !== null) {
167-
var is_unowned = (flags & UNOWNED) !== 0;
168168
var i;
169169

170170
if ((flags & DISCONNECTED) !== 0) {
@@ -198,7 +198,10 @@ export function check_dirtiness(reaction) {
198198
}
199199
}
200200

201-
set_signal_status(reaction, CLEAN);
201+
// Unowned signals should never be marked as clean.
202+
if (!is_unowned) {
203+
set_signal_status(reaction, CLEAN);
204+
}
202205
}
203206

204207
return false;

packages/svelte/tests/signals/test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,21 @@ describe('signals', () => {
676676
assert.equal(d.deps?.length, 1);
677677
};
678678
});
679+
680+
test('unowned deriveds correctly update', () => {
681+
return () => {
682+
const arr1 = proxy<{ a: number }[]>([]);
683+
const arr2 = proxy([]);
684+
const combined = derived(() => [...arr1, ...arr2]);
685+
const derived_length = derived(() => $.get(combined).length);
686+
687+
assert.deepEqual($.get(combined), []);
688+
assert.equal($.get(derived_length), 0);
689+
690+
arr1.push({ a: 1 });
691+
692+
assert.deepEqual($.get(combined), [{ a: 1 }]);
693+
assert.equal($.get(derived_length), 1);
694+
};
695+
});
679696
});

0 commit comments

Comments
 (0)