Skip to content

Commit ed0a393

Browse files
authored
chore: simplify batch logic (#16838)
* always delete a batch once committed * activate inside flush * centralise logic * chore: simplify batch logic a bit more (#16845) * more * remove unnecessary flushSync * simplify
1 parent f0cede6 commit ed0a393

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
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/async.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ export async function async_body(fn) {
244244
if (pending) {
245245
batch.flush();
246246
} else {
247-
batch.activate();
248247
batch.decrement();
249248
}
250249

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

Lines changed: 14 additions & 23 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';
@@ -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

Comments
 (0)