@@ -23,7 +23,7 @@ import {
23
23
update_effect
24
24
} from '../runtime.js' ;
25
25
import * 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' ;
27
27
import { DEV } from 'esm-env' ;
28
28
import { invoke_error_boundary } from '../error-handling.js' ;
29
29
import { old_values } from './sources.js' ;
@@ -216,15 +216,6 @@ export class Batch {
216
216
flush_queued_effects ( render_effects ) ;
217
217
flush_queued_effects ( effects ) ;
218
218
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
-
228
219
this . #deferred?. resolve ( ) ;
229
220
} else {
230
221
this . #defer_effects( this . #render_effects) ;
@@ -365,19 +356,15 @@ export class Batch {
365
356
366
357
flush ( ) {
367
358
if ( queued_root_effects . length > 0 ) {
359
+ this . activate ( ) ;
368
360
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
- }
378
361
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( ) ;
381
368
}
382
369
383
370
this . deactivate ( ) ;
@@ -394,6 +381,7 @@ export class Batch {
394
381
}
395
382
396
383
this . #callbacks. clear ( ) ;
384
+ batches . delete ( this ) ;
397
385
}
398
386
399
387
increment ( ) {
@@ -478,14 +466,17 @@ export function flushSync(fn) {
478
466
var result ;
479
467
480
468
if ( fn ) {
481
- flush_effects ( ) ;
469
+ if ( current_batch !== null ) {
470
+ flush_effects ( ) ;
471
+ }
472
+
482
473
result = fn ( ) ;
483
474
}
484
475
485
476
while ( true ) {
486
477
flush_tasks ( ) ;
487
478
488
- if ( queued_root_effects . length === 0 && ! has_pending_tasks ( ) ) {
479
+ if ( queued_root_effects . length === 0 ) {
489
480
current_batch ?. flush ( ) ;
490
481
491
482
// we need to check again, in case we just updated an `$effect.pending()`
0 commit comments