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/strong-berries-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: store forked derived values
3 changes: 3 additions & 0 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,15 @@ export function fork(fn) {

var batch = Batch.ensure();
batch.is_fork = true;
batch_values = new Map();

var committed = false;
var settled = batch.settled();

flushSync(fn);

batch_values = null;

// revert state changes
for (var [source, value] of batch.previous) {
source.v = value;
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export function update_derived(derived) {
if (batch_values !== null) {
// only cache the value if we're in a tracking context, otherwise we won't
// clear the cache in `mark_reactions` when dependencies are updated
if (effect_tracking()) {
if (effect_tracking() || current_batch?.is_fork) {
batch_values.set(derived, value);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target, logs }) {
const fork = target.querySelector('button');

flushSync(() => {
fork?.click();
});

assert.deepEqual(logs, [1]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { fork } from "svelte";

let state = $state(0);
let count = $derived(state);
</script>

<button onclick={() => {
fork(() => {
state++;
console.log(count);
});
}}>fork</button>
Loading