@@ -23,7 +23,7 @@ import {
2323 update_effect
2424} from '../runtime.js' ;
2525import * as e from '../errors.js' ;
26- import { flush_tasks , has_pending_tasks , queue_micro_task } from '../dom/task.js' ;
26+ import { flush_tasks , queue_micro_task } from '../dom/task.js' ;
2727import { DEV } from 'esm-env' ;
2828import { invoke_error_boundary } from '../error-handling.js' ;
2929import { old_values } from './sources.js' ;
@@ -216,15 +216,6 @@ export class Batch {
216216 flush_queued_effects ( render_effects ) ;
217217 flush_queued_effects ( effects ) ;
218218
219- // Reinstate the current batch if there was no new one created, as `process()` runs in a loop in `flush_effects()`.
220- // That method expects `current_batch` to be set, and could run the loop again if effects result in new effects
221- // being scheduled but without writes happening in which case no new batch is created.
222- if ( current_batch === null ) {
223- current_batch = this ;
224- } else {
225- batches . delete ( this ) ;
226- }
227-
228219 this . #deferred?. resolve ( ) ;
229220 } else {
230221 this . #defer_effects( this . #render_effects) ;
@@ -365,19 +356,15 @@ export class Batch {
365356
366357 flush ( ) {
367358 if ( queued_root_effects . length > 0 ) {
359+ this . activate ( ) ;
368360 flush_effects ( ) ;
369- } else {
370- this . #commit( ) ;
371- }
372-
373- if ( current_batch !== this ) {
374- // this can happen if a `flushSync` occurred during `flush_effects()`,
375- // which is permitted in legacy mode despite being a terrible idea
376- return ;
377- }
378361
379- if ( this . #pending === 0 ) {
380- batches . delete ( this ) ;
362+ if ( current_batch !== null && current_batch !== this ) {
363+ // this can happen if a new batch was created during `flush_effects()`
364+ return ;
365+ }
366+ } else if ( this . #pending === 0 ) {
367+ this . #commit( ) ;
381368 }
382369
383370 this . deactivate ( ) ;
@@ -394,6 +381,7 @@ export class Batch {
394381 }
395382
396383 this . #callbacks. clear ( ) ;
384+ batches . delete ( this ) ;
397385 }
398386
399387 increment ( ) {
@@ -478,14 +466,17 @@ export function flushSync(fn) {
478466 var result ;
479467
480468 if ( fn ) {
481- flush_effects ( ) ;
469+ if ( current_batch !== null ) {
470+ flush_effects ( ) ;
471+ }
472+
482473 result = fn ( ) ;
483474 }
484475
485476 while ( true ) {
486477 flush_tasks ( ) ;
487478
488- if ( queued_root_effects . length === 0 && ! has_pending_tasks ( ) ) {
479+ if ( queued_root_effects . length === 0 ) {
489480 current_batch ?. flush ( ) ;
490481
491482 // we need to check again, in case we just updated an `$effect.pending()`
0 commit comments