mirror of https://github.com/sveltejs/svelte
fix: correctly handle closure passed to $derived.by when destructuring (#11028)
* fix: correctly handle closure passed to $derived.by when destructuring * oopspull/11018/head
parent
ade3d1afb9
commit
2a784fce16
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"svelte": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: correctly handle closure passed to $derived.by when destructuring
|
@ -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…
Reference in new issue