pull/16197/head
Rich Harris 4 months ago
parent cd24e51bc8
commit c8e47cc98d

@ -1,8 +1,5 @@
/** @import { Effect, TemplateNode, Value } from '#client' */ /** @import { TemplateNode, Value } from '#client' */
/** @import { Batch } from '../../reactivity/batch.js' */
import { async_derived } from '../../reactivity/deriveds.js'; import { async_derived } from '../../reactivity/deriveds.js';
import { current_batch } from '../../reactivity/batch.js';
import { active_effect, schedule_effect } from '../../runtime.js';
import { capture, get_pending_boundary } from './boundary.js'; import { capture, get_pending_boundary } from './boundary.js';
/** /**
@ -13,26 +10,15 @@ import { capture, get_pending_boundary } from './boundary.js';
export function async(node, expressions, fn) { export function async(node, expressions, fn) {
// TODO handle hydration // TODO handle hydration
var batch = /** @type {Batch} */ (current_batch);
var effect = /** @type {Effect} */ (active_effect);
var boundary = get_pending_boundary(effect);
var ran = boundary.ran;
var restore = capture(); var restore = capture();
var boundary = get_pending_boundary();
boundary.increment(); boundary.increment();
Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => { Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => {
if (ran) batch.restore();
restore(); restore();
fn(node, ...result); fn(node, ...result);
// TODO is this necessary?
schedule_effect(effect);
if (ran) batch.flush();
boundary.decrement(); boundary.decrement();
}); });
} }

@ -327,9 +327,8 @@ function move_effect(effect, fragment) {
} }
} }
/** @param {Effect} effect */ export function get_pending_boundary() {
export function get_pending_boundary(effect) { var boundary = /** @type {Effect} */ (active_effect).b;
let boundary = effect.b;
while (boundary !== null && !boundary.has_pending_snippet()) { while (boundary !== null && !boundary.has_pending_snippet()) {
boundary = boundary.parent; boundary = boundary.parent;
@ -367,7 +366,7 @@ export function capture(track = true) {
// TODO we should probably be incrementing the current batch, not the boundary? // TODO we should probably be incrementing the current batch, not the boundary?
export function suspend() { export function suspend() {
let boundary = get_pending_boundary(/** @type {Effect} */ (active_effect)); let boundary = get_pending_boundary();
boundary.increment(); boundary.increment();

@ -104,7 +104,7 @@ export function async_derived(fn, location) {
throw new Error('TODO cannot create unowned async derived'); throw new Error('TODO cannot create unowned async derived');
} }
let boundary = get_pending_boundary(parent); let boundary = get_pending_boundary();
var promise = /** @type {Promise<V>} */ (/** @type {unknown} */ (undefined)); var promise = /** @type {Promise<V>} */ (/** @type {unknown} */ (undefined));
var signal = source(/** @type {V} */ (UNINITIALIZED)); var signal = source(/** @type {V} */ (UNINITIALIZED));

@ -346,7 +346,7 @@ 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 boundary = get_pending_boundary();
var ran = boundary.ran; var ran = boundary.ran;
Promise.all(async.map((expression) => async_derived(expression))).then((result) => { Promise.all(async.map((expression) => async_derived(expression))).then((result) => {

Loading…
Cancel
Save