remove script_suspend in favour of component-level suspending

aaa
Rich Harris 7 months ago
parent 38934893df
commit 3dd1d30d90

@ -371,7 +371,11 @@ export function client_component(analysis, options) {
const body = b.function_declaration(
b.id('$$body'),
[b.id('$$anchor'), b.id('$$props')],
b.block([...component_block.body, b.stmt(b.call('$.exit'))])
b.block([
b.var('$$unsuspend', b.call('$.suspend')),
...component_block.body,
b.stmt(b.call('$$unsuspend'))
])
);
body.async = true;

@ -1,7 +1,6 @@
/** @import { AwaitExpression, Expression } from 'estree' */
/** @import { Context } from '../types' */
import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js';
/**
* @param {AwaitExpression} node
@ -14,22 +13,9 @@ export function AwaitExpression(node, context) {
return context.next();
}
const inside_derived = context.path.some(
(n) =>
n.type === 'VariableDeclaration' &&
n.declarations.some(
(d) =>
d.init?.type === 'CallExpression' && get_rune(d.init, context.state.scope) === '$derived'
)
);
const expression = b.call(
return b.call(
b.await(
b.call('$.save', node.argument && /** @type {Expression} */ (context.visit(node.argument)))
)
);
return inside_derived
? expression
: b.await(b.call('$.script_suspend', b.arrow([], expression, true)));
}

@ -243,23 +243,18 @@ export function boundary(node, props, boundary_fn) {
}
}
// TODO separate this stuff out — suspending and context preservation should
// be distinct concepts
export function capture() {
var previous_effect = active_effect;
var previous_reaction = active_reaction;
var previous_component_context = component_context;
return function restore(should_exit = true) {
return function restore() {
set_active_effect(previous_effect);
set_active_reaction(previous_reaction);
set_component_context(previous_component_context);
// prevent the active effect from outstaying its welcome
if (should_exit) {
queue_post_micro_task(exit);
}
queue_post_micro_task(exit);
};
}
@ -295,22 +290,6 @@ export function suspend() {
};
}
/**
* @template T
* @param {() => Promise<T>} fn
*/
export async function script_suspend(fn) {
const restore = capture();
const unsuspend = suspend();
try {
exit();
return await fn();
} finally {
restore(false);
unsuspend();
}
}
/**
* @template T
* @param {Promise<T>} promise
@ -326,7 +305,7 @@ export async function save(promise) {
};
}
export function exit() {
function exit() {
set_active_effect(null);
set_active_reaction(null);
set_component_context(null);

@ -130,7 +130,7 @@ export {
update_store,
mark_store_binding
} from './reactivity/store.js';
export { boundary, exit, save, suspend, script_suspend } from './dom/blocks/boundary.js';
export { boundary, save, suspend } from './dom/blocks/boundary.js';
export { set_text } from './render.js';
export {
get,

Loading…
Cancel
Save