mirror of https://github.com/sveltejs/svelte
fix: run effects in pending snippets (#17719)
Boundaries are buggy: if the `pending` snippet contains state, changes to that state [won't cause updates](https://svelte.dev/playground/hello-world?version=5.51.2#H4sIAAAAAAAAE22SQW-DMAyF_0qUTSpoE92uFJh223Hate0hJWaNliZRYsoqxH9fQtJW6noD-33Pz4aRKnYAWtIPkFKTQVvJSQZcIPCcPtNOSHC0XI8UTyboQsHXE_VuTOGOIDHUdszBvXqrFYJCb0Mr11phsNmoDUpAYsFpeQTrSE3W25Uv-0bXqxaFVsT0bp8dmewhJ2PobNB7OSQcOrAWuKc-rT4IB8UgcP91dsvyVZRf_IvZK8tJ3VzoInXTiCuDvVVXlYkT5u50k9DtRYfZJd11XGq8FSlKAsPOre4V-uSPDhlC9hIE1fJ6GFXtekRvrlUrRftTjzF25J5q8jrN9xOqtXDwhw14RO7jc5bIzI-3-vihyp3358yeZmFlmpENTGD8CIu0GV_kU7U0TdxmfHBKGON3MqC4UN9ZPsVDBHzOe1bjuEzaad7230j_nyD8Ii3R9jBt_RsTchCK07Jj0sH0B6hNF6aqAgAA): ```svelte <script> let resolvers = []; function push(value) { const deferred = Promise.withResolvers(); resolvers.push(() => deferred.resolve(value)); return deferred.promise; } function shift() { resolvers.shift()?.(); } let count = $state(0); </script> <button onclick={() => count += 1}> increment </button> <button onclick={shift}> shift </button> <svelte:boundary> <p>{await push('resolved')}</p> {#snippet pending()} <p>{count}</p> {/snippet} </svelte:boundary> ``` The issue is that the boundary's `this.#effect` has the `BOUNDARY_EFFECT` flag, and `this.#pending_effect` is a child thereof. Instead, `this.#main_effect` should have the flag. (It turns out `this.#failed_effect` _also_ needs the flag, because errors that occur in a `failed` snippet cause the boundary to re-render in its `failed` state, which I found somewhat confusing to be honest. Probably the right choice though.) I was able to simplify the code a bit, too. ~~(Actually now that I think about it do we need `this.#effect` at all? Will check.)~~ ### Before submitting the PR, please make sure you do the following - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>pull/17728/head
parent
ff70ab1b76
commit
6557a0a591
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: run effects in pending snippets
|
||||
@ -0,0 +1,38 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `
|
||||
<button>increment</button>
|
||||
<button>shift</button>
|
||||
<p>0</p>
|
||||
`,
|
||||
|
||||
async test({ assert, target }) {
|
||||
const [increment, shift] = target.querySelectorAll('button');
|
||||
|
||||
increment.click();
|
||||
await tick();
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>increment</button>
|
||||
<button>shift</button>
|
||||
<p>1</p>
|
||||
`
|
||||
);
|
||||
|
||||
shift.click();
|
||||
await tick();
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>increment</button>
|
||||
<button>shift</button>
|
||||
<p>resolved</p>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,31 @@
|
||||
<script>
|
||||
let resolvers = [];
|
||||
|
||||
function push(value) {
|
||||
const deferred = Promise.withResolvers();
|
||||
resolvers.push(() => deferred.resolve(value));
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
function shift() {
|
||||
resolvers.shift()?.();
|
||||
}
|
||||
|
||||
let count = $state(0);
|
||||
</script>
|
||||
|
||||
<button onclick={() => count += 1}>
|
||||
increment
|
||||
</button>
|
||||
|
||||
<button onclick={shift}>
|
||||
shift
|
||||
</button>
|
||||
|
||||
<svelte:boundary>
|
||||
<p>{await push('resolved')}</p>
|
||||
|
||||
{#snippet pending()}
|
||||
<p>{count}</p>
|
||||
{/snippet}
|
||||
</svelte:boundary>
|
||||
Loading…
Reference in new issue