File tree Expand file tree Collapse file tree
tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-4 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' svelte ' : patch
3+ ---
4+
5+ fix: prevent false-positive reactivity loss warning
Original file line number Diff line number Diff line change @@ -181,23 +181,23 @@ export async function save(promise) {
181181 * @returns {Promise<() => T> }
182182 */
183183export 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 } ) ;
Original file line number Diff line number Diff 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' ;
5657import { 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 )
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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 }
You can’t perform that action at this time.
0 commit comments