pull/16197/head
Rich Harris 5 months ago
parent b68dcdcf7e
commit 5e8bcfa8cc

@ -22,14 +22,15 @@ export function async(node, expressions, fn) {
boundary.increment(); boundary.increment();
Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => { Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => {
batch.run(() => { batch?.restore();
restore(); restore();
fn(node, ...result); fn(node, ...result);
// TODO is this necessary? // TODO is this necessary?
schedule_effect(effect); schedule_effect(effect);
});
batch?.flush();
boundary.decrement(); boundary.decrement();
}); });
} }

@ -2,6 +2,7 @@
import { CLEAN, DIRTY } from '#client/constants'; import { CLEAN, DIRTY } from '#client/constants';
import { import {
flush_queued_effects, flush_queued_effects,
flush_queued_root_effects,
process_effects, process_effects,
schedule_effect, schedule_effect,
set_queued_root_effects, set_queued_root_effects,
@ -17,10 +18,6 @@ const batches = new Set();
/** @type {Batch | null} */ /** @type {Batch | null} */
export let current_batch = null; export let current_batch = null;
export function remove_current_batch() {
current_batch = null;
}
/** Update `$effect.pending()` */ /** Update `$effect.pending()` */
function update_pending() { function update_pending() {
internal_set(pending, batches.size > 0); internal_set(pending, batches.size > 0);
@ -149,12 +146,21 @@ export class Batch {
} }
} }
/** restore() {
* @param {() => void} fn
*/
run(fn) {
current_batch = this; current_batch = this;
fn(); }
flush() {
flush_queued_root_effects();
// TODO can this happen?
if (current_batch !== this) return;
if (this.settled()) {
this.remove();
}
current_batch = null;
} }
commit() { commit() {
@ -210,6 +216,15 @@ export class Batch {
current_batch = new Batch(); current_batch = new Batch();
batches.add(current_batch); batches.add(current_batch);
queueMicrotask(() => {
if (current_batch === null) {
// a flushSync happened in the meantime
return;
}
current_batch.flush();
});
} }
return current_batch; return current_batch;

@ -162,9 +162,9 @@ export function async_derived(fn, location) {
} }
} }
batch.run(() => { batch?.restore();
internal_set(signal, v); internal_set(signal, v);
}); batch?.flush();
if (DEV && location !== undefined) { if (DEV && location !== undefined) {
recent_async_deriveds.add(signal); recent_async_deriveds.add(signal);

@ -357,9 +357,9 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
var effect = create_template_effect(fn, [...sync.map(d), ...result]); var effect = create_template_effect(fn, [...sync.map(d), ...result]);
batch.run(() => { batch?.restore();
schedule_effect(effect); schedule_effect(effect);
}); batch?.flush();
}); });
} else { } else {
create_template_effect(fn, sync.map(d)); create_template_effect(fn, sync.map(d));

@ -51,7 +51,7 @@ import {
import { Boundary } from './dom/blocks/boundary.js'; import { Boundary } from './dom/blocks/boundary.js';
import * as w from './warnings.js'; import * as w from './warnings.js';
import { is_firefox } from './dom/operations.js'; import { is_firefox } from './dom/operations.js';
import { current_batch, Batch, remove_current_batch } from './reactivity/batch.js'; import { current_batch, Batch } from './reactivity/batch.js';
import { log_effect_tree, root } from './dev/debug.js'; import { log_effect_tree, root } from './dev/debug.js';
// Used for DEV time error handling // Used for DEV time error handling
@ -693,7 +693,7 @@ function infinite_loop_guard() {
} }
} }
function flush_queued_root_effects() { export function flush_queued_root_effects() {
var was_updating_effect = is_updating_effect; var was_updating_effect = is_updating_effect;
var batch = /** @type {Batch} */ (current_batch); var batch = /** @type {Batch} */ (current_batch);
@ -764,24 +764,6 @@ export function flush_queued_effects(effects) {
* @returns {void} * @returns {void}
*/ */
export function schedule_effect(signal) { export function schedule_effect(signal) {
if (!is_flushing) {
is_flushing = true;
queueMicrotask(() => {
if (current_batch === null) {
// a flushSync happened in the meantime
return;
}
flush_queued_root_effects();
if (current_batch?.settled()) {
current_batch.remove();
}
remove_current_batch();
});
}
var effect = (last_scheduled_effect = signal); var effect = (last_scheduled_effect = signal);
while (effect.parent !== null) { while (effect.parent !== null) {
@ -868,7 +850,7 @@ export function process_effects(batch, root) {
export function flushSync(fn) { export function flushSync(fn) {
var result; var result;
Batch.ensure(); const batch = Batch.ensure();
if (fn) { if (fn) {
is_flushing = true; is_flushing = true;
@ -884,12 +866,10 @@ export function flushSync(fn) {
flush_tasks(); flush_tasks();
} }
if (current_batch?.settled()) { if (batch === current_batch) {
current_batch.remove(); batch.flush();
} }
remove_current_batch();
return /** @type {T} */ (result); return /** @type {T} */ (result);
} }

Loading…
Cancel
Save