pull/16197/head
Rich Harris 5 months ago
parent 3d0b6f71c4
commit 48a781e2b1

@ -15,14 +15,16 @@ export function async(node, expressions, fn) {
var batch = /** @type {Batch} */ (current_batch); var batch = /** @type {Batch} */ (current_batch);
var effect = /** @type {Effect} */ (active_effect); var effect = /** @type {Effect} */ (active_effect);
var boundary = get_pending_boundary(effect); var boundary = get_pending_boundary(effect);
var ran = boundary.ran;
var restore = capture(); var restore = capture();
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?.restore(); if (ran) batch.restore();
restore(); restore();
fn(node, ...result); fn(node, ...result);
@ -30,7 +32,7 @@ export function async(node, expressions, fn) {
// TODO is this necessary? // TODO is this necessary?
schedule_effect(effect); schedule_effect(effect);
batch?.flush(); if (ran) batch.flush();
boundary.decrement(); boundary.decrement();
}); });
} }

@ -74,6 +74,8 @@ export class Batch {
} }
for (const [source, current] of this.#current) { for (const [source, current] of this.#current) {
current_values.set(source, source.v);
// TODO this shouldn't be necessary, but tests fail otherwise, // TODO this shouldn't be necessary, but tests fail otherwise,
// presumably because we need a try-finally somewhere, and the // presumably because we need a try-finally somewhere, and the
// source wasn't correctly reverted after the previous batch // source wasn't correctly reverted after the previous batch
@ -214,16 +216,16 @@ export class Batch {
raf.tick(update_pending); raf.tick(update_pending);
} }
current_batch = new Batch(); const batch = (current_batch = new Batch());
batches.add(current_batch); batches.add(current_batch);
queueMicrotask(() => { queueMicrotask(() => {
if (current_batch === null) { if (current_batch !== batch) {
// a flushSync happened in the meantime // a flushSync happened in the meantime
return; return;
} }
current_batch.flush(); batch.flush();
}); });
} }

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

@ -40,7 +40,7 @@ import { DEV } from 'esm-env';
import { define_property } from '../../shared/utils.js'; import { define_property } from '../../shared/utils.js';
import { get_next_sibling } from '../dom/operations.js'; import { get_next_sibling } from '../dom/operations.js';
import { async_derived, derived } from './deriveds.js'; import { async_derived, derived } from './deriveds.js';
import { capture } from '../dom/blocks/boundary.js'; import { capture, get_pending_boundary } from '../dom/blocks/boundary.js';
import { component_context, dev_current_component_function } from '../context.js'; import { component_context, dev_current_component_function } from '../context.js';
import { current_batch, Batch } from './batch.js'; import { current_batch, Batch } from './batch.js';
@ -348,6 +348,9 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
var batch = /** @type {Batch} */ (current_batch); var batch = /** @type {Batch} */ (current_batch);
var restore = capture(); var restore = capture();
var boundary = get_pending_boundary(parent);
var ran = boundary.ran;
Promise.all(async.map((expression) => async_derived(expression))).then((result) => { Promise.all(async.map((expression) => async_derived(expression))).then((result) => {
restore(); restore();
@ -357,9 +360,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?.restore(); if (ran) batch.restore();
schedule_effect(effect); schedule_effect(effect);
batch?.flush(); if (ran) batch.flush();
}); });
} else { } else {
create_template_effect(fn, sync.map(d)); create_template_effect(fn, sync.map(d));

Loading…
Cancel
Save