Skip to content

Commit 3d5c4ab

Browse files
fix: prevent false-positive reactivity loss warning (#18373)
By checking that current/previous batch are null we filter out false positives. `reactivity_loss_tracker` is only reset after a microtask, so if a flush happens before that, we get warnings for things we shouldn't warn on. Fixes #18370 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent bdb1a0f commit 3d5c4ab

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎.changeset/rare-donkeys-repair.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: prevent false-positive reactivity loss warning

‎packages/svelte/src/internal/client/reactivity/async.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,23 @@ export async function save(promise) {
181181
* @returns {Promise<() => T>}
182182
*/
183183
export async function track_reactivity_loss(promise) {
184-
var previous_async_effect = reactivity_loss_tracker;
184+
var previous_reactivity_loss_tracker = reactivity_loss_tracker;
185185
// Ensure that unrelated reads after an async operation is kicked off don't cause false positives
186186
queueMicrotask(() => {
187-
if (reactivity_loss_tracker === previous_async_effect) {
187+
if (reactivity_loss_tracker === previous_reactivity_loss_tracker) {
188188
set_reactivity_loss_tracker(null);
189189
}
190190
});
191191

192192
var value = await promise;
193193

194194
return () => {
195-
set_reactivity_loss_tracker(previous_async_effect);
195+
set_reactivity_loss_tracker(previous_reactivity_loss_tracker);
196196
// While this can result in false negatives it also guards against the more important
197197
// false positives that would occur if this is the last in a chain of async operations,
198198
// and the reactivity_loss_tracker would then stay around until the next async operation happens.
199199
queueMicrotask(() => {
200-
if (reactivity_loss_tracker === previous_async_effect) {
200+
if (reactivity_loss_tracker === previous_reactivity_loss_tracker) {
201201
set_reactivity_loss_tracker(null);
202202
}
203203
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
batch_values,
5252
current_batch,
5353
flushSync,
54+
previous_batch,
5455
schedule_effect
5556
} from './reactivity/batch.js';
5657
import { handle_error } from './error-handling.js';
@@ -581,6 +582,11 @@ export function get(signal) {
581582
if (
582583
!untracking &&
583584
reactivity_loss_tracker &&
585+
// By checking that current/previous batch are null we filter out false positives.
586+
// reactivity_loss_tracker is only reset after a microtask, so if a flush happens
587+
// before that, we get warnings for things we shouldn't warn on.
588+
current_batch === null &&
589+
previous_batch === null &&
584590
!reactivity_loss_tracker.warned &&
585591
(reactivity_loss_tracker.effect.f & REACTION_IS_UPDATING) === 0 &&
586592
!reactivity_loss_tracker.effect_deps.has(signal)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
compileOptions: { dev: true },
6+
async test({ assert, target, warnings }) {
7+
await tick();
8+
const [increment] = target.querySelectorAll('button');
9+
10+
increment.click();
11+
await new Promise((resolve) => setTimeout(resolve, 10));
12+
assert.htmlEqual(target.innerHTML, '<button>increment</button> 1 1');
13+
assert.deepEqual(warnings, []);
14+
}
15+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
let x = $state(0);
3+
let y = $state(0);
4+
</script>
5+
6+
<button
7+
onclick={() => {
8+
x++;
9+
queueMicrotask(() => queueMicrotask(() => y++));
10+
}}
11+
>
12+
increment
13+
</button>
14+
15+
{await x}
16+
{y}

0 commit comments

Comments
 (0)