Skip to content

Commit

Permalink
Fixed a lot of runtime issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rster2002 committed Aug 7, 2021
1 parent 5343883 commit c8736dd
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 59 deletions.
55 changes: 46 additions & 9 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`{
key: ${properties.key},
first: ${properties.first},
Expand All @@ -395,6 +425,7 @@ export default class Block {

const init_declarations = [];
const init_statements = [];
const init_functions = [];

Array.from(this.variables.values()).forEach(({ id, init }) => {
init_declarations.push(b`let ${id};`);
Expand All @@ -407,9 +438,12 @@ export default class Block {
this.chunks.init.forEach(node => {
if (Array.isArray(node)) {
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 }) => {
init_declarations.push(b`let ${id}`);
init_declarations.push(b`let ${id}`); // TODO declaration is not always `let`
init_statements.push(b`${id} = ${init}`);
});
} else {
Expand All @@ -426,9 +460,11 @@ export default class Block {
${init_declarations}
${init_statements.length > 0
${init_statements.length > 0 || init_functions.length > 0
? b`
try {
${init_functions}
${init_statements}
} catch (e) {
@handle_error(@get_current_component(), e);
Expand Down Expand Up @@ -490,7 +526,6 @@ export default class Block {
render_listeners(chunk: string = '') {
if (this.event_listeners.length > 0) {
this.add_variable({ type: 'Identifier', name: '#mounted' });
this.chunks.destroy.push(b`#mounted = false`);

const dispose: Identifier = {
type: 'Identifier',
Expand All @@ -499,10 +534,6 @@ export default class Block {

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) {
this.chunks.mount.push(
b`
Expand All @@ -514,7 +545,11 @@ export default class Block {
);

this.chunks.destroy.push(
b`${dispose}();`
b`
if (#mounted) {
${dispose}();
}
`
);
} else {
this.chunks.mount.push(b`
Expand All @@ -530,6 +565,8 @@ export default class Block {
b`@run_all(${dispose});`
);
}

this.chunks.destroy.push(b`#mounted = false`);
}
}
}
32 changes: 14 additions & 18 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
import { extract_names, Scope } from 'periscopic';
import { invalidate } from './invalidate';
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 { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { flatten } from '../../utils/flatten';
Expand Down Expand Up @@ -462,16 +462,12 @@ export default function dom(
instance_javascript_with_ctx.push(node);

if (Array.isArray(node) && node[0].type === 'VariableDeclaration') {
walk(node[0], {
enter(declaration: Identifier) {
if (declaration.type === 'Identifier' && !initializedIdentifiers.includes(declaration.name)) {
const index = renderer.initial_context.findIndex(member => member.name === declaration.name);

if (index >= 0) {
node.push(x`#return_values[${index}] = ${declaration}`);
initializedIdentifiers.push(declaration.name);
}
}
node[0].declarations.forEach(({ id }) => {
const index = renderer.initial_context.findIndex(member => member.name === id.name);

if (index >= 0) {
instance_javascript_with_ctx.push(b`#return_values[${index}] = ${id};`[0]);
initializedIdentifiers.push(id.name);
}
});
}
Expand All @@ -481,7 +477,7 @@ export default function dom(
const index = renderer.initial_context.findIndex(member => member.name === node.id.name);

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);
}
}
Expand All @@ -492,6 +488,12 @@ export default function dom(

const instance_try_block: any = b`
try {
${reactive_store_declarations}
${reactive_store_subscriptions}
${resubscribable_reactive_store_unsubscribers}
${instance_javascript_with_ctx}
${unknown_props_check}
Expand Down Expand Up @@ -534,12 +536,6 @@ export default function dom(
${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.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`}
${compute_slots}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class EventHandlerWrapper {
}

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})`
);
}
}
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export default class ElementWrapper extends Wrapper {
);
} else {
block.event_listeners.push(
x`@listen(${this.var}, "${name}", ${callee})`
x`@listen(${this.var}, "${name}", @attach_error_handler(${this.var}, @get_current_component(), ${callee}))`
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function add_action(block: Block, target: string, action: Action) {
);
} else {
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}))`
);
}

Expand Down
48 changes: 21 additions & 27 deletions src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,35 +182,25 @@ export default function ssr(
return $$rendered;
`
: b`
try {
${instance_javascript}
${reactive_declarations}
${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`
${css.code ? b`
Expand All @@ -224,7 +214,11 @@ export default function ssr(
${component.fully_hoisted}
const ${name} = @create_ssr_component(($$result, $$props, $$bindings, #slots) => {
${blocks}
try {
${blocks}
} catch (e) {
@handle_error(@get_current_component(), e);
}
});
`;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function make_dirty(component, i) {
export function attach_error_handler(that: any, component, fn) {
return (...rest) => {
try {
fn.call(that, ...rest);
return fn.call(that, ...rest);
} catch (e) {
handle_error(component, e);
}
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Readable } from 'svelte/store';
import { attach_error_handler } from './Component';
import { get_current_component } from './lifecycle';

export function noop() {}

Expand Down Expand Up @@ -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 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;
}

0 comments on commit c8736dd

Please sign in to comment.