@@ -64,13 +64,12 @@ function dequeue() {
64
64
/** @type {Effect[] } */
65
65
let queued_root_effects = [ ] ;
66
66
67
- /** @type {Effect | null } */
68
- let last_scheduled_effect = null ;
69
-
70
67
let is_flushing = false ;
71
68
72
69
let is_flushing_sync = false ;
73
70
71
+ let flush_count = 0 ;
72
+
74
73
export class Batch {
75
74
/**
76
75
* The current values of any sources that are updated in this batch
@@ -463,10 +462,6 @@ export function flushSync(fn) {
463
462
464
463
// we need to check again, in case we just updated an `$effect.pending()`
465
464
if ( queued_root_effects . length === 0 ) {
466
- // this would be reset in `flush_effects()` but since we are early returning here,
467
- // we need to reset it here as well in case the first time there's 0 queued root effects
468
- last_scheduled_effect = null ;
469
-
470
465
return /** @type {T } */ ( result ) ;
471
466
}
472
467
}
@@ -483,46 +478,16 @@ function flush_effects() {
483
478
is_flushing = true ;
484
479
485
480
try {
486
- var flush_count = 0 ;
481
+ flush_count = 0 ;
487
482
set_is_updating_effect ( true ) ;
488
483
489
484
while ( queued_root_effects . length > 0 ) {
490
- var batch = Batch . ensure ( ) ;
491
-
492
- if ( flush_count ++ > 1000 ) {
493
- if ( DEV ) {
494
- var updates = new Map ( ) ;
495
-
496
- for ( const source of batch . current . keys ( ) ) {
497
- for ( const [ stack , update ] of source . updated ?? [ ] ) {
498
- var entry = updates . get ( stack ) ;
499
-
500
- if ( ! entry ) {
501
- entry = { error : update . error , count : 0 } ;
502
- updates . set ( stack , entry ) ;
503
- }
504
-
505
- entry . count += update . count ;
506
- }
507
- }
508
-
509
- for ( const update of updates . values ( ) ) {
510
- // eslint-disable-next-line no-console
511
- console . error ( update . error ) ;
512
- }
513
- }
514
-
515
- infinite_loop_guard ( ) ;
516
- }
517
-
518
- batch . process ( queued_root_effects ) ;
485
+ Batch . ensure ( ) . process ( queued_root_effects ) ;
519
486
old_values . clear ( ) ;
520
487
}
521
488
} finally {
522
489
is_flushing = false ;
523
490
set_is_updating_effect ( was_updating_effect ) ;
524
-
525
- last_scheduled_effect = null ;
526
491
}
527
492
}
528
493
@@ -537,7 +502,7 @@ function infinite_loop_guard() {
537
502
538
503
// Best effort: invoke the boundary nearest the most recent
539
504
// effect and hope that it's relevant to the infinite loop
540
- invoke_error_boundary ( error , last_scheduled_effect ) ;
505
+ invoke_error_boundary ( error , active_effect ) ;
541
506
}
542
507
}
543
508
@@ -598,7 +563,7 @@ function flush_queued_effects(effects) {
598
563
* @returns {void }
599
564
*/
600
565
export function schedule_effect ( signal ) {
601
- var effect = ( last_scheduled_effect = signal ) ;
566
+ var effect = signal ;
602
567
603
568
while ( effect . parent !== null ) {
604
569
effect = effect . parent ;
@@ -616,6 +581,34 @@ export function schedule_effect(signal) {
616
581
}
617
582
}
618
583
584
+ if ( queued_root_effects . length === 0 && flush_count ++ > 1000 ) {
585
+ flush_count = 0 ;
586
+
587
+ if ( DEV && current_batch !== null ) {
588
+ var updates = new Map ( ) ;
589
+
590
+ for ( const source of current_batch . current . keys ( ) ) {
591
+ for ( const [ stack , update ] of source . updated ?? [ ] ) {
592
+ var entry = updates . get ( stack ) ;
593
+
594
+ if ( ! entry ) {
595
+ entry = { error : update . error , count : 0 } ;
596
+ updates . set ( stack , entry ) ;
597
+ }
598
+
599
+ entry . count += update . count ;
600
+ }
601
+ }
602
+
603
+ for ( const update of updates . values ( ) ) {
604
+ // eslint-disable-next-line no-console
605
+ console . error ( update . error ) ;
606
+ }
607
+ }
608
+
609
+ infinite_loop_guard ( ) ;
610
+ }
611
+
619
612
queued_root_effects . push ( effect ) ;
620
613
}
621
614
0 commit comments