fix: correctly handle closure passed to $derived.by when destructuring (#11028)

* fix: correctly handle closure passed to $derived.by when destructuring

* oops
pull/11018/head
Dominic Gannaway 1 year ago committed by GitHub
parent ade3d1afb9
commit 2a784fce16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: correctly handle closure passed to $derived.by when destructuring

@ -301,7 +301,7 @@ export const javascript_visitors_runes = {
declarations.push(
b.declarator(
b.id(object_id),
b.call('$.derived', b.thunk(rune === '$derived.by' ? b.call(value) : value))
b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
)
);
declarations.push(

@ -0,0 +1,19 @@
import { test } from '../../test';
import { log } from './log.js';
export default test({
before_test() {
log.length = 0;
},
html: `<button>0</button>`,
async test({ assert, target, window }) {
const btn = target.querySelector('button');
const clickEvent = new window.Event('click', { bubbles: true });
await btn?.dispatchEvent(clickEvent);
assert.htmlEqual(target.innerHTML, `<button>2</button>`);
assert.deepEqual(log, ['create_derived']);
}
});

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

@ -0,0 +1,18 @@
<script>
import {log} from './log.js'
let count = $state(0);
function create_derived() {
log.push('create_derived');
return () => {
return {
get double() {
return count * 2;
}
}
}
}
let {double} = $derived.by(create_derived());
</script>
<button on:click={() => count++}>{double}</button>
Loading…
Cancel
Save