mirror of https://github.com/sveltejs/svelte
chore: add $derived.call rune (#10240)
* chore: add $derived.fn rune * fix strange bug * update types * remove prev stuff * regenerate types * $derived.fn -> $derived.call * docs * regenerate types * get rid of $$derived * tighten up validation etc * fix tests --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/10324/head
parent
76a4bbd5ec
commit
d9a6b8b17a
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"svelte": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore: add $derived.call rune
|
@ -0,0 +1,30 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
html: `
|
||||||
|
<button>0</button>
|
||||||
|
<p>doubled: 0</p>
|
||||||
|
`,
|
||||||
|
|
||||||
|
async test({ assert, target }) {
|
||||||
|
const btn = target.querySelector('button');
|
||||||
|
|
||||||
|
await btn?.click();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>1</button>
|
||||||
|
<p>doubled: 2</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
await btn?.click();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>2</button>
|
||||||
|
<p>doubled: 4</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,11 @@
|
|||||||
|
<script>
|
||||||
|
class Counter {
|
||||||
|
count = $state(0);
|
||||||
|
doubled = $derived.call(() => this.count * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
const counter = new Counter();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button on:click={() => counter.count++}>{counter.count}</button>
|
||||||
|
<p>doubled: {counter.doubled}</p>
|
@ -0,0 +1,13 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
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>`);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,6 @@
|
|||||||
|
<script>
|
||||||
|
let count = $state(0);
|
||||||
|
let double = $derived.call(() => count * 2);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button on:click={() => count++}>{double}</button>
|
Loading…
Reference in new issue