hoist functions, use names to make stuff a little clearer

aa
Rich Harris 8 months ago
parent 5a4b11b78b
commit 1c4db3d341

@ -83,7 +83,10 @@ export function boundary(node, props, boundary_fn) {
var hydrate_open = hydrate_node; var hydrate_open = hydrate_node;
var is_creating_fallback = false; var is_creating_fallback = false;
var render_snippet = (/** @type { () => void } */ snippet_fn) => { /**
* @param {() => void} snippet_fn
*/
function render_snippet(snippet_fn) {
with_boundary(boundary, () => { with_boundary(boundary, () => {
is_creating_fallback = true; is_creating_fallback = true;
@ -98,15 +101,9 @@ export function boundary(node, props, boundary_fn) {
reset_is_throwing_error(); reset_is_throwing_error();
is_creating_fallback = false; is_creating_fallback = false;
}); });
}; }
// @ts-ignore We re-use the effect's fn property to avoid allocation of an additional field
boundary.fn = (/** @type {unknown} */ input) => {
let pending = /** @type {(anchor: Node) => void} */ (props.pending);
if (input === ASYNC_INCREMENT) { function suspend() {
if (async_count++ === 0) {
queue_boundary_micro_task(() => {
if (async_effect || !boundary_effect) { if (async_effect || !boundary_effect) {
return; return;
} }
@ -135,32 +132,56 @@ export function boundary(node, props, boundary_fn) {
false false
); );
const pending = props.pending;
if (pending) {
render_snippet(() => { render_snippet(() => {
pending(anchor); pending(anchor);
}); });
});
} }
return true;
} }
if (input === ASYNC_DECREMENT) { function unsuspend() {
if (--async_count === 0) {
queue_boundary_micro_task(() => {
if (!async_effect) { if (!async_effect) {
return; return;
} }
if (boundary_effect) { if (boundary_effect) {
destroy_effect(boundary_effect); destroy_effect(boundary_effect);
} }
boundary_effect = async_effect; boundary_effect = async_effect;
async_effect = null; async_effect = null;
anchor.before(/** @type {DocumentFragment} */ (async_fragment)); anchor.before(/** @type {DocumentFragment} */ (async_fragment));
resume_effect(boundary_effect); resume_effect(boundary_effect);
}
function reset() {
pause_effect(boundary_effect);
with_boundary(boundary, () => {
is_creating_fallback = false;
boundary_effect = branch(() => boundary_fn(anchor));
reset_is_throwing_error();
}); });
} }
return true; // @ts-ignore We re-use the effect's fn property to avoid allocation of an additional field
boundary.fn = (/** @type {unknown} */ input) => {
if (input === ASYNC_INCREMENT) {
if (async_count++ === 0) {
queue_boundary_micro_task(suspend);
}
return;
}
if (input === ASYNC_DECREMENT) {
if (--async_count === 0) {
queue_boundary_micro_task(unsuspend);
}
return;
} }
var error = input; var error = input;
@ -169,20 +190,10 @@ export function boundary(node, props, boundary_fn) {
// If we have nothing to capture the error, or if we hit an error while // If we have nothing to capture the error, or if we hit an error while
// rendering the fallback, re-throw for another boundary to handle // rendering the fallback, re-throw for another boundary to handle
if ((!onerror && !failed) || is_creating_fallback) { if (is_creating_fallback || (!onerror && !failed)) {
throw error; throw error;
} }
var reset = () => {
pause_effect(boundary_effect);
with_boundary(boundary, () => {
is_creating_fallback = false;
boundary_effect = branch(() => boundary_fn(anchor));
reset_is_throwing_error();
});
};
onerror?.(error, reset); onerror?.(error, reset);
if (boundary_effect) { if (boundary_effect) {

Loading…
Cancel
Save