separate suspend and capture from save

aaa
Rich Harris 8 months ago
parent 78ffe81598
commit df67772711

@ -1,7 +1,7 @@
/** @import { TemplateNode, Value } from '#client' */
import { async_derived } from '../../reactivity/deriveds.js';
import { save } from './boundary.js';
import { capture, suspend } from './boundary.js';
/**
* @param {TemplateNode} node
@ -11,7 +11,12 @@ import { save } from './boundary.js';
export function async(node, expressions, fn) {
// TODO handle hydration
save(Promise.all(expressions.map(async_derived))).then((result) => {
fn(node, ...result.restore());
var restore = capture();
var unsuspend = suspend();
Promise.all(expressions.map(async_derived)).then((result) => {
restore();
fn(node, ...result);
unsuspend();
});
}

@ -268,6 +268,30 @@ export function capture() {
};
}
export function suspend() {
var boundary = active_effect;
while (boundary !== null) {
if ((boundary.f & BOUNDARY_EFFECT) !== 0) {
break;
}
boundary = boundary.parent;
}
if (boundary === null) {
e.await_outside_boundary();
}
// @ts-ignore
boundary?.fn(ASYNC_INCREMENT);
return function unsuspend() {
// @ts-ignore
boundary?.fn(ASYNC_DECREMENT);
};
}
/**
* @template T
* @param {Promise<T>} promise

Loading…
Cancel
Save