@@ -45,6 +45,52 @@ export function VariableDeclaration(node, context) {
4545
4646 continue ;
4747 }
48+ if (
49+ init . type === 'AwaitExpression' &&
50+ context . state . analysis . instance ?. scope === context . state . scope
51+ ) {
52+ const parallelize = can_be_parallelized (
53+ init . argument ,
54+ context . state . scope ,
55+ context . state . analysis ,
56+ context . state . current_parallelized_chunk ?. bindings ?? [ ]
57+ ) ;
58+ if ( parallelize ) {
59+ const bindings = context . state . scope . get_bindings ( declarator ) ;
60+ const visited = /** @type {VariableDeclarator } */ (
61+ context . visit ( {
62+ ...declarator ,
63+ init : init . argument
64+ } )
65+ ) ;
66+ const declarators = [
67+ {
68+ id : visited . id ,
69+ init : /** @type {Expression } */ ( visited . init )
70+ }
71+ ] ;
72+ if (
73+ context . state . current_parallelized_chunk &&
74+ context . state . current_parallelized_chunk . kind === node . kind
75+ ) {
76+ context . state . current_parallelized_chunk . declarators . push ( ...declarators ) ;
77+ context . state . current_parallelized_chunk . bindings . push ( ...bindings ) ;
78+ context . state . current_parallelized_chunk . position = /** @type {Program } */ (
79+ context . path . at ( - 1 )
80+ ) . body . indexOf ( node ) ;
81+ } else {
82+ const chunk = {
83+ kind : node . kind ,
84+ declarators,
85+ position : /** @type {Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
86+ bindings
87+ } ;
88+ context . state . current_parallelized_chunk = chunk ;
89+ context . state . parallelized_chunks . push ( chunk ) ;
90+ }
91+ continue ;
92+ }
93+ }
4894 declarations . push ( /** @type {VariableDeclarator } */ ( context . visit ( declarator ) ) ) ;
4995 continue ;
5096 }
@@ -215,11 +261,18 @@ export function VariableDeclaration(node, context) {
215261 // TODO make it work without this
216262 declarator . id . type === 'Identifier'
217263 ) {
218- parallelize = can_be_parallelized ( value , context . state . scope , context . state . analysis ) ;
264+ parallelize = can_be_parallelized (
265+ value ,
266+ context . state . scope ,
267+ context . state . analysis ,
268+ context . state . current_parallelized_chunk ?. bindings ?? [ ]
269+ ) ;
219270 }
220271
221272 /** @type {VariableDeclarator[] } */
222273 const derived_declarators = [ ] ;
274+ /** @type {Binding[] } */
275+ const bindings = [ ] ;
223276
224277 if ( declarator . id . type === 'Identifier' ) {
225278 let expression = /** @type {Expression } */ (
@@ -239,15 +292,13 @@ export function VariableDeclaration(node, context) {
239292
240293 if ( ! parallelize ) call = b . call ( b . await ( b . call ( '$.save' , call ) ) ) ;
241294 if ( dev ) call = b . call ( '$.tag' , call , b . literal ( declarator . id . name ) ) ;
242-
295+ bindings . push ( /** @type { Binding } */ ( context . state . scope . get ( declarator . id . name ) ) ) ;
243296 derived_declarators . push ( b . declarator ( declarator . id , call ) ) ;
244297 } else {
245298 if ( rune === '$derived' ) expression = b . thunk ( expression ) ;
246299
247300 let call = b . call ( '$.derived' , expression ) ;
248301 if ( dev ) call = b . call ( '$.tag' , call , b . literal ( declarator . id . name ) ) ;
249-
250- derived_declarators . push ( b . declarator ( declarator . id , call ) ) ;
251302 }
252303 } else {
253304 const init = /** @type {CallExpression } */ ( declarator . init ) ;
@@ -273,17 +324,13 @@ export function VariableDeclaration(node, context) {
273324 b . thunk ( expression , true ) ,
274325 location ? b . literal ( location ) : undefined
275326 ) ;
276- if ( ! parallelize ) {
277- call = b . call ( b . await ( b . call ( '$.save' , call ) ) ) ;
278- }
327+ call = b . call ( b . await ( b . call ( '$.save' , call ) ) ) ;
279328 }
280329
281330 if ( dev ) {
282331 const label = `[$derived ${ declarator . id . type === 'ArrayPattern' ? 'iterable' : 'object' } ]` ;
283332 call = b . call ( '$.tag' , call , b . literal ( label ) ) ;
284333 }
285-
286- derived_declarators . push ( b . declarator ( id , call ) ) ;
287334 }
288335
289336 const { inserts, paths } = extract_paths ( declarator . id , rhs ) ;
@@ -299,8 +346,6 @@ export function VariableDeclaration(node, context) {
299346 const label = `[$derived ${ declarator . id . type === 'ArrayPattern' ? 'iterable' : 'object' } ]` ;
300347 call = b . call ( '$.tag' , call , b . literal ( label ) ) ;
301348 }
302-
303- derived_declarators . push ( b . declarator ( id , call ) ) ;
304349 }
305350
306351 for ( const path of paths ) {
@@ -327,19 +372,22 @@ export function VariableDeclaration(node, context) {
327372 } ) ) ;
328373 if (
329374 context . state . current_parallelized_chunk &&
330- context . state . current_parallelized_chunk . kind === node . kind &&
331- context . state . current_parallelized_chunk . position ===
332- /** @type {Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node )
375+ context . state . current_parallelized_chunk . kind === node . kind
333376 ) {
334377 context . state . current_parallelized_chunk . declarators . push ( ...declarators ) ;
378+ context . state . current_parallelized_chunk . bindings . push ( ...bindings ) ;
379+ context . state . current_parallelized_chunk . position = /** @type {Program } */ (
380+ context . path . at ( - 1 )
381+ ) . body . indexOf ( node ) ;
335382 } else {
336383 const chunk = {
337384 kind : node . kind ,
338385 declarators,
339- position : /** @type {Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node )
386+ position : /** @type {Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
387+ bindings
340388 } ;
341389 context . state . current_parallelized_chunk = chunk ;
342- context . state . parallelized_derived_chunks . push ( chunk ) ;
390+ context . state . parallelized_chunks . push ( chunk ) ;
343391 }
344392 }
345393
0 commit comments