Skip to content

Commit

Permalink
feat: derive destructured derived objects values (#10488)
Browse files Browse the repository at this point in the history
* feat: derive destructured derived objects values

* address feedback

* address feedback
  • Loading branch information
trueadm authored Feb 15, 2024
1 parent 302c379 commit 4b274dd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-humans-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: derive destructured derived objects values
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 4b274dd

Please sign in to comment.