Skip to content

Commit c8736dd

Browse files
committed
Fixed a lot of runtime issues
1 parent 5343883 commit c8736dd

File tree

8 files changed

+88
-59
lines changed

8 files changed

+88
-59
lines changed

src/compiler/compile/render_dom/Block.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,36 @@ export default class Block {
375375
}
376376
}
377377

378+
if (properties.create.type === 'FunctionExpression') {
379+
properties.create.body.body = b`
380+
try {
381+
${properties.create.body.body}
382+
} catch (e) {
383+
@handle_error(@get_current_component(), e);
384+
}
385+
`;
386+
}
387+
388+
if (properties.mount.type === 'FunctionExpression') {
389+
properties.mount.body.body = b`
390+
try {
391+
${properties.mount.body.body}
392+
} catch (e) {
393+
@handle_error(@get_current_component(), e);
394+
}
395+
`;
396+
}
397+
398+
if (properties.hydrate?.type === 'FunctionExpression') {
399+
properties.hydrate.body.body = b`
400+
try {
401+
${properties.hydrate.body.body}
402+
} catch (e) {
403+
@handle_error(@get_current_component(), e);
404+
}
405+
`;
406+
}
407+
378408
const return_value: any = x`{
379409
key: ${properties.key},
380410
first: ${properties.first},
@@ -395,6 +425,7 @@ export default class Block {
395425

396426
const init_declarations = [];
397427
const init_statements = [];
428+
const init_functions = [];
398429

399430
Array.from(this.variables.values()).forEach(({ id, init }) => {
400431
init_declarations.push(b`let ${id};`);
@@ -407,9 +438,12 @@ export default class Block {
407438
this.chunks.init.forEach(node => {
408439
if (Array.isArray(node)) {
409440
node.forEach((declaration: any) => { // TODO add type to this
410-
if (declaration.declarations) {
441+
if (declaration.type === 'FunctionDeclaration') {
442+
init_declarations.push(b`let ${declaration.id};`);
443+
init_functions.push(b`${declaration.id} = ${declaration}`);
444+
} else if (declaration.type === 'VariableDeclaration') {
411445
declaration.declarations.forEach(({ id, init }) => {
412-
init_declarations.push(b`let ${id}`);
446+
init_declarations.push(b`let ${id}`); // TODO declaration is not always `let`
413447
init_statements.push(b`${id} = ${init}`);
414448
});
415449
} else {
@@ -426,9 +460,11 @@ export default class Block {
426460
427461
${init_declarations}
428462
429-
${init_statements.length > 0
463+
${init_statements.length > 0 || init_functions.length > 0
430464
? b`
431465
try {
466+
${init_functions}
467+
432468
${init_statements}
433469
} catch (e) {
434470
@handle_error(@get_current_component(), e);
@@ -490,7 +526,6 @@ export default class Block {
490526
render_listeners(chunk: string = '') {
491527
if (this.event_listeners.length > 0) {
492528
this.add_variable({ type: 'Identifier', name: '#mounted' });
493-
this.chunks.destroy.push(b`#mounted = false`);
494529

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

500535
this.add_variable(dispose);
501536

502-
this.event_listeners.forEach((event_listener: any) => {
503-
event_listener.arguments[2] = x`@attach_error_handler(${event_listener.arguments[0]}, @get_current_component(), ${event_listener.arguments[2]})`;
504-
});
505-
506537
if (this.event_listeners.length === 1) {
507538
this.chunks.mount.push(
508539
b`
@@ -514,7 +545,11 @@ export default class Block {
514545
);
515546

516547
this.chunks.destroy.push(
517-
b`${dispose}();`
548+
b`
549+
if (#mounted) {
550+
${dispose}();
551+
}
552+
`
518553
);
519554
} else {
520555
this.chunks.mount.push(b`
@@ -530,6 +565,8 @@ export default class Block {
530565
b`@run_all(${dispose});`
531566
);
532567
}
568+
569+
this.chunks.destroy.push(b`#mounted = false`);
533570
}
534571
}
535572
}

src/compiler/compile/render_dom/index.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
66
import { extract_names, Scope } from 'periscopic';
77
import { invalidate } from './invalidate';
88
import Block from './Block';
9-
import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression, Identifier } from 'estree';
9+
import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree';
1010
import { apply_preprocessor_sourcemap } from '../../utils/mapped_code';
1111
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
1212
import { flatten } from '../../utils/flatten';
@@ -462,16 +462,12 @@ export default function dom(
462462
instance_javascript_with_ctx.push(node);
463463

464464
if (Array.isArray(node) && node[0].type === 'VariableDeclaration') {
465-
walk(node[0], {
466-
enter(declaration: Identifier) {
467-
if (declaration.type === 'Identifier' && !initializedIdentifiers.includes(declaration.name)) {
468-
const index = renderer.initial_context.findIndex(member => member.name === declaration.name);
469-
470-
if (index >= 0) {
471-
node.push(x`#return_values[${index}] = ${declaration}`);
472-
initializedIdentifiers.push(declaration.name);
473-
}
474-
}
465+
node[0].declarations.forEach(({ id }) => {
466+
const index = renderer.initial_context.findIndex(member => member.name === id.name);
467+
468+
if (index >= 0) {
469+
instance_javascript_with_ctx.push(b`#return_values[${index}] = ${id};`[0]);
470+
initializedIdentifiers.push(id.name);
475471
}
476472
});
477473
}
@@ -481,7 +477,7 @@ export default function dom(
481477
const index = renderer.initial_context.findIndex(member => member.name === node.id.name);
482478

483479
if (index >= 0) {
484-
instance_javascript_with_ctx.push(x`#return_values[${index}] = ${node.id.name}`);
480+
instance_javascript_with_ctx.push(b`#return_values[${index}] = ${node.id};`[0]);
485481
initializedIdentifiers.push(node.id.name);
486482
}
487483
}
@@ -492,6 +488,12 @@ export default function dom(
492488

493489
const instance_try_block: any = b`
494490
try {
491+
${reactive_store_declarations}
492+
493+
${reactive_store_subscriptions}
494+
495+
${resubscribable_reactive_store_unsubscribers}
496+
495497
${instance_javascript_with_ctx}
496498
497499
${unknown_props_check}
@@ -534,12 +536,6 @@ export default function dom(
534536
535537
${rest}
536538
537-
${reactive_store_declarations}
538-
539-
${reactive_store_subscriptions}
540-
541-
${resubscribable_reactive_store_unsubscribers}
542-
543539
${component.slots.size || component.compile_options.dev || uses_slots ? b`let { $$slots: #slots = {}, $$scope } = $$props;` : null}
544540
${component.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`}
545541
${compute_slots}

src/compiler/compile/render_dom/wrappers/Element/EventHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default class EventHandlerWrapper {
6767
}
6868

6969
block.event_listeners.push(
70-
x`@listen(${target}, "${this.node.name}", ${snippet}, ${args})`
70+
x`@listen(${target}, "${this.node.name}", @attach_error_handler(${target}, @get_current_component(), ${snippet}), ${args})`
7171
);
7272
}
7373
}

src/compiler/compile/render_dom/wrappers/Element/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ export default class ElementWrapper extends Wrapper {
541541
);
542542
} else {
543543
block.event_listeners.push(
544-
x`@listen(${this.var}, "${name}", ${callee})`
544+
x`@listen(${this.var}, "${name}", @attach_error_handler(${this.var}, @get_current_component(), ${callee}))`
545545
);
546546
}
547547
});

src/compiler/compile/render_dom/wrappers/shared/add_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function add_action(block: Block, target: string, action: Action) {
4040
);
4141
} else {
4242
block.event_listeners.push(
43-
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
43+
x`@action_destroyer(${id} = @attach_error_handler(null, @get_current_component(), ${fn}).call(null, ${target}, ${snippet}))`
4444
);
4545
}
4646

src/compiler/compile/render_ssr/index.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,25 @@ export default function ssr(
182182
return $$rendered;
183183
`
184184
: b`
185-
try {
186-
${instance_javascript}
185+
${reactive_declarations}
187186
188-
${reactive_declarations}
187+
${reactive_store_unsubscriptions}
188+
189+
return ${literal};`;
190+
191+
const blocks = [
192+
...injected.map(name => b`let ${name};`),
193+
rest,
194+
slots,
195+
...reactive_store_declarations,
196+
...reactive_store_subscriptions,
197+
instance_javascript,
198+
...parent_bindings,
199+
css.code && b`$$result.css.add(#css);`,
200+
main
201+
].filter(Boolean);
189202

190-
${reactive_store_unsubscriptions}
191203

192-
return ${literal};
193-
} catch (e) {
194-
@handle_error(@get_current_component(), e);
195-
}`;
196-
197-
const blocks = [
198-
...injected.map(name => b`let ${name};`),
199-
rest,
200-
slots,
201-
...reactive_store_declarations,
202-
...reactive_store_subscriptions,
203-
// b`
204-
// try {
205-
// ${instance_javascript}
206-
// } catch (e) {
207-
// @handle_error(@get_current_component(), e);
208-
// }
209-
// `,
210-
...parent_bindings,
211-
css.code && b`$$result.css.add(#css);`,
212-
main
213-
].filter(Boolean);
214204

215205
const js = b`
216206
${css.code ? b`
@@ -224,7 +214,11 @@ export default function ssr(
224214
${component.fully_hoisted}
225215
226216
const ${name} = @create_ssr_component(($$result, $$props, $$bindings, #slots) => {
227-
${blocks}
217+
try {
218+
${blocks}
219+
} catch (e) {
220+
@handle_error(@get_current_component(), e);
221+
}
228222
});
229223
`;
230224

src/runtime/internal/Component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function make_dirty(component, i) {
109109
export function attach_error_handler(that: any, component, fn) {
110110
return (...rest) => {
111111
try {
112-
fn.call(that, ...rest);
112+
return fn.call(that, ...rest);
113113
} catch (e) {
114114
handle_error(component, e);
115115
}

src/runtime/internal/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Readable } from 'svelte/store';
2+
import { attach_error_handler } from './Component';
3+
import { get_current_component } from './lifecycle';
24

35
export function noop() {}
46

@@ -185,5 +187,5 @@ export function set_store_value(store, ret, value) {
185187
export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
186188

187189
export function action_destroyer(action_result) {
188-
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;
190+
return action_result && is_function(action_result.destroy) ? attach_error_handler(action_result, get_current_component(), action_result.destroy) : noop;
189191
}

0 commit comments

Comments
 (0)