Skip to content

Commit 181fb2a

Browse files
fix: correctly set is_updating before flushing root effects (#15442)
* fix: correctly set `is_updating` before flushing root effects * rename for consistency with update_effect * use var --------- Co-authored-by: Rich Harris <[email protected]>
1 parent b28b013 commit 181fb2a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

.changeset/proud-poems-brake.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: correctly set `is_updating` before flushing root effects

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,11 @@ function infinite_loop_guard() {
644644
}
645645

646646
function flush_queued_root_effects() {
647+
var was_updating_effect = is_updating_effect;
648+
647649
try {
648650
var flush_count = 0;
651+
is_updating_effect = true;
649652

650653
while (queued_root_effects.length > 0) {
651654
if (flush_count++ > 1000) {
@@ -670,6 +673,7 @@ function flush_queued_root_effects() {
670673
}
671674
} finally {
672675
is_flushing = false;
676+
is_updating_effect = was_updating_effect;
673677

674678
last_scheduled_effect = null;
675679
if (DEV) {

packages/svelte/tests/signals/test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,4 +1135,45 @@ describe('signals', () => {
11351135
destroy();
11361136
};
11371137
});
1138+
1139+
test('unowned deriveds correctly update', () => {
1140+
const log: any[] = [];
1141+
1142+
return () => {
1143+
const a = state(0);
1144+
const b = state(0);
1145+
const c = derived(() => {
1146+
return $.get(a);
1147+
});
1148+
const d = derived(() => {
1149+
return $.get(b);
1150+
});
1151+
1152+
const destroy = effect_root(() => {
1153+
const e = derived(() => {
1154+
return $.get(c) === 1 && $.get(d) === 1;
1155+
});
1156+
render_effect(() => {
1157+
log.push($.get(e));
1158+
});
1159+
});
1160+
1161+
assert.deepEqual(log, [false]);
1162+
1163+
set(a, 1);
1164+
set(b, 1);
1165+
1166+
flushSync();
1167+
1168+
assert.deepEqual(log, [false, true]);
1169+
1170+
set(b, 9);
1171+
1172+
flushSync();
1173+
1174+
assert.deepEqual(log, [false, true, false]);
1175+
1176+
destroy();
1177+
};
1178+
});
11381179
});

0 commit comments

Comments
 (0)