@@ -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}
0 commit comments