Skip to content

Commit c8f979b

Browse files
committed
fix: allow async destructured deriveds
1 parent 05f6436 commit c8f979b

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

.changeset/mighty-balloons-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: allow async destructured deriveds

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,15 @@ export function VariableDeclaration(node, context) {
174174
const call = b.call('$.derived', expression);
175175
return b.declarator(
176176
id,
177-
dev ? b.call('$.tag', call, b.literal('[$state iterable]')) : call
177+
dev
178+
? b.call(
179+
'$.tag',
180+
call,
181+
b.literal(
182+
`[$state ${declarator.id.type === 'ArrayPattern' ? 'iterable' : 'object'}]`
183+
)
184+
)
185+
: call
178186
);
179187
}),
180188
...paths.map((path) => {
@@ -228,19 +236,37 @@ export function VariableDeclaration(node, context) {
228236
}
229237
} else {
230238
const init = /** @type {CallExpression} */ (declarator.init);
231-
239+
let expression = /** @type {Expression} */ (
240+
context.visit(value, {
241+
...context.state,
242+
in_derived: rune === '$derived'
243+
})
244+
);
232245
let rhs = value;
233246

234247
if (rune !== '$derived' || init.arguments[0].type !== 'Identifier') {
235248
const id = b.id(context.state.scope.generate('$$d'));
236249
rhs = b.call('$.get', id);
237-
238-
let expression = /** @type {Expression} */ (context.visit(value));
239-
if (rune === '$derived') expression = b.thunk(expression);
240-
const call = b.call('$.derived', expression);
241-
declarations.push(
242-
b.declarator(id, dev ? b.call('$.tag', call, b.literal('[$derived iterable]')) : call)
243-
);
250+
let call = b.call('$.derived', rune === '$derived' ? b.thunk(expression) : expression);
251+
if (is_async) {
252+
const location = dev && !is_ignored(init, 'await_waterfall') && locate_node(init);
253+
call = b.call(
254+
'$.async_derived',
255+
b.thunk(expression, true),
256+
location ? b.literal(location) : undefined
257+
);
258+
call = b.call(b.await(b.call('$.save', call)));
259+
}
260+
if (dev) {
261+
call = b.call(
262+
'$.tag',
263+
call,
264+
b.literal(
265+
`[$derived ${declarator.id.type === 'ArrayPattern' ? 'iterable' : 'object'}]`
266+
)
267+
);
268+
}
269+
declarations.push(b.declarator(id, call));
244270
}
245271

246272
const { inserts, paths } = extract_paths(declarator.id, rhs);
@@ -252,7 +278,18 @@ export function VariableDeclaration(node, context) {
252278
const expression = /** @type {Expression} */ (context.visit(b.thunk(value)));
253279
const call = b.call('$.derived', expression);
254280
declarations.push(
255-
b.declarator(id, dev ? b.call('$.tag', call, b.literal('[$derived iterable]')) : call)
281+
b.declarator(
282+
id,
283+
dev
284+
? b.call(
285+
'$.tag',
286+
call,
287+
b.literal(
288+
`[$derived ${declarator.id.type === 'ArrayPattern' ? 'iterable' : 'object'}]`
289+
)
290+
)
291+
: call
292+
)
256293
);
257294
}
258295

0 commit comments

Comments
 (0)