@@ -14,7 +14,7 @@ import { locator } from '../../../../../state.js';
14
14
* @param {Array<AST.Text | AST.ExpressionTag> } values
15
15
* @param {(node: AST.SvelteNode, state: any) => any } visit
16
16
* @param {ComponentClientTransformState } state
17
- * @returns {{ value: Expression, has_state: boolean, has_call: boolean, is_async: boolean } }
17
+ * @returns {{ value: Expression, has_state: boolean, has_call: boolean } }
18
18
*/
19
19
export function build_template_chunk ( values , visit , state ) {
20
20
/** @type {Expression[] } */
@@ -26,16 +26,15 @@ export function build_template_chunk(values, visit, state) {
26
26
let has_call = false ;
27
27
let has_state = false ;
28
28
let is_async = false ;
29
- let contains_multiple_call_expression = false ;
29
+ let should_memoize = false ;
30
30
31
31
for ( const node of values ) {
32
32
if ( node . type === 'ExpressionTag' ) {
33
33
const metadata = node . metadata . expression ;
34
34
35
- contains_multiple_call_expression ||= has_call && metadata . has_call ;
35
+ should_memoize ||= ( has_call || is_async ) && ( metadata . has_call || metadata . is_async ) ;
36
36
has_call ||= metadata . has_call ;
37
37
has_state ||= metadata . has_state ;
38
- is_async ||= metadata . is_async ;
39
38
}
40
39
}
41
40
@@ -49,32 +48,26 @@ export function build_template_chunk(values, visit, state) {
49
48
quasi . value . cooked += node . expression . value + '' ;
50
49
}
51
50
} else {
52
- if ( contains_multiple_call_expression ) {
53
- const id = b . id ( state . scope . generate ( 'stringified_text' ) ) ;
51
+ const expression = /** @type {Expression } */ ( visit ( node . expression , state ) ) ;
52
+
53
+ if ( node . metadata . expression . is_async ) {
54
+ const id = b . id ( state . scope . generate ( 'expression' ) ) ;
55
+ state . metadata . async . push ( { id, expression : b . logical ( '??' , expression , b . literal ( '' ) ) } ) ;
56
+
57
+ expressions . push ( b . call ( id ) ) ;
58
+ } else if ( node . metadata . expression . has_call && should_memoize ) {
59
+ const id = b . id ( state . scope . generate ( 'expression' ) ) ;
54
60
state . init . push (
55
- b . const (
56
- id ,
57
- create_derived (
58
- state ,
59
- b . thunk (
60
- b . logical (
61
- '??' ,
62
- /** @type {Expression } */ ( visit ( node . expression , state ) ) ,
63
- b . literal ( '' )
64
- ) ,
65
- is_async
66
- )
67
- )
68
- )
61
+ b . const ( id , create_derived ( state , b . thunk ( b . logical ( '??' , expression , b . literal ( '' ) ) ) ) )
69
62
) ;
70
63
71
- expressions . push ( is_async ? b . await ( b . call ( '$.get' , id ) ) : b . call ( '$.get' , id ) ) ;
64
+ expressions . push ( b . call ( '$.get' , id ) ) ;
72
65
} else if ( values . length === 1 ) {
73
66
// If we have a single expression, then pass that in directly to possibly avoid doing
74
67
// extra work in the template_effect (instead we do the work in set_text).
75
- return { value : visit ( node . expression , state ) , has_state, has_call, is_async } ;
68
+ return { value : expression , has_state, has_call } ;
76
69
} else {
77
- expressions . push ( b . logical ( '??' , visit ( node . expression , state ) , b . literal ( '' ) ) ) ;
70
+ expressions . push ( b . logical ( '??' , expression , b . literal ( '' ) ) ) ;
78
71
}
79
72
80
73
quasi = b . quasi ( '' , i + 1 === values . length ) ;
@@ -88,28 +81,26 @@ export function build_template_chunk(values, visit, state) {
88
81
89
82
const value = b . template ( quasis , expressions ) ;
90
83
91
- return { value, has_state, has_call, is_async } ;
84
+ return { value, has_state, has_call } ;
92
85
}
93
86
94
87
/**
95
88
* @param {Statement } statement
96
- * @param {boolean } is_async
97
89
*/
98
- export function build_update ( statement , is_async ) {
90
+ export function build_update ( statement ) {
99
91
const body =
100
92
statement . type === 'ExpressionStatement' ? statement . expression : b . block ( [ statement ] ) ;
101
93
102
- return b . stmt ( b . call ( '$.template_effect' , b . thunk ( body , is_async ) ) ) ;
94
+ return b . stmt ( b . call ( '$.template_effect' , b . thunk ( body ) ) ) ;
103
95
}
104
96
105
97
/**
106
98
* @param {Statement[] } update
107
- * @param {boolean } is_async
108
99
*/
109
- export function build_render_statement ( update , is_async ) {
100
+ export function build_render_statement ( update ) {
110
101
return update . length === 1
111
- ? build_update ( update [ 0 ] , is_async )
112
- : b . stmt ( b . call ( '$.template_effect' , b . thunk ( b . block ( update ) , is_async ) ) ) ;
102
+ ? build_update ( update [ 0 ] )
103
+ : b . stmt ( b . call ( '$.template_effect' , b . thunk ( b . block ( update ) ) ) ) ;
113
104
}
114
105
115
106
/**
0 commit comments