feat: derive destructured derived objects values (#10488)

* feat: derive destructured derived objects values

* address feedback

* address feedback
pull/10500/head
Dominic Gannaway 10 months ago committed by GitHub
parent 302c379e01
commit 4b274dd00d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
feat: derive destructured derived objects values

@ -315,7 +315,16 @@ export const javascript_visitors_runes = {
)
);
for (let i = 0; i < bindings.length; i++) {
bindings[i].expression = b.member(b.call('$.get', b.id(id)), b.literal(i), true);
const binding = bindings[i];
declarations.push(
b.declarator(
binding.node,
b.call(
'$.derived',
b.thunk(b.member(b.call('$.get', b.id(id)), b.literal(i), true))
)
)
);
}
}
continue;

@ -0,0 +1,28 @@
import { test } from '../../test';
import { flushSync } from 'svelte';
import { log } from './log.js';
export default test({
before_test() {
log.length = 0;
},
async test({ assert, target }) {
const [b1, b2, b3] = target.querySelectorAll('button');
log.length = 0;
flushSync(() => {
b1.click();
});
assert.deepEqual(log, ['a', 1]);
log.length = 0;
flushSync(() => {
b2.click();
});
assert.deepEqual(log, ['b', 1]);
log.length = 0;
flushSync(() => {
b3.click();
});
assert.deepEqual(log, ['c', 1]);
}
});

@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];

@ -0,0 +1,24 @@
<script>
import { log } from './log.js';
let a = $state(0);
let b = $state(0);
let c = $state(0);
const {a: a1, b: b1, c: c1} = $derived({a, b, c});
$effect(() => {
log.push('a', a1)
});
$effect(() => {
log.push('b', b1)
});
$effect(() => {
log.push('c', c1)
});
</script>
<button onclick={() => a++}>{a1}</button>
<button onclick={() => b++}>{b1}</button>
<button onclick={() => c++}>{c1}</button>
Loading…
Cancel
Save