mirror of https://github.com/sveltejs/svelte
fix: don't assume boundary exists during increment/decrement (#18289)
Follow-up to #18273 (not merged yet hence no changeset here): We can run into a null-pointer when wanting to increment/decrement inside an effect root that is outside a the component tree. Similarly, if not using the component logic to unset context at the right time we gotta do it "manually" inside `save`. --------- Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>pull/18302/head
parent
c01e598fff
commit
b40c359d44
@ -1,11 +0,0 @@
|
||||
<script>
|
||||
setTimeout(() => {
|
||||
$effect.root(() => {
|
||||
async function fn() {
|
||||
const value = $derived(await 1);
|
||||
return { get value() { return value } };
|
||||
}
|
||||
fn().then(r => console.log(r.value));
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@ -1,9 +1,15 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
// Test that an async derived inside an $effect.root not connected to the component tree still works
|
||||
async test({ assert, logs }) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
assert.deepEqual(logs, [1]);
|
||||
assert.deepEqual(logs, [1, 1]);
|
||||
const [button] = document.querySelectorAll('button');
|
||||
|
||||
button.click();
|
||||
await tick();
|
||||
assert.deepEqual(logs, [1, 1, 2]);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,19 @@
|
||||
<script>
|
||||
let increment;
|
||||
|
||||
setTimeout(() => {
|
||||
$effect.root(() => {
|
||||
async function fn() {
|
||||
let count = $state(1);
|
||||
increment = () => { count++; };
|
||||
const value = $derived(await count);
|
||||
$effect.pre(() => console.log(value))
|
||||
return { get value() { return value } };
|
||||
}
|
||||
fn().then(r => console.log(r.value));
|
||||
})
|
||||
})
|
||||
|
||||
</script>
|
||||
|
||||
<button onclick={() => increment()}>increment</button>
|
||||
@ -0,0 +1,14 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, logs }) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
assert.deepEqual(logs, [1, 1]);
|
||||
const [button] = document.querySelectorAll('button');
|
||||
|
||||
button.click();
|
||||
await tick();
|
||||
assert.deepEqual(logs, [1, 1, 2]);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,15 @@
|
||||
<script>
|
||||
let increment;
|
||||
|
||||
async function fn() {
|
||||
let count = $state(1);
|
||||
increment = () => { count++; };
|
||||
const value = $derived(await count);
|
||||
$effect.pre(() => console.log(value))
|
||||
return { get value() { return value } };
|
||||
}
|
||||
fn().then(r => console.log(r.value));
|
||||
|
||||
</script>
|
||||
|
||||
<button onclick={() => increment()}>increment</button>
|
||||
Loading…
Reference in new issue