99 EFFECT_ASYNC ,
1010 INERT ,
1111 RENDER_EFFECT ,
12- ROOT_EFFECT
12+ ROOT_EFFECT ,
13+ USER_EFFECT
1314} from '#client/constants' ;
1415import { async_mode_flag } from '../../flags/index.js' ;
1516import { deferred , define_property } from '../../shared/utils.js' ;
@@ -23,7 +24,8 @@ import {
2324 set_is_updating_effect ,
2425 set_queued_root_effects ,
2526 set_signal_status ,
26- update_effect
27+ update_effect ,
28+ write_version
2729} from '../runtime.js' ;
2830import * as e from '../errors.js' ;
2931import { flush_tasks } from '../dom/task.js' ;
@@ -513,7 +515,7 @@ function infinite_loop_guard() {
513515 * @param {Array<Effect> } effects
514516 * @returns {void }
515517 */
516- export function flush_queued_effects ( effects ) {
518+ function flush_queued_effects ( effects ) {
517519 var length = effects . length ;
518520 if ( length === 0 ) return ;
519521
@@ -522,6 +524,8 @@ export function flush_queued_effects(effects) {
522524
523525 if ( ( effect . f & ( DESTROYED | INERT ) ) === 0 ) {
524526 if ( check_dirtiness ( effect ) ) {
527+ var wv = write_version ;
528+
525529 update_effect ( effect ) ;
526530
527531 // Effects with no dependencies or teardown do not get added to the effect tree.
@@ -538,9 +542,19 @@ export function flush_queued_effects(effects) {
538542 effect . fn = null ;
539543 }
540544 }
545+
546+ // if state is written in a user effect, abort and re-schedule, lest we run
547+ // effects that should be removed as a result of the state change
548+ if ( write_version > wv && ( effect . f & USER_EFFECT ) !== 0 ) {
549+ break ;
550+ }
541551 }
542552 }
543553 }
554+
555+ for ( ; i < length ; i += 1 ) {
556+ schedule_effect ( effects [ i ] ) ;
557+ }
544558}
545559
546560/**
0 commit comments