Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/common-boats-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent reactivity loss during fork
20 changes: 20 additions & 0 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export class Batch {

is_fork = false;

/**
* Branches that had their CLEAN flag toggled during fork execution
* @type {Set<Effect> | null}
*/
toggled_branches = null;

is_deferred() {
return this.is_fork || this.#blocking_pending > 0;
}
Expand Down Expand Up @@ -862,6 +868,12 @@ export function schedule_effect(signal) {
if ((flags & (ROOT_EFFECT | BRANCH_EFFECT)) !== 0) {
if ((flags & CLEAN) === 0) return;
effect.f ^= CLEAN;

// Track branches toggled during fork execution so we can restore
// their CLEAN flag after flush
if (current_batch !== null && current_batch.is_fork) {
(current_batch.toggled_branches ??= new Set()).add(effect);
}
}
}

Expand Down Expand Up @@ -964,6 +976,14 @@ export function fork(fn) {

flushSync(fn);

// Restore CLEAN flags that were toggled during fork initialization.
if (batch.toggled_branches !== null) {
for (const effect of batch.toggled_branches) {
effect.f |= CLEAN;
}
batch.toggled_branches = null;
}

batch_values = null;

// revert state changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let count = $state(0);
</script>

<button onclick={() => count++}>{count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target }) {
const [forkBtn, counterBtn] = target.querySelectorAll('button');

flushSync(() => {
forkBtn.click();
});

assert.equal(counterBtn.textContent, '0');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '1');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import { fork } from 'svelte';
import Child from './Child.svelte';

let show = $state(false);
</script>

<button onclick={() => {
let f = fork(() => {
show = !show;
});
f.discard();
}}>fork</button>

{#if show}
hi
{:else}
{#if show || !show}
<Child />
{/if}
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let count = $state(0);
</script>

<button onclick={() => count++}>{count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target }) {
const [forkBtn, counterBtn] = target.querySelectorAll('button');

flushSync(() => {
forkBtn.click();
});

assert.equal(counterBtn.textContent, '0');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '1');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '2');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { fork } from 'svelte';
import Child from './Child.svelte';

let show = $state(false);
let pendingFork = $state(null);
</script>

<button onclick={() => {
// Create fork but don't discard to simulate SvelteKit preload
pendingFork = fork(() => {
show = !show;
});
}}>fork</button>

{#if show}
hi
{:else}
{#if show || !show}
<Child />
{/if}
{/if}
Loading