Skip to content

Commit 2c64556

Browse files
authored
chore: simplify batch logic a bit more (#16845)
* more * remove unnecessary flushSync * simplify
1 parent a9ab2af commit 2c64556

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

packages/svelte/src/internal/client/dom/blocks/await.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
set_dev_current_component_function,
2323
set_dev_stack
2424
} from '../../context.js';
25-
import { flushSync } from '../../reactivity/batch.js';
25+
import { flushSync, is_flushing_sync } from '../../reactivity/batch.js';
2626

2727
const PENDING = 0;
2828
const THEN = 1;
@@ -126,7 +126,7 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
126126

127127
// without this, the DOM does not update until two ticks after the promise
128128
// resolves, which is unexpected behaviour (and somewhat irksome to test)
129-
flushSync();
129+
if (!is_flushing_sync) flushSync();
130130
}
131131
}
132132
}

packages/svelte/src/internal/client/dom/task.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ function run_micro_tasks() {
1010
run_all(tasks);
1111
}
1212

13-
export function has_pending_tasks() {
14-
return micro_tasks.length > 0;
15-
}
16-
1713
/**
1814
* @param {() => void} fn
1915
*/
@@ -40,7 +36,7 @@ export function queue_micro_task(fn) {
4036
* Synchronously run any queued tasks.
4137
*/
4238
export function flush_tasks() {
43-
if (micro_tasks.length > 0) {
39+
while (micro_tasks.length > 0) {
4440
run_micro_tasks();
4541
}
4642
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
update_effect
2424
} from '../runtime.js';
2525
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';
2727
import { DEV } from 'esm-env';
2828
import { invoke_error_boundary } from '../error-handling.js';
2929
import { old_values } from './sources.js';
@@ -355,20 +355,18 @@ export class Batch {
355355
}
356356

357357
flush() {
358-
this.activate();
359-
360358
if (queued_root_effects.length > 0) {
359+
this.activate();
361360
flush_effects();
361+
362+
if (current_batch !== null && current_batch !== this) {
363+
// this can happen if a new batch was created during `flush_effects()`
364+
return;
365+
}
362366
} else if (this.#pending === 0) {
363367
this.#commit();
364368
}
365369

366-
if (current_batch !== null && current_batch !== this) {
367-
// this can happen if a `flushSync` occurred during `flush_effects()`,
368-
// which is permitted in legacy mode despite being a terrible idea
369-
return;
370-
}
371-
372370
this.deactivate();
373371
}
374372

@@ -468,14 +466,17 @@ export function flushSync(fn) {
468466
var result;
469467

470468
if (fn) {
471-
flush_effects();
469+
if (current_batch !== null) {
470+
flush_effects();
471+
}
472+
472473
result = fn();
473474
}
474475

475476
while (true) {
476477
flush_tasks();
477478

478-
if (queued_root_effects.length === 0 && !has_pending_tasks()) {
479+
if (queued_root_effects.length === 0) {
479480
current_batch?.flush();
480481

481482
// we need to check again, in case we just updated an `$effect.pending()`

0 commit comments

Comments
 (0)