Skip to content

Commit 63d3dd5

Browse files
committed
chore: simplify infinite_loop_guard
1 parent 82e431f commit 63d3dd5

File tree

1 file changed

+34
-41
lines changed
  • packages/svelte/src/internal/client/reactivity

1 file changed

+34
-41
lines changed

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ function dequeue() {
6464
/** @type {Effect[]} */
6565
let queued_root_effects = [];
6666

67-
/** @type {Effect | null} */
68-
let last_scheduled_effect = null;
69-
7067
let is_flushing = false;
7168

7269
let is_flushing_sync = false;
7370

71+
let flush_count = 0;
72+
7473
export class Batch {
7574
/**
7675
* The current values of any sources that are updated in this batch
@@ -463,10 +462,6 @@ export function flushSync(fn) {
463462

464463
// we need to check again, in case we just updated an `$effect.pending()`
465464
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-
470465
return /** @type {T} */ (result);
471466
}
472467
}
@@ -483,46 +478,16 @@ function flush_effects() {
483478
is_flushing = true;
484479

485480
try {
486-
var flush_count = 0;
481+
flush_count = 0;
487482
set_is_updating_effect(true);
488483

489484
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);
519486
old_values.clear();
520487
}
521488
} finally {
522489
is_flushing = false;
523490
set_is_updating_effect(was_updating_effect);
524-
525-
last_scheduled_effect = null;
526491
}
527492
}
528493

@@ -537,7 +502,7 @@ function infinite_loop_guard() {
537502

538503
// Best effort: invoke the boundary nearest the most recent
539504
// 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);
541506
}
542507
}
543508

@@ -598,7 +563,7 @@ function flush_queued_effects(effects) {
598563
* @returns {void}
599564
*/
600565
export function schedule_effect(signal) {
601-
var effect = (last_scheduled_effect = signal);
566+
var effect = signal;
602567

603568
while (effect.parent !== null) {
604569
effect = effect.parent;
@@ -616,6 +581,34 @@ export function schedule_effect(signal) {
616581
}
617582
}
618583

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+
619612
queued_root_effects.push(effect);
620613
}
621614

0 commit comments

Comments
 (0)