mirror of https://github.com/sveltejs/svelte
fix: preserve old dependencies when updating reaction inside fork (#17579)
Co-authored-by: David Roizenman <hmnd@users.noreply.github.com>pull/17564/head
parent
ece2e83eb9
commit
3608b3c869
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: preserve old dependencies when updating reaction inside fork
|
||||
@ -0,0 +1,25 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
skip_no_async: true,
|
||||
async test({ assert, target }) {
|
||||
const [fork_btn, _toggle_btn, inc_count_1_btn] = target.querySelectorAll('button');
|
||||
const p = /** @type {HTMLElement} */ (target.querySelector('p'));
|
||||
|
||||
assert.equal(p.textContent, '0');
|
||||
|
||||
// Trigger derived to re-evaluate during fork and switch to tracking count_2
|
||||
flushSync(() => {
|
||||
fork_btn.click();
|
||||
});
|
||||
|
||||
assert.equal(p.textContent, '0');
|
||||
|
||||
flushSync(() => {
|
||||
inc_count_1_btn.click();
|
||||
});
|
||||
|
||||
assert.equal(p.textContent, '1');
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,27 @@
|
||||
<script>
|
||||
import { fork } from 'svelte';
|
||||
|
||||
let show_count_1 = $state(true);
|
||||
let count_1 = $state(0);
|
||||
let count_2 = $state(0);
|
||||
|
||||
const count = $derived(show_count_1 ? count_1 : count_2);
|
||||
</script>
|
||||
|
||||
<!-- This if block causes the derived to execute during the fork, switching its dependency
|
||||
away from count_1 and over to count_2. After discard, count_1 should still be tracked. -->
|
||||
{#if count}
|
||||
{/if}
|
||||
|
||||
<button onclick={() => {
|
||||
const f = fork(() => {
|
||||
show_count_1 = !show_count_1;
|
||||
});
|
||||
f.discard();
|
||||
}}>fork toggle</button>
|
||||
|
||||
<button onclick={() => show_count_1 = !show_count_1}>toggle</button>
|
||||
<button onclick={() => count_1++}>increment count 1</button>
|
||||
<button onclick={() => count_2++}>increment count 2</button>
|
||||
|
||||
<p>{count}</p>
|
||||
Loading…
Reference in new issue