Skip to content

Commit 5e8bcfa

Browse files
committed
tweak/fix
1 parent b68dcdc commit 5e8bcfa

File tree

5 files changed

+42
-46
lines changed

5 files changed

+42
-46
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ export function async(node, expressions, fn) {
2222
boundary.increment();
2323

2424
Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => {
25-
batch.run(() => {
26-
restore();
27-
fn(node, ...result);
25+
batch?.restore();
2826

29-
// TODO is this necessary?
30-
schedule_effect(effect);
31-
});
27+
restore();
28+
fn(node, ...result);
3229

30+
// TODO is this necessary?
31+
schedule_effect(effect);
32+
33+
batch?.flush();
3334
boundary.decrement();
3435
});
3536
}

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { CLEAN, DIRTY } from '#client/constants';
33
import {
44
flush_queued_effects,
5+
flush_queued_root_effects,
56
process_effects,
67
schedule_effect,
78
set_queued_root_effects,
@@ -17,10 +18,6 @@ const batches = new Set();
1718
/** @type {Batch | null} */
1819
export let current_batch = null;
1920

20-
export function remove_current_batch() {
21-
current_batch = null;
22-
}
23-
2421
/** Update `$effect.pending()` */
2522
function update_pending() {
2623
internal_set(pending, batches.size > 0);
@@ -149,12 +146,21 @@ export class Batch {
149146
}
150147
}
151148

152-
/**
153-
* @param {() => void} fn
154-
*/
155-
run(fn) {
149+
restore() {
156150
current_batch = this;
157-
fn();
151+
}
152+
153+
flush() {
154+
flush_queued_root_effects();
155+
156+
// TODO can this happen?
157+
if (current_batch !== this) return;
158+
159+
if (this.settled()) {
160+
this.remove();
161+
}
162+
163+
current_batch = null;
158164
}
159165

160166
commit() {
@@ -210,6 +216,15 @@ export class Batch {
210216

211217
current_batch = new Batch();
212218
batches.add(current_batch);
219+
220+
queueMicrotask(() => {
221+
if (current_batch === null) {
222+
// a flushSync happened in the meantime
223+
return;
224+
}
225+
226+
current_batch.flush();
227+
});
213228
}
214229

215230
return current_batch;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ export function async_derived(fn, location) {
162162
}
163163
}
164164

165-
batch.run(() => {
166-
internal_set(signal, v);
167-
});
165+
batch?.restore();
166+
internal_set(signal, v);
167+
batch?.flush();
168168

169169
if (DEV && location !== undefined) {
170170
recent_async_deriveds.add(signal);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
357357

358358
var effect = create_template_effect(fn, [...sync.map(d), ...result]);
359359

360-
batch.run(() => {
361-
schedule_effect(effect);
362-
});
360+
batch?.restore();
361+
schedule_effect(effect);
362+
batch?.flush();
363363
});
364364
} else {
365365
create_template_effect(fn, sync.map(d));

packages/svelte/src/internal/client/runtime.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
import { Boundary } from './dom/blocks/boundary.js';
5252
import * as w from './warnings.js';
5353
import { is_firefox } from './dom/operations.js';
54-
import { current_batch, Batch, remove_current_batch } from './reactivity/batch.js';
54+
import { current_batch, Batch } from './reactivity/batch.js';
5555
import { log_effect_tree, root } from './dev/debug.js';
5656

5757
// Used for DEV time error handling
@@ -693,7 +693,7 @@ function infinite_loop_guard() {
693693
}
694694
}
695695

696-
function flush_queued_root_effects() {
696+
export function flush_queued_root_effects() {
697697
var was_updating_effect = is_updating_effect;
698698
var batch = /** @type {Batch} */ (current_batch);
699699

@@ -764,24 +764,6 @@ export function flush_queued_effects(effects) {
764764
* @returns {void}
765765
*/
766766
export function schedule_effect(signal) {
767-
if (!is_flushing) {
768-
is_flushing = true;
769-
queueMicrotask(() => {
770-
if (current_batch === null) {
771-
// a flushSync happened in the meantime
772-
return;
773-
}
774-
775-
flush_queued_root_effects();
776-
777-
if (current_batch?.settled()) {
778-
current_batch.remove();
779-
}
780-
781-
remove_current_batch();
782-
});
783-
}
784-
785767
var effect = (last_scheduled_effect = signal);
786768

787769
while (effect.parent !== null) {
@@ -868,7 +850,7 @@ export function process_effects(batch, root) {
868850
export function flushSync(fn) {
869851
var result;
870852

871-
Batch.ensure();
853+
const batch = Batch.ensure();
872854

873855
if (fn) {
874856
is_flushing = true;
@@ -884,12 +866,10 @@ export function flushSync(fn) {
884866
flush_tasks();
885867
}
886868

887-
if (current_batch?.settled()) {
888-
current_batch.remove();
869+
if (batch === current_batch) {
870+
batch.flush();
889871
}
890872

891-
remove_current_batch();
892-
893873
return /** @type {T} */ (result);
894874
}
895875

0 commit comments

Comments
 (0)