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( const body = b.function_declaration(
b.id('$$body'), b.id('$$body'),
[b.id('$$anchor'), b.id('$$props')], [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; body.async = true;

@ -1,7 +1,6 @@
/** @import { AwaitExpression, Expression } from 'estree' */ /** @import { AwaitExpression, Expression } from 'estree' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
import * as b from '../../../../utils/builders.js'; import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js';
/** /**
* @param {AwaitExpression} node * @param {AwaitExpression} node
@ -14,22 +13,9 @@ export function AwaitExpression(node, context) {
return context.next(); return context.next();
} }
const inside_derived = context.path.some( return b.call(
(n) =>
n.type === 'VariableDeclaration' &&
n.declarations.some(
(d) =>
d.init?.type === 'CallExpression' && get_rune(d.init, context.state.scope) === '$derived'
)
);
const expression = b.call(
b.await( b.await(
b.call('$.save', node.argument && /** @type {Expression} */ (context.visit(node.argument))) 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() { export function capture() {
var previous_effect = active_effect; var previous_effect = active_effect;
var previous_reaction = active_reaction; var previous_reaction = active_reaction;
var previous_component_context = component_context; var previous_component_context = component_context;
return function restore(should_exit = true) { return function restore() {
set_active_effect(previous_effect); set_active_effect(previous_effect);
set_active_reaction(previous_reaction); set_active_reaction(previous_reaction);
set_component_context(previous_component_context); set_component_context(previous_component_context);
// prevent the active effect from outstaying its welcome // 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 * @template T
* @param {Promise<T>} promise * @param {Promise<T>} promise
@ -326,7 +305,7 @@ export async function save(promise) {
}; };
} }
export function exit() { function exit() {
set_active_effect(null); set_active_effect(null);
set_active_reaction(null); set_active_reaction(null);
set_component_context(null); set_component_context(null);

@ -130,7 +130,7 @@ export {
update_store, update_store,
mark_store_binding mark_store_binding
} from './reactivity/store.js'; } 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 { set_text } from './render.js';
export { export {
get, get,

Loading…
Cancel
Save