fix: don't run teardown effects when deriveds are unfreezed (#18227)

The logic was flawed - a teardown effect only has a teardown function
but not `fn` property, but unfreeze thought that everything with a
`teardown` needs to be unfreezed

Helps with #18221 (though likely doesn't fix it completely, at least not
the more general `batch.#roots`problems)
pull/18239/head
Simon H 2 weeks ago committed by GitHub
parent 1e899cba35
commit dc1f037fa6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't run teardown effects when deriveds are unfreezed

@ -452,8 +452,8 @@ export function freeze_derived_effects(derived) {
// make it a noop so it doesn't get called again if the derived
// is unfrozen. we don't set it to `null`, because the existence
// of a teardown function is what determines whether the
// effect runs again during unfreezing
e.teardown = noop;
// effect runs again during unfreezing (but not for teardown-only effects)
if (e.fn !== null) e.teardown = noop;
e.ac = null;
remove_reactions(e, 0);
@ -471,7 +471,7 @@ export function unfreeze_derived_effects(derived) {
for (const e of derived.effects) {
// if the effect was previously frozen — indicated by the presence
// of a teardown function — unfreeze it
if (e.teardown) {
if (e.teardown && e.fn !== null) {
update_effect(e);
}
}

@ -0,0 +1,21 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
compileOptions: { dev: true }, // for testing that teardown effect in eager $.get(loaded) doesn't lead to a crash (because it means REACTION_RAN is set, which means unfreeze_derived runs)
async test({ assert, target }) {
const [count, shift] = target.querySelectorAll('button');
shift.click();
await tick();
assert.htmlEqual(target.innerHTML, `<button>0</button><button>shift</button><p>0</p>`);
count.click();
await tick();
assert.htmlEqual(target.innerHTML, `<button>1</button><button>shift</button><p>0 (...)</p>`);
shift.click();
await tick();
assert.htmlEqual(target.innerHTML, `<button>1</button><button>shift</button><p>1</p>`);
}
});

@ -0,0 +1,21 @@
<script>
let count = $state(0);
let resolvers = [];
function push(value) {
const { promise, resolve } = Promise.withResolvers();
resolvers.push(() => resolve(value));
return promise;
}
</script>
<button onclick={() => count += 1}>{$state.eager(count)}</button>
<button onclick={() => resolvers.shift()?.()}>shift</button>
<svelte:boundary>
{@const loaded = $state.eager(count) === count}
<p>{await push(count)} {loaded ? '' : '(...)'}</p>
{#snippet pending()}{/snippet}
</svelte:boundary>
Loading…
Cancel
Save