wrap in try-catch

using-dispose
Rich Harris 3 months ago
parent 56a396ec1a
commit 97b78b37b8

@ -350,7 +350,7 @@ export function client_component(analysis, options) {
const push_args = [b.id('$$props'), b.literal(analysis.runes)]; const push_args = [b.id('$$props'), b.literal(analysis.runes)];
if (dev) push_args.push(b.id(analysis.name)); if (dev) push_args.push(b.id(analysis.name));
const component_block = b.block([ let component_block = b.block([
...store_setup, ...store_setup,
...legacy_reactive_declarations, ...legacy_reactive_declarations,
...group_binding_declarations, ...group_binding_declarations,
@ -362,10 +362,6 @@ export function client_component(analysis, options) {
.../** @type {ESTree.Statement[]} */ (template.body) .../** @type {ESTree.Statement[]} */ (template.body)
]); ]);
if (analysis.disposable.length > 0) {
component_block.body.push(b.stmt(b.call('$.dispose', ...analysis.disposable)));
}
if (!analysis.runes) { if (!analysis.runes) {
// Bind static exports to props so that people can access them with bind:x // Bind static exports to props so that people can access them with bind:x
for (const { name, alias } of analysis.exports) { for (const { name, alias } of analysis.exports) {
@ -495,6 +491,16 @@ export function client_component(analysis, options) {
body = [...imports, ...state.module_level_snippets, ...body]; body = [...imports, ...state.module_level_snippets, ...body];
if (analysis.disposable.length > 0) {
component_block = b.block([
b.declaration(
'var',
analysis.disposable.map((id) => b.declarator(id))
),
b.try(component_block.body, null, [b.stmt(b.call('$.dispose', ...analysis.disposable))])
]);
}
const component = b.function_declaration( const component = b.function_declaration(
b.id(analysis.name), b.id(analysis.name),
should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')], should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')],

@ -351,14 +351,14 @@ export function VariableDeclaration(node, context) {
...node.declarations.map((declarator) => /** @type {Identifier} */ (declarator.id)) ...node.declarations.map((declarator) => /** @type {Identifier} */ (declarator.id))
); );
if (dev) { const assignments = declarations.map((declarator) => {
declarations = declarations.map((declarator) => ({ let init = /** @type {Expression} */ (declarator.init);
...declarator, if (dev) init = b.call('$.disposable', init);
init: b.call('$.disposable', /** @type {Expression} */ (declarator.init))
})); return b.assignment('=', declarator.id, init);
} });
kind = 'const'; return assignments.length === 1 ? assignments[0] : b.sequence(assignments);
} }
return { return {

@ -635,6 +635,35 @@ export function throw_error(str) {
}; };
} }
/**
* @param {ESTree.Statement[]} body
* @param {ESTree.CatchClause | null} handler
* @param {ESTree.Statement[] | null} finalizer
* @returns {ESTree.TryStatement}
*/
function try_builder(body, handler, finalizer) {
return {
type: 'TryStatement',
block: block(body),
handler,
finalizer: finalizer && block(finalizer)
};
}
/**
*
* @param {ESTree.Pattern | null} param
* @param {ESTree.Statement[]} body
* @returns {ESTree.CatchClause}
*/
function catch_clause(param, body) {
return {
type: 'CatchClause',
param,
body: block(body)
};
}
export { export {
await_builder as await, await_builder as await,
let_builder as let, let_builder as let,
@ -648,7 +677,9 @@ export {
if_builder as if, if_builder as if,
this_instance as this, this_instance as this,
null_instance as null, null_instance as null,
debugger_builder as debugger debugger_builder as debugger,
try_builder as try,
catch_clause as catch
}; };
/** /**

Loading…
Cancel
Save