Fixed a lot of runtime issues

pull/6585/head
rster20002 5 years ago
parent 5343883edf
commit c8736dd81f

@ -375,6 +375,36 @@ export default class Block {
} }
} }
if (properties.create.type === 'FunctionExpression') {
properties.create.body.body = b`
try {
${properties.create.body.body}
} catch (e) {
@handle_error(@get_current_component(), e);
}
`;
}
if (properties.mount.type === 'FunctionExpression') {
properties.mount.body.body = b`
try {
${properties.mount.body.body}
} catch (e) {
@handle_error(@get_current_component(), e);
}
`;
}
if (properties.hydrate?.type === 'FunctionExpression') {
properties.hydrate.body.body = b`
try {
${properties.hydrate.body.body}
} catch (e) {
@handle_error(@get_current_component(), e);
}
`;
}
const return_value: any = x`{ const return_value: any = x`{
key: ${properties.key}, key: ${properties.key},
first: ${properties.first}, first: ${properties.first},
@ -395,6 +425,7 @@ export default class Block {
const init_declarations = []; const init_declarations = [];
const init_statements = []; const init_statements = [];
const init_functions = [];
Array.from(this.variables.values()).forEach(({ id, init }) => { Array.from(this.variables.values()).forEach(({ id, init }) => {
init_declarations.push(b`let ${id};`); init_declarations.push(b`let ${id};`);
@ -407,9 +438,12 @@ export default class Block {
this.chunks.init.forEach(node => { this.chunks.init.forEach(node => {
if (Array.isArray(node)) { if (Array.isArray(node)) {
node.forEach((declaration: any) => { // TODO add type to this node.forEach((declaration: any) => { // TODO add type to this
if (declaration.declarations) { if (declaration.type === 'FunctionDeclaration') {
init_declarations.push(b`let ${declaration.id};`);
init_functions.push(b`${declaration.id} = ${declaration}`);
} else if (declaration.type === 'VariableDeclaration') {
declaration.declarations.forEach(({ id, init }) => { declaration.declarations.forEach(({ id, init }) => {
init_declarations.push(b`let ${id}`); init_declarations.push(b`let ${id}`); // TODO declaration is not always `let`
init_statements.push(b`${id} = ${init}`); init_statements.push(b`${id} = ${init}`);
}); });
} else { } else {
@ -426,9 +460,11 @@ export default class Block {
${init_declarations} ${init_declarations}
${init_statements.length > 0 ${init_statements.length > 0 || init_functions.length > 0
? b` ? b`
try { try {
${init_functions}
${init_statements} ${init_statements}
} catch (e) { } catch (e) {
@handle_error(@get_current_component(), e); @handle_error(@get_current_component(), e);
@ -490,7 +526,6 @@ export default class Block {
render_listeners(chunk: string = '') { render_listeners(chunk: string = '') {
if (this.event_listeners.length > 0) { if (this.event_listeners.length > 0) {
this.add_variable({ type: 'Identifier', name: '#mounted' }); this.add_variable({ type: 'Identifier', name: '#mounted' });
this.chunks.destroy.push(b`#mounted = false`);
const dispose: Identifier = { const dispose: Identifier = {
type: 'Identifier', type: 'Identifier',
@ -499,10 +534,6 @@ export default class Block {
this.add_variable(dispose); this.add_variable(dispose);
this.event_listeners.forEach((event_listener: any) => {
event_listener.arguments[2] = x`@attach_error_handler(${event_listener.arguments[0]}, @get_current_component(), ${event_listener.arguments[2]})`;
});
if (this.event_listeners.length === 1) { if (this.event_listeners.length === 1) {
this.chunks.mount.push( this.chunks.mount.push(
b` b`
@ -514,7 +545,11 @@ export default class Block {
); );
this.chunks.destroy.push( this.chunks.destroy.push(
b`${dispose}();` b`
if (#mounted) {
${dispose}();
}
`
); );
} else { } else {
this.chunks.mount.push(b` this.chunks.mount.push(b`
@ -530,6 +565,8 @@ export default class Block {
b`@run_all(${dispose});` b`@run_all(${dispose});`
); );
} }
this.chunks.destroy.push(b`#mounted = false`);
} }
} }
} }

@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
import { extract_names, Scope } from 'periscopic'; import { extract_names, Scope } from 'periscopic';
import { invalidate } from './invalidate'; import { invalidate } from './invalidate';
import Block from './Block'; import Block from './Block';
import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression, Identifier } from 'estree'; import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree';
import { apply_preprocessor_sourcemap } from '../../utils/mapped_code'; import { apply_preprocessor_sourcemap } from '../../utils/mapped_code';
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types'; import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { flatten } from '../../utils/flatten'; import { flatten } from '../../utils/flatten';
@ -462,16 +462,12 @@ export default function dom(
instance_javascript_with_ctx.push(node); instance_javascript_with_ctx.push(node);
if (Array.isArray(node) && node[0].type === 'VariableDeclaration') { if (Array.isArray(node) && node[0].type === 'VariableDeclaration') {
walk(node[0], { node[0].declarations.forEach(({ id }) => {
enter(declaration: Identifier) { const index = renderer.initial_context.findIndex(member => member.name === id.name);
if (declaration.type === 'Identifier' && !initializedIdentifiers.includes(declaration.name)) {
const index = renderer.initial_context.findIndex(member => member.name === declaration.name); if (index >= 0) {
instance_javascript_with_ctx.push(b`#return_values[${index}] = ${id};`[0]);
if (index >= 0) { initializedIdentifiers.push(id.name);
node.push(x`#return_values[${index}] = ${declaration}`);
initializedIdentifiers.push(declaration.name);
}
}
} }
}); });
} }
@ -481,7 +477,7 @@ export default function dom(
const index = renderer.initial_context.findIndex(member => member.name === node.id.name); const index = renderer.initial_context.findIndex(member => member.name === node.id.name);
if (index >= 0) { if (index >= 0) {
instance_javascript_with_ctx.push(x`#return_values[${index}] = ${node.id.name}`); instance_javascript_with_ctx.push(b`#return_values[${index}] = ${node.id};`[0]);
initializedIdentifiers.push(node.id.name); initializedIdentifiers.push(node.id.name);
} }
} }
@ -492,6 +488,12 @@ export default function dom(
const instance_try_block: any = b` const instance_try_block: any = b`
try { try {
${reactive_store_declarations}
${reactive_store_subscriptions}
${resubscribable_reactive_store_unsubscribers}
${instance_javascript_with_ctx} ${instance_javascript_with_ctx}
${unknown_props_check} ${unknown_props_check}
@ -534,12 +536,6 @@ export default function dom(
${rest} ${rest}
${reactive_store_declarations}
${reactive_store_subscriptions}
${resubscribable_reactive_store_unsubscribers}
${component.slots.size || component.compile_options.dev || uses_slots ? b`let { $$slots: #slots = {}, $$scope } = $$props;` : null} ${component.slots.size || component.compile_options.dev || uses_slots ? b`let { $$slots: #slots = {}, $$scope } = $$props;` : null}
${component.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`} ${component.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`}
${compute_slots} ${compute_slots}

@ -67,7 +67,7 @@ export default class EventHandlerWrapper {
} }
block.event_listeners.push( block.event_listeners.push(
x`@listen(${target}, "${this.node.name}", ${snippet}, ${args})` x`@listen(${target}, "${this.node.name}", @attach_error_handler(${target}, @get_current_component(), ${snippet}), ${args})`
); );
} }
} }

@ -541,7 +541,7 @@ export default class ElementWrapper extends Wrapper {
); );
} else { } else {
block.event_listeners.push( block.event_listeners.push(
x`@listen(${this.var}, "${name}", ${callee})` x`@listen(${this.var}, "${name}", @attach_error_handler(${this.var}, @get_current_component(), ${callee}))`
); );
} }
}); });

@ -40,7 +40,7 @@ export function add_action(block: Block, target: string, action: Action) {
); );
} else { } else {
block.event_listeners.push( block.event_listeners.push(
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))` x`@action_destroyer(${id} = @attach_error_handler(null, @get_current_component(), ${fn}).call(null, ${target}, ${snippet}))`
); );
} }

@ -182,35 +182,25 @@ export default function ssr(
return $$rendered; return $$rendered;
` `
: b` : b`
try { ${reactive_declarations}
${instance_javascript}
${reactive_declarations} ${reactive_store_unsubscriptions}
return ${literal};`;
const blocks = [
...injected.map(name => b`let ${name};`),
rest,
slots,
...reactive_store_declarations,
...reactive_store_subscriptions,
instance_javascript,
...parent_bindings,
css.code && b`$$result.css.add(#css);`,
main
].filter(Boolean);
${reactive_store_unsubscriptions}
return ${literal};
} catch (e) {
@handle_error(@get_current_component(), e);
}`;
const blocks = [
...injected.map(name => b`let ${name};`),
rest,
slots,
...reactive_store_declarations,
...reactive_store_subscriptions,
// b`
// try {
// ${instance_javascript}
// } catch (e) {
// @handle_error(@get_current_component(), e);
// }
// `,
...parent_bindings,
css.code && b`$$result.css.add(#css);`,
main
].filter(Boolean);
const js = b` const js = b`
${css.code ? b` ${css.code ? b`
@ -224,7 +214,11 @@ export default function ssr(
${component.fully_hoisted} ${component.fully_hoisted}
const ${name} = @create_ssr_component(($$result, $$props, $$bindings, #slots) => { const ${name} = @create_ssr_component(($$result, $$props, $$bindings, #slots) => {
${blocks} try {
${blocks}
} catch (e) {
@handle_error(@get_current_component(), e);
}
}); });
`; `;

@ -109,7 +109,7 @@ function make_dirty(component, i) {
export function attach_error_handler(that: any, component, fn) { export function attach_error_handler(that: any, component, fn) {
return (...rest) => { return (...rest) => {
try { try {
fn.call(that, ...rest); return fn.call(that, ...rest);
} catch (e) { } catch (e) {
handle_error(component, e); handle_error(component, e);
} }

@ -1,4 +1,6 @@
import { Readable } from 'svelte/store'; import { Readable } from 'svelte/store';
import { attach_error_handler } from './Component';
import { get_current_component } from './lifecycle';
export function noop() {} export function noop() {}
@ -185,5 +187,5 @@ export function set_store_value(store, ret, value) {
export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
export function action_destroyer(action_result) { export function action_destroyer(action_result) {
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop; return action_result && is_function(action_result.destroy) ? attach_error_handler(action_result, get_current_component(), action_result.destroy) : noop;
} }

Loading…
Cancel
Save